Bash.tv → GitHub → itch.io

Localization Manager

Shows the game’s text in several languages, with fill-in values, correct plurals for each language, number formats, right-to-left languages and a list of missing translations.

Files: localization-manager.js (the component, no dependencies) and localization-manager.test.mjs (5 tests). Copy localization-manager.js into your game, for example to src/components/, and import it as an ES module.

Usage

import { createI18n } from "./localization-manager.js";
const i18n = createI18n({
  fallback: "en",
  locales: {
    en: { menu: { start: "Start" }, coins: { one: "{count} coin", other: "{count} coins" }, hello: "Hi, {name}!" },
    es: { menu: { start: "Empezar" }, coins: { one: "{count} moneda", other: "{count} monedas" } },
  },
});
i18n.setLocale(i18n.detect());            // the player's browser language if you have it
i18n.t("menu.start");                     // "Empezar"
i18n.t("coins", { count: 3 });            // "3 monedas"
i18n.t("hello", { name: "Sam" });         // falls back to English: "Hi, Sam!"

API

Engines

Engine Notes
Vanilla, Phaser, PixiJS, Three.js yes. In canvas games, call t() when drawing text; in HTML menus use data-i18n and apply().

Tests

Node tests: nested keys, fill-ins, plurals for English, Russian and Arabic, fallback, missing list, closest match, detection, direction, merging, loading, apply().

Run them with node --test toolkit/components/localization-manager/ (Node 20 or newer, nothing to install).