# Internationalization

English, German, French and Brazilian Portuguese UI with request-scoped SSR translation.

## Catalog ownership [#catalog-ownership]

Web catalogs live in `apps/web/src/locales/{en,de,fr,pt-BR}.ts`. `MessageKey` derives from English; the other catalogs must retain key and interpolation parity. Shared error translations come from `packages/errors/src/locales` and join the i18next resources in `apps/web/src/platform/i18n/catalogs.server.ts` (mobile: `apps/mobile/src/platform/i18n/catalog.ts`). Locale files are static objects, with no spreads, so [x18n](https://x18n.dev) can read and edit them. Mobile maintains its own app catalogs with the same supported languages. Errors (`packages/errors/src/locales`) and account emails (`packages/email/src/locales`) have one file per language too; a regional language's file is named after its tag (`pt-BR.ts`) and exported under a camel-case name (`ptBR`).

Use `useTranslation` in components and hooks. Format dates and numbers with Intl. Do not concatenate translated fragments when the sentence needs different word order.

i18next checks interpolation variables in the types: `t("KEY")` does not compile when the message has a `{{placeholder}}`, so pass the values. Type a config object whose keys reach `t(key)` without options with `MessageKey` (`apps/web/src/platform/i18n/i18n-keys.ts`), the keys whose message takes no variables; `AnyMessageKey` is every key, for code that passes the values itself.

## Public URLs per language [#public-urls-per-language]

A public page exists once per language, at one path each: `/privacy` is the default language, `/de/privacy`, `/fr/privacy` and `/br/privacy` the others. The configuration is one leaf, `packages/intl/src/locale.config.ts`:

```ts
export const languages = ["en", "de", "fr", "pt-BR"] as const;
export const fallbackLanguage: Language = "en"; // served unprefixed
export const localeSlugs = { en: "en", de: "de", fr: "fr", "pt-BR": "br" } as const; // the path prefix
export const localizedSegments = {}; // canonical segment → translated segment, per language
export const localizedPublicPaths = [
  "/",
  "/privacy",
  "/terms",
  "/architecture",
  "/index.md",
  "/privacy.md",
  "/terms.md",
  "/posts/*",
  "/u/*",
] as const;
export const rootLocaleRedirect = true; // first human visit to / follows cookie, then Accept-Language
```

Brazilian Portuguese shows how a regional language works: `"pt-BR"` is in `languages` and `"pt-BR": "br"` in `localeSlugs`, so its pages answer at `/br/privacy`, `?lang=pt-BR`, `?lang=pt_br` and `?lang=pt` redirect there, `/pt-BR/privacy` redirects to `/br/privacy`, and `Accept-Language: pt-BR` sends a first visit from `/` to `/br`. A language family matches its one language: `pt-PT` and `pt` also resolve to `pt-BR`. A Brazilian project that wants Portuguese as its default sets `fallbackLanguage = "pt-BR"`; the template keeps English. A project that does not need a language removes it from `languages`, `localeSlugs` and the maps that the type checker then flags, and deletes its catalog files. Translated segments go through the same leaf: `localizedSegments = { de: { privacy: "datenschutz" } }` makes `/de/datenschutz` the German page while the route file stays `routes/{-$locale}/privacy.tsx`; `localizePath("/privacy", "de")` then returns the translated form for links, hreflang and the sitemap, and the router's `rewrite` pair (`router.tsx`, wired only when a segment is translated) maps between the public URL and the route tree. The template ships the identity mapping.

The pure functions live in `packages/intl/src/locale-paths.ts`: `splitLocalePath` (pathname → language and canonical page), `localizePath` (page and language → public path), `canonicalLocalePath` (the one form to redirect to), `queryLanguage` and `negotiateLanguage` (a `?lang=` tag or an `Accept-Language` header → one of ours, region tags included). The redirects themselves and the crawler check are in `apps/web/src/platform/seo/locale-redirects.ts` ([SEO and discovery](/docs/features/seo) lists them).

## Server rendering [#server-rendering]

`getSSRLanguage` reads the path on a public page (`/de/privacy` is German, `/privacy` is the default language, whatever the cookie says) and the locale cookie everywhere else, then the fallback. `getRequestI18n` clones the i18next instance for that request while sharing loaded catalogs. Mutating one global language during SSR would make concurrent requests affect each other.

The server holds every catalog (`apps/web/src/platform/i18n/catalogs.server.ts`, imported by `server.ts`); the client entry ships none. The browser boots in the language the server rendered: the root route preloads `/locales/{$lang}.json?v=<version>`, a content-hashed, immutable asset, and `client.tsx` loads it before hydrating (`platform/i18n/boot-catalog.ts`), so the first client render and SSR share one catalog and a repeat visit pays nothing for it. The client keeps its shared instance and updates the document language after a switch. On a public page the language switcher is a list of links to the current page in each language, a full navigation that sets the locale cookie; under the app shell, which has one URL, `switchLanguage` fetches the missing catalog once and switches in place. Developer articles are English only: their article region has `lang="en"`, their chrome is translated, and their sitemap entries have no hreflang alternates.

## Extend [#extend]

Add a message to English, German, French and Brazilian Portuguese together. Preserve placeholder names and run the locale parity tests and `pnpm i18n:check` (x18n; config in `apps/{web,mobile}/x18n.config.yml`). `pnpm i18n:ui` opens an editor across locales. A new language also needs `locale.config.ts` (the language and its slug), a catalog file in each of the four catalog folders and its entry in `catalogs.server.ts`, `apps/mobile/src/platform/i18n/catalog.ts`, `packages/errors/src/locales/index.ts` and `packages/email/src/copy.ts`, the `OG_LOCALE` map in `platform/seo/seo.ts` and the OG templates' copy, the switcher's label key (`LANGUAGE_*`), the mobile language picker (`LANGUAGE_PREFS` and its endonym), `PUSH_LOCALES` with its push text, the email month names in `packages/email/src/user-text.ts`, `x18n.config.yml` in both apps and the per-language strings in `scripts/remove-optional-catalog.mjs` and `scripts/remove-task-clients.mjs`. `Record<Language, …>` maps fail `check-types` until the language has copy.

The app on the phone picks the device's first preferred locale that names one of the languages, by full tag first (`apps/mobile/src/platform/i18n/device-language.ts`, through `matchLanguage`): a phone in `pt-BR` reports the language code `pt`, so matching the code alone would miss the regional language.

Brazilian Portuguese copy uses "você", Brazilian spelling and vocabulary (tela, arquivo, celular, baixar), and the domain glossary: organização, membro, tarefa, assinatura, cobrança, forma de pagamento, chave de acesso. Machine-assisted text needs a native review before a client ships it. Translate the domain consistently: a Todo's translated label can differ from its code identifier.

## Key files [#key-files]

- [`packages/intl/src/i18n.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/packages/intl/src/i18n.ts)
- [`packages/intl/src/server.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/packages/intl/src/server.ts)
- [`packages/intl/src/locale.config.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/packages/intl/src/locale.config.ts)
- [`packages/intl/src/locale-paths.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/packages/intl/src/locale-paths.ts)
- [`apps/web/src/platform/seo/locale-redirects.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/web/src/platform/seo/locale-redirects.ts)
- [`apps/web/src/platform/i18n/language-switcher.tsx`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/web/src/platform/i18n/language-switcher.tsx)
- [`apps/web/src/platform/i18n/intl.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/web/src/platform/i18n/intl.ts)
- [`apps/web/src/platform/i18n/catalogs.server.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/web/src/platform/i18n/catalogs.server.ts)
- [`apps/web/src/platform/i18n/boot-catalog.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/web/src/platform/i18n/boot-catalog.ts)
- [`apps/web/src/routes/locales/{$lang}[.]json.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/web/src/routes/locales/%7B$lang%7D%5B.%5Djson.ts)
- [`apps/web/src/locales/en.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/web/src/locales/en.ts)
- [`apps/mobile/src/platform/i18n/i18n.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/mobile/src/platform/i18n/i18n.ts)
- [`apps/mobile/src/platform/i18n/catalog.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/mobile/src/platform/i18n/catalog.ts)
- [`apps/mobile/src/platform/i18n/device-language.ts`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/mobile/src/platform/i18n/device-language.ts)
- [`apps/web/x18n.config.yml`](https://github.com/CarlosZiegler/template-cloudflare/blob/main/apps/web/x18n.config.yml)
