--:--
notes/commonplace/i18n-and-translation.mdx

NOTES / Commonplace ·

Picking a language and translating a site

i18n (internationalization: 18 letters between i and n) is making a site able to speak several languages. l10n (localization) is the work of actually adding one.

Rule one: no text in components

Every visible string lives in a table, keyed by an id:

// strings/en.ts (the source language)
export const en = {
  'notes.title': 'Notes',
  'notes.count': '{count} notes',
};
// strings/zh.ts
export const zh = { 'notes.title': '笔记', 'notes.count': '{count} 篇笔记' };

Components ask for t('notes.title'). Then:

  • Adding a language means adding a table, not touching components.
  • Placeholders ({count}) let each language put the variable where its grammar wants it. Never build sentences by concatenating fragments.
  • Markup inside a sentence uses tags in the string ('Read the <link>notes</link>') that the component maps to elements, so translators can move the link too.
  • Shared UI libraries take their words from the app (a provider or props) instead of carrying their own language.

One registry of languages

Keep everything about a language in one table, and have every part of the site read from it:

export const LANGUAGES = {
  en: { name: 'English', tag: 'en', azure: 'en', libre: 'en', countries: [] },
  'zh-Hant': {
    name: '繁體中文',
    tag: 'zh-TW',
    azure: 'zh-Hant',
    libre: 'zt',
    countries: ['TW', 'HK', 'MO'],
  },
  // ...
} as const;
export type Locale = keyof typeof LANGUAGES;
  • The name in its own language goes in the picker: a Japanese reader looks for 日本語, not "Japanese".
  • Store each service's code separately. The same language is zh-TW in HTML, zh-Hant at Azure and zt at LibreTranslate.
  • Country lists and Accept-Language aliases live in the same entry, so the language detection is generated from the table.
  • Adding a language is one entry; hand-written string tables are optional per language, and machine translation fills the rest.

Choosing the language

Ask in order, first answer wins:

  1. An explicit choice in the URL (?lang=zh): shareable and testable.
  2. A cookie remembering what the visitor picked with the switch.
  3. The visitor's country, from a CDN header (CF-IPCountry) or an IP lookup. Cache lookups per IP, keep the timeout short, and remember that sending IPs to a lookup service is sharing visitor data.
  4. The browser's Accept-Language header.
  5. The default.

Country is a guess, not a language: people travel and use VPNs. Always offer a visible switch, and let it win.

Set <html lang="..."> to the chosen language, for screen readers, fonts and search engines.

Machine translation

For long content you won't translate by hand, translate on the server and cache:

  • Cache key = target language + a hash of the source text. Unchanged text is never translated twice, and an edit re-translates only what changed.
  • Translate the rendered HTML in HTML mode, so tags survive. Mark things that must not change (<span translate="no">, placeholders, code).
  • If the service is down or unconfigured, fall back to the source language instead of failing the page.
  • Free options: Azure AI Translator F0 (2 million characters a month) or a self-hosted LibreTranslate. Both are statistical/neural MT, not LLMs.

Hand-written translations stay better for short UI text; use machine translation to fill gaps and for long content.

Placeholders and tests that pin the words

  • Put variable words in placeholders, not in shared strings. On /siam each animal judge refers to itself differently in Chinese (本喵 for the cat, 本汪 for the dog, the formal 本庭 for the bear). The first version wrote 本喵 into the shared verdict text, so the bear's verdict said 本喵 too. The fix: shared strings take {self} and each judge supplies its own word.
  • Lock decisions about wording with a test. When a phrase must never appear in one language (the Chinese /siam page says nothing about on-chain notarization), a test that scans every string in that language for the forbidden words stops a later edit from bringing it back. Other tests check that each judge's verdict renders its own word.
  • Keep a feature switch out of the copy. Where behaviour is still undecided (does a jury reverse a ruling or only flag it?), one constant picks the behaviour and the strings for both outcomes live side by side, so flipping it needs no copy edits.

Related: the day it was built, Afternoon on the live site; eight languages from one registry, Evening: MySQL for visits, a pixel world map, eight languages. Placeholders and wording tests: Couple Court at /siam.