HenniOKF
Documentation

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

FileWhat
lib/i18n/en.tsthe 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.tsthe German texts, typed against Dictionary
lib/i18n/index.tsLOCALES, DEFAULT_LOCALE, the cookie name locale, format()
lib/i18n/server.tsgetLocale() and getDictionary() for server components and server actions
lib/i18n/client.tsxI18nProvider and useI18n() for client components
lib/i18n/actions.tssetLocale(), 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

  1. Add the key to the fitting group in lib/i18n/en.ts.

  2. Add the same key to lib/i18n/de.ts, with the same placeholders.

  3. Use it:

    // in a server component or a server action
    const { t } = await getDictionary();
    // in a client component
    const { t } = useI18n();
    
  4. Run npm run check.

  5. Look through e2e/ for not.toContainText on 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:

CheckWhat fails
English is the default, German the second languagea changed LOCALES or DEFAULT_LOCALE
German has exactly the keys of Englisha key in one dictionary only
every text is a non-empty string with the same placeholdersan empty text, a missing or additional {…}
outside a request the default appliesa 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 of cookies() outside a request, but calls unstable_rethrow first. Remove that and the pages under /docs freeze 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 cookie locale or by clicking "DE".

Next: Tests.