t3-code-android-nightly/docs/internals/14_i18n.md
Chenxin Yan b4b58d231c
chore(i18n): move to General Translation v12 and drop the four GT patches (#46460)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
2026-12-01 17:57:48 -08:00

7.8 KiB

Translating the UI

Web uses gt-react, mobile gt-react-native. There is no GT cloud: gt generate scans source for <T>, gt`…` and msg and writes committed catalogs keyed by a hash of the English message (apps/{web,mobile}/src/i18n/translations/*.json). Translations are written into those files by hand.

Which API

Where the text is Use
JSX children, including <code>, <strong>, links <T>…</T>, values in <Var>, plurals with <Plural n={n} one={…}>…</Plural> (the children are the fallback)
A prop or string inside a component (title, aria-label, tooltips) const gt = useGT(), then gt`Discard draft`
A constant table (settings index, menus, command registry) msg("Diagnostics") in the table; const m = useMessages() and m(entry.label) where it renders
A helper that builds a label and is called during render Take a resolve: MessageResolver = m parameter, return resolve(msg(…)); components pass useMessages()
Outside React: toasts, stores, announcements, alerts m(msg("An error occurred.")), m from ~/i18n/messages (mobile: src/i18n/messages)

m() resolves at call time with the resolver of the language on screen, which I18nProvider hands over in the commit that shows it, and returns English when no provider is mounted, so unit tests keep asserting English without setup. Never call m() during render: the React Compiler caches the result and a language switch would not update it. useGT() and useMessages() re-render on a switch.

GT's hooks need its provider. The app mounts it in I18nProvider; a web browser test gets it from render() in apps/web/test/render.tsx, in English unless the test passes { locale: "de" } to check a translation from the catalog.

Import T, msg, the hooks and the helpers (plural, select, part) straight from gt-react or gt-react-native. The CLI only finds strings whose functions are imported from a GT package; a re-export of a GT function through a local module hides every string behind it, and the next gt generate deletes their keys. Passing gt from useGT() to a helper as a plain argument is followed; putting it in an object is not, and the CLI warns. Mobile loads the Intl polyfills GT needs at the top of index.ts, so direct imports are safe.

Code in packages/client-runtime and packages/shared is scanned by both apps (src in each gt.config.json) and marks strings with msg() from gt-i18n. Its helpers take resolve: M = mFallback, and mFallback always returns English: apps pass useMessages() in render or m elsewhere, and key caches and effects on useLocale(), not on the resolver.

The server imports packages/shared and stays English, so a server-imported module keeps its English and its translatable copy lives in a *Copy sibling only clients import (usageLimits.ts and usageLimitsCopy.ts).

Writing strings

A message is a tagged template. Its text is literal: {, ' and # need no escaping.

  • Variables: gt`${{ shortcut }} to undo`, or ${{ name: value }} when the value is not a variable of that name. The English text must not change by one character: its hash is the key, and tests assert on it.
  • Plurals: gt`${plural({ count }, { one: part`${{ count }} thread`, other: part`${{ count }} threads` })}`. A branch with a value in it is a part`…`; a branch of plain text is a string. Use ${{ count }}, not ${n}: n formats 1234 as "1,234" and changes the English. When the singular has no number ("View browser"), use "=1": "…".
  • Choices: gt`Merge ${select({ term }, { mr: "merge request", cr: "change request", other: "pull request" })}`. TypeScript wants every value of a union covered, by its own branch or by other.
  • Context disambiguates short words that translate differently: gt({ context: "verb, opens a file" })`Open` vs gt({ context: "pull request state" })`Open`. Add it to any one- or two-word label whose part of speech is not obvious.
  • msg("Save") is a string. msg`…${{ name }}…` and msg({ context })`…` are objects (Msg) that carry their values and context, so a table of them is typed string | Msg, two built from the same text are not equal, so they cannot be compared or used as keys, and m() or mFallback() turns one into text. A message whose values arrive later is a function: (count: number) => msg`…`.
  • Never put a tagged template in a default parameter value ((title = gt`Saved`) => …): the web React Compiler skips the component. Read it into a const first.
  • Split nothing. One sentence is one message, with variables inside it. Never build a sentence from translated fragments. Never put tags in a template; markup goes in <T>.
  • The same English in gt`…` and in <T> is two keys. That is fine.

Build steps

Mobile compiles messages in apps/mobile/babel.config.js with two plugins from @generaltranslation/compiler/babel, which run before React Compiler. precomputeMessages hashes each template at build time, which Hermes is slow to do on a phone. lowerTaggedTemplates turns tagged templates into calls, because React Compiler's Babel plugin skips a component that holds a tagged template with a value. Metro caches each file's output, so start it with --clear after changing the GT version. Web runs the bundler plugin from @generaltranslation/compiler in apps/web/vite.config.ts, in production builds only: it hashes each <T> and template at build time and fails the build on a misuse gt generate would also reject. The dev server and the tests skip it, because it doubles dev start (generaltranslation/gt#3093) and the runtime computes the same hashes. Desktop runs no GT build step.

What stays English

Product and provider names (T3 Code, Codex, Claude, GitHub), model ids, code, commands, file paths, URLs, keybinding glyphs and key names, user content, agent prompts and anything sent to an agent, log and console output, and text authored by the server (errors, activity labels, provider status). Wrap the sentence around them and pass them as variables.

Dates and numbers

timestampFormat.ts formats times in the host's regional locale with the user's 12/24-hour setting; keep that. Words around them ("just now", "5m ago", "Expires in") are strings and follow the rules above.

Regenerating catalogs

Run vp run i18n:generate in apps/web and in apps/mobile after wrapping strings (about 20 s, offline). It adds new keys to every locale with the English message as the value and, once every source file has been read, drops keys no longer in source. A message with values is stored as a tree, e.g. ["Hallo ", ["name"], "!"]: translate the strings and keep the rest. A key still holding its English seed renders English but uses the target language's plural rules; a key missing from the locale file renders the source with English rules. Do not edit en.json by hand.

The desktop main process has its own catalogs in apps/desktop/src/i18n/translations, regenerated with vp run i18n:generate in apps/desktop. vp run i18n:check runs gt generate --check in all three apps.

t3code/no-untranslated-copy reports unwrapped copy as an error. vp run i18n:untranslated at the repo root counts what it flags, per directory. For English that stays English and the rule still flags, write // oxlint-disable-next-line t3code/no-untranslated-copy -- <why>.