Multiple languages and translated interfaces for browser games. A tested i18n module with string tables, {name} placeholders, plurals for every language through Intl.PluralRules (Polish, Arabic, Russian forms), fallback to the base language, picking the player's browser language, number and date formats, missing-key reports, right-to-left detection, and canvas text that shrinks and wraps to fit...
Download skill (.zip) Files on GitHubGame localization
Plan for it early: no text in code or images, only keys. Adding a language later is then just a new file.
assets/i18n.js
import { createI18n, fitText, checkTables } from "./i18n.js";
import en from "./lang/en.js"; import es from "./lang/es.js";
const i18n = createI18n({ base: "en", tables: { en, es }, lang: settings.get("language"), onMissing: (key, lang) => console.warn("missing", lang, key) });
const t = i18n.t;
t("menu.play"); t("hud.coins", { count: 3 }); t("greet", { name }); i18n.number(12345); i18n.setLang("es")
// en.js: export default { "language.name": "English", menu: { play: "Play" }, hud: { coins: { one: "{count} coin", other: "{count} coins" } }, greet: "Hi {name}!" }
lang: "auto"picks the best match from the browser’s languages (es-MX → es), else the base.- Plurals: give the forms the language uses (
zerooptional; Polish needs one/few/many/other, Arabic six).checkTableslists the missing ones. - Numbers in placeholders are formatted for the language (
15.000in Spanish). - A missing key falls back to English and is reported once; an unknown key shows the key itself so it’s easy to spot.
- The language menu shows each language in its own name (
"language.name": "Español").
Text that fits
German and Finnish run ~30% longer than English, Chinese and Japanese shorter but taller. On canvas:
const f = fitText(ctx, t("menu.settings"), 360, { size: 24, minSize: 14, maxLines: 2 }); ctx.font = f.font; f.lines.forEach(...)
It shrinks the font, then wraps (per word, or per character for Chinese and Japanese). Design buttons with room to grow; in HTML, let buttons grow and test with the longest language.
Check tables
const report = checkTables({ en, es, de }); // { es: { missing, extra, placeholders, plurals }, ... }
Run it in a test (node) before every release. A placeholder mismatch ({name} dropped) is a bug.
More rules
- Fonts: the game font may lack accents, Cyrillic or CJK. Fall back to
system-ui, sans-serifor load a font that covers the languages (Noto). Pixel fonts often need a second font for other scripts. - Right-to-left (Arabic, Hebrew):
i18n.rtl; mirror HTML layouts (dir="rtl", set bysetLang); canvas HUDs usually stay as is. - Whole sentences, never glued pieces (“You found “ + item): word order differs. Use placeholders.
- Keys describe meaning (
gameover.retry), and add notes for translators where context is unclear. - Machine translation is a start; ask a native speaker to play it.
- Store the language in settings (game-settings-system).
Tested
Node tests: browser language pick, Spanish plurals and number format, placeholders, fallback with a report, unknown keys, Polish plural forms, unsupported language, RTL, language names, text fitting and per-character wrapping, the table checker. Used in the UI kit demo (English and Spanish, switching at runtime).
Pitfalls
Text in images, string concatenation, a font without accents, buttons sized for English, forgetting plural forms, and dates or numbers formatted by hand.