Guide
Texts in two languages
How the dictionaries work, how to add a text in English and German, and what the tests check.
What it is about
Every word of the UI exists in English and German. English is the source and
the default. The rules are in spec/i18n.md; this page says how to work
with them.
The dictionaries
| File | What |
|---|---|
lib/i18n/en.ts | the English texts, grouped by area: shell, home, docs, signIn, firstAdmin, account, users, settings, status, roles, errors, locale, meta. Its type is Dictionary |
lib/i18n/de.ts | the German texts, typed against Dictionary |
lib/i18n/index.ts | LOCALES, DEFAULT_LOCALE, the cookie name locale, format() |
lib/i18n/server.ts | getLocale() and getDictionary() for server components and server actions |
lib/i18n/client.tsx | I18nProvider and useI18n() for client components |
lib/i18n/actions.ts | setLocale(), the server action of the switch |
Values are plain strings. A placeholder stands in braces and is filled with
format():
// lib/i18n/en.ts
account: { passwordHint: "At least {n} characters." }
// lib/i18n/de.ts
account: { passwordHint: "Mindestens {n} Zeichen." }
// a component
format(t.account.passwordHint, { n: LIMITS.password })
Add a text
-
Add the key to the fitting group in
lib/i18n/en.ts. -
Add the same key to
lib/i18n/de.ts, with the same placeholders. -
Use it:
// in a server component or a server action const { t } = await getDictionary(); // in a client component const { t } = useI18n(); -
Run
npm run check. -
Look through
e2e/fornot.toContainTexton the page: if the new English text contains a word a test excludes, the browser test fails.
A text that both languages write the same (such as "Admin") still gets its key in both.
What the tests check
tests/i18n.test.ts:
| Check | What fails |
|---|---|
| English is the default, German the second language | a changed LOCALES or DEFAULT_LOCALE |
| German has exactly the keys of English | a key in one dictionary only |
| every text is a non-empty string with the same placeholders | an empty text, a missing or additional {…} |
| outside a request the default applies | a getLocale() that throws in tests |
e2e/i18n.spec.ts checks in the browser: the default is English, the switch
shows German texts and holds across pages and reloads, it works without
JavaScript, the guest and admin pages pass axe in German, messages of server
actions follow the language.
What is not translated
The content of bundles, this documentation (only its frame), messages of JSON
routes and logs, codes and names of variables. Messages of server actions
are translated: they come from getDictionary() in the action. Messages of
Better Auth are not shown as they are; the user management shows "That did
not work." in the language of the UI.
Pitfalls
getLocale()and the build. It catches the error ofcookies()outside a request, but callsunstable_rethrowfirst. Remove that and the pages under/docsfreeze as static English pages at build time.- The switch re-renders. Setting a cookie in a server action renders the current page again; that is why the switch needs no redirect. Do not add one.
- The language is in the cookie only. No
Accept-Language, no prefix in the address. A test chooses German by setting the cookielocaleor by clicking "DE".
Next: Tests.