ARLTS is a free, non-profit English-learning app for learners in Vietnam. For months, every label in it was English. Then learners asked for Vietnamese.
There's a catch here. In a language app, half the text on the page is the interface (buttons, menus, tips, a running tally of right and wrong) and half is the thing being learned (words, meanings, example lines, answer tiles). A learner who turns on Vietnamese wants "Log in" to read "Đăng nhập". They don't want the word they're learning to turn Vietnamese too. That would break every activity.
The app was already large, and its labels were written right into the HTML and into code in dozens of activities and games. Moving every label into an i18n library would have meant editing thousands of lines. Our way: leave the English page alone, and translate it on the page, from the outside.
The idea
One block of code, about 100 lines, does 4 things:
- A dictionary maps exact English labels to Vietnamese (about 1,580 of them today).
- It walks the text nodes inside a list of interface areas and replaces any node whose whole text matches a key.
- It never goes into a list of learning-content areas, whatever they hold.
- It keeps the English it replaced, and turning English back on puts back exactly what was there.
Exact matches only
The dictionary is a plain map:
const VI = {
"Learn": "Học",
"Log in": "Đăng nhập",
"Create account": "Tạo tài khoản",
// ...about 1,580 more
};
A text node changes only when its whole text, trimmed, is a key. Never part of a line. "Learn" turns into "Học", but "Learn more about this word" keeps its English until it gets its own entry. That one rule is the whole guard: an unknown label is left alone, and the worst outcome is a button in English, never a broken line.
function viOf(text) {
const key = text.trim().replace(/\s+/g, " ");
if (!key) return null;
if (Object.hasOwn(VI, key)) return text.replace(text.trim(), VI[key]);
return null;
}
Lines with numbers in them
Exact matches can't cover "Right: 7/10" or "(or colour)." For those there's a brief list of patterns. The frame turns Vietnamese; the parts it captures, numbers or English words, go through untouched:
const PATTERNS = [
[/^Right: (\d+)\/(\d+)$/, "Đúng: $1/$2"],
[/^\(or (.+)\)\.$/, "(hoặc $1)."],
];
The latter matters: "colour" is another accepted answer, an English word, and it keeps its English inside a Vietnamese line.
Where to walk, and where not to
The toggle works only inside a list of interface roots (the top bar, the footer, the frame of each page), and it ignores a list of content areas. Ours names the word cards, answer tiles, example lines, and game boards:
const SKIP = ".word, .meaning, .example, .answer-tile, .game-board, [data-vi-skip]";
function walk(root) {
const w = document.createTreeWalker(root, NodeFilter.SHOW_TEXT);
const nodes = [];
let n;
while ((n = w.nextNode())) nodes.push(n);
nodes.forEach(swap);
}
function swap(node) {
const el = node.parentElement;
if (!el || el.closest(SKIP)) return;
const vi = viOf(node.nodeValue);
if (vi === null || vi === node.nodeValue) return;
changed.push([node, node.nodeValue]); // remember the English
node.nodeValue = vi;
}
The [data-vi-skip] attribute is the way out for anything new: a new activity can mark its content with it, and nobody has to edit the toggle.
Text nodes aren't the whole interface. Hover tips, box hints, and labels for people who use a reader app live in attributes, and the lookup runs on title, placeholder, and aria-label too. A Vietnamese learner who listens to the page hears Vietnamese labels.
Text that turns up later
The page keeps changing after load: a tally goes up, a pop-up opens, a game begins. A MutationObserver on each root hands new nodes, edited text, and changed attributes back to the functions above:
const obs = new MutationObserver((records) => {
for (const r of records) {
if (r.type === "characterData") swap(r.target);
else if (r.type === "attributes") swapAttrs(r.target);
else r.addedNodes.forEach(walk);
}
});
roots.forEach((r) =>
obs.observe(r, {
childList: true,
characterData: true,
subtree: true,
attributes: true,
attributeFilter: ["title", "placeholder", "aria-label"],
})
);
Pop-ups that the page adds right into <body> (dialogs, toasts, menus) get a watcher on <body> itself, with childList only. Each new pop-up gets walked and watched the moment it arrives.
3 traps we hit
-
Our own writes come back. Writing to
nodeValuewakes the observer, which looks at the new text and tries again. Harmless, until a pattern matched its own output: a date turned from mm/dd into dd/mm matched the date pattern again and flipped back. The fix is aWeakMapfrom each node to the text we wrote, and the observer ignores nodes whose value is the one we put there. -
Undo has to look before it puts things back. Turning English back on walks the kept
[node, english]pairs in reverse. But the page may have changed a node in the meantime (a tally went from 7 to 8). Our undo puts the English back only if the node holds the Vietnamese for that English. If not, the page's newer text wins. -
Iframes are their own documents. Our games run in iframes built from the page itself, and a
TreeWalkeron the main page never goes inside them. The<body>of each frame gets walked and watched on its own, and a frame that loads later is picked up by aloadlistener ondocument, in the capture phase.
What it costs, and what it doesn't do
Nothing to install, no build, no back end: the dictionary comes with the page, and the choice is kept in localStorage. On a phone the app keeps English for now, because Vietnamese labels run longer and our phone layouts are tight.
It doesn't replace real i18n. It can't handle plurals properly, or a line whose word order changes around a variable, beyond the patterns we write by hand. But for adding a 2nd language to an interface that was never built for one, with a promise never to touch the learning content, 100 lines did it.
Try it at https://arlts.org: on a computer, the flag by the logo is the toggle. Free, non-profit, no account needed.
Top comments (0)