Skip to content

ADR-0010: UI i18n with a typed in-house t() and flat JSON catalogues

Status: Accepted, 2026-09-24. Decision owners: Owner. Scope: frontend/src/lib/i18n.ts, frontend/src/locales/{pl,en}.json, every component, view and lib/ module with user-visible text, frontend/src/lib/format.ts (dates and numbers), frontend/index.html (pre-paint lang), frontend/src/api/_core.ts (Accept-Language).

Context

ADR-0009 made the UI bilingual (Polish and English) with a catalogue that keeps both locales in sync. The UI had about 800 hard-coded Polish strings in some 70 files, pl-PL hard-coded in 13 date and number formatters, and lang="pl" forced on rendered document content. API error codes were already mapped to Polish copy in one module (lib/errorMessages.ts).

Problem

We need a way to look strings up that:

  • adds little to the bundle (the budget in scripts/check-bundle.mjs is 150 JS files and 8 MiB, and the build was at 145 files);
  • catches a mistyped or missing key at compile time;
  • handles Polish plurals (one, few, many: "1 plik", "2 pliki", "5 plików");
  • works outside React too (error mapping, toasts from hooks, formatters).

Options considered:

  1. i18next + react-i18next. The de facto standard: namespaces, lazy backends, plugins. About 18 KB min+gz for the two packages. Plurals via Intl.PluralRules with _one/_few/_many key suffixes. Typed keys need a module augmentation of its resource types, which works but slows the type checker on large catalogues. Most of the feature set (backends, language detector plugins, context, nesting) is unused here. About 1 day.
  2. FormatJS (react-intl). Full ICU MessageFormat, strong date and number helpers. About 16 KB min+gz, more unless messages are precompiled, which needs a build step. Keys are not typed without extra tooling. ICU syntax is more than two locales with simple placeholders need. About 1.5 days.
  3. Lingui. Small runtime (about 3 KB), ICU, message extraction from source. Relies on a Babel or SWC macro; our Vite 8 build runs on Rolldown and Oxc, so the macro path adds a transform we do not have today and a compile step for catalogues. About 2 days including tooling.
  4. In-house typed t() over JSON catalogues. About 1 KB. Keys are typed from pl.json with no generator: t("login.titel") fails to compile for a key that does not exist. Plurals use the platform's Intl.PluralRules, so the CLDR rules for Polish come for free. Only {name} interpolation, no ICU. A test keeps the two catalogues in step. About half a day for the module, the rest is the string migration that every option needs.

Decision

Option 4.

  • Catalogues: src/locales/pl.json and src/locales/en.json, flat dotted keys grouped by screen or component (login.title, shareDialog.expires), with common.* for words used in many places and error.<code> for API error codes. Flat keys keep the TypeScript type trivial (keyof typeof pl), make grep find a key's use sites, and sort into a stable diff. Keys are kept sorted.
  • Lookup: t(key, vars?) returns the string with {name} replaced. tn(key, count, vars?) picks a plural form: a plural entry is an object with CLDR category forms (one, few, many, other for Polish; one, other for English), and {count} is the locale-formatted number. hasMessage(key) narrows a key built at runtime, for error.<code>.
  • Formatting: formatNumber and the date helpers in lib/format.ts use Intl with intlLocale(), the browser's regional variant of the active language (en-GB, pl-PL) or the bare language code. Formatters are cached per locale.
  • Locale choice: a saved choice (localStorage["ics-locale"], wrapped in try/catch) wins, otherwise the first of navigator.languages that is pl or en, otherwise English. A switcher sits next to the theme toggle in the chrome bar. <html lang> follows the locale, set before paint by the inline script in index.html and after that by setLocale().
  • Re-rendering: the locale is module state, so t() works anywhere. <LocaleRoot> in main.tsx remounts the tree when the locale changes, which also refreshes memoised list rows. The cost is that unsaved local state (an open dialog, a draft) is lost on a switch; switching is rare and deliberate, so this is accepted instead of threading a context through every memoised component.
  • Document content: rendered Markdown and HTML bundles keep their own language. The UI no longer forces lang="pl" on them; assets carry no language metadata today, so the attribute is omitted and the content inherits nothing from the UI locale by intent.
  • API: every request sends Accept-Language with the UI locale, so the backend can localise e-mails.
  • Bundling: both catalogues are imported statically and end up in a shared chunk loaded at startup (next to api/_core, since eager and lazy views both use them). With about 850 keys they are about 110 KB raw and about 33 KB gzipped; English alone is about 15 KB gzipped. Lazy-loading English would add a JS file (the build is at 146 of 150) and a loading state before the first paint, for a saving smaller than one icon set.

Reactivation trigger

  • A third locale, or catalogues above about 50 KB gzipped: move non-default locales to lazy chunks.
  • Copy that needs ICU features beyond plurals (select on gender, nested plurals, rich text with markup in the middle of a sentence): re-evaluate Lingui or FormatJS.
  • Translators working outside the repository: consider a format with extraction tooling and context comments.

Consequences

  • Every new UI string needs a key in both locale files; the parity test (src/lib/i18n.test.tsx) fails otherwise, and tsc rejects a key that is missing from pl.json.
  • Strings computed at module load time would not follow a locale switch: labels live in render code or in functions, never in module constants.
  • Tests pin the locale to English in src/test-setup.ts and assert on English copy; tests that cover Polish switch with setLocale("pl").
  • e2e specs select by English labels and run with locale: "en-US" in playwright.config.ts.
  • Byte sizes use the locale's decimal separator ("2,0 KB" in Polish).
  • Doc type labels (doc_types.label_pl, label_en) are data stored in the database, not UI text, so they are not in these catalogues: admins add types at runtime, while catalogues change only with a deployment. See ADR-0015.

References

  • ADR-0009: English first, UI translated through i18n.
  • frontend/src/lib/i18n.ts, frontend/src/lib/format.ts, frontend/src/components/LanguageSwitcher.tsx.