Skip to main content

Locales & the translate pre-commit hook

src/locales/{en,zh_CN}.json, the auto-translate pre-commit hook, and the Chinese terminology conventions AdminJS labels follow.

Deep-dive doc split out of .claude/rules/architecture.md (which is now an index). Append new findings about this area here, not to the index.

The translate pre-commit hook writes locale entries for you — review them​

Committing a Prisma schema change that adds columns triggers a translate step in the pre-commit hook. It generates an entry for every new field name in both src/locales/en.json and src/locales/zh_CN.json, machine-translating the Chinese via gpt-4o-mini, and stages the result into your commit. A commit you expected to touch 13 files lands as 15.

This is convenient but it means machine-translated, user-visible Chinese labels enter the product without anyone writing them. Always read the locale diff after committing a schema change (git show --stat HEAD to spot it, then git show HEAD -- src/locales/). Observed failure modes, all from INFRA-638's four new keys:

  • Wrong domain term. baseSize → 基础尺寸. In apparel, 尺码 is the garment size; 尺寸 is a physical dimension. See the convention below.
  • Invented vocabulary that ignores the feature's existing terms. lengthVariant → 长度变体, while the sibling ticket in the same epic (INFRA-637) already shipped 可变长度 for the identical concept. Two Chinese names for one feature across two screens. Check what the rest of the epic already uses before accepting the hook's guess.
  • Keys for fields that never render (see reverse relations below).

The hook skips cleanly when no matching files are staged (translate (skip) no matching staged files), so a locale-only follow-up commit does not re-trigger it.

Keys live under properties, not labels​

Prisma field labels land in the properties block. There is a separate labels block with its own overlapping-looking keys — labels.Size is 尺寸 while properties.size is 尺码. When checking a convention or fixing a term, confirm which block you are in; grepping the raw JSON by line number does not tell you.

Top-level blocks: labels, properties, components, resources, buttons, actions, messages, time, pages.

Chinese terminology: apparel size is 尺码​

Under properties, the size family is consistently 尺码, and ID fields take a bare ID suffix:

KeyChinese
size尺码
sizeCode尺码号
sizeRange尺码范围
sizeRangeId尺码范围ID
sizeRangeToSize尺码范围至尺码
baseSize / baseSizeId基础尺码 / 基础尺码ID

Counts under properties: 10 uses of 尺码 vs 1 of 尺寸. The lone outlier is sizeId: 尺寸ID, which contradicts its own sibling sizeRangeId: 尺码范围ID and looks like the same mistake made earlier and never caught — a candidate cleanup, untouched so far because it is user-visible and out of scope for the tickets that noticed it.

Reverse array relations get keys that never render​

Adding a self-relation or any bidirectional relation produces keys for both sides, e.g. baseSize (to-one, FK-owning) and lengthVariants (Size[], reverse). Only the FK-owning side is ever looked up: @adminjs/prisma's Resource.prepareProperties() skips relation fields where the model doesn't own the FK (field.relationName && !field.relationFromFields?.length), so no label is requested for the array side. Those entries are inert.

The file already carries such keys (sizeRangeToSize is the same kind of reverse relation and has one), so leaving them is consistent with precedent rather than a bug — but don't spend effort translating them well, and don't assume a key's existence means a UI surface exists.

A reverse key therefore often duplicates its FK-side sibling's translation, and that is expected. lengthVariant and lengthVariants are both 可变长度 in zh_CN.json, because Chinese does not inflect for plural and only the singular ever renders. A locale-coverage audit will see this as a silent duplicate; it is inert, not a missed translation. Invent a distinct string only if the reverse key ever becomes user-visible — which, per the prepareProperties() behaviour above, would itself be the surprising part worth investigating first.

Pre-existing en/zh key-parity gaps​

A parity check across both files is not clean at baseline, so don't read a mismatch as something your change caused. As of 2026-08 the standing gaps are:

  • EN-only: pages.dashboard.quarterSuffix
  • CN-only: actions.exportMeasurements, actions.uploadMeasurements

Verify against git show HEAD:src/locales/... before attributing a gap to your own diff.

Login page welcome text: component override + components.Login keys (INFRA-790)​

The AdminJS login page renders translateComponent('Login.welcomeHeader') / translateComponent('Login.welcomeMessage') inside the built-in Login component. INFRA-790 replaced the AdminJS welcome text by overriding the whole Login component rather than only adding locale keys:

  • src/config/componentLoader.ts registers Login: componentLoader.override('Login', `${componentsPath}/common/Login/Login`) — override, not add, because Login is in ComponentLoader.defaultComponents (add throws for default names). The built-in Login is wrapped in allowOverride(Login, 'Login'), so the registered component replaces it on the login page (the login template loads components.bundle.js, which registers all overrides).
  • componentLoader.ts builds its loader through makeComponentLoaderWithSelectiveLoad (dev-only DEV_SELECTIVE_LOAD_COMPS swaps heavy pages for the HelloWorld placeholder) and exports the original ComponentLoader as componentLoader. The placeholder is pre-registered under the same name/path as the real HelloWorld entry; keep those two paths identical — ComponentLoader.add throws already defined when a name is re-added with a different path.
  • src/components/common/Login/Login.tsx mirrors the built-in layout/form, removes the AdminJS illustrations, and renders the welcome text via translateComponent; the components.Login key in src/locales/{en,zh_CN}.json supplies the wording.
  • The left panel replaces the AdminJS illustrations with public/images/mes-icons.svg — a hand-drawn vector of the three MES icons (hexagons only, no text, transparent background). The MANUFACTURING / EXECUTION / SYSTEM captions are real text from components.Login.label{Manufacturing,Execution,System} (uppercased via CSS so zh stays natural), so they stay sharp and translate. The icons sit directly on the mocha panel (no card) with opacity: 0.75 on the <img> only — the captions use a solid light tint (#C9D6F2, ~6.9:1 contrast on #4F3E3A). Don't put the opacity on the shared wrapper: blue #5978BB captions at 75% drop to ~1.9:1 and become hard to read even in bold. Don't swap in a raster export with baked-in words: the original stock PNG (558px) rendered blurry and its captions couldn't be localized.
  • Branding rules: the welcome block is deliberately bilingual and identical in both locales: welcomeMessage is the Chinese line 欢迎使用 Birdy Grey 生产执行系统 and welcomeDescription the English description, shown together to every user (factory staff are Chinese-speaking, Birdy Grey office users English-speaking). Only the icon captions (label*) differ per locale. Form labels (Email/Password/Login) still come from AdminJS built-in translations — do not duplicate them in the project locale files.
  • The pre-commit translate hook does not machine-translate these keys when the same add diff exists in both en.json and zh_CN.json (the diff uuid is type#keyPath; the value is ignored, so the zh entry is treated as already translated).
  • Gotcha: the translate hook script imports server.ts, which connects to PostgreSQL at module load outside NODE_ENV=test. Commits that stage src/locales/en.json therefore fail when the DB is unreachable — work around with LEFTHOOK_EXCLUDE=translate git commit ... (the hook is a no-op for identical en/zh key diffs anyway).

The component override is the extension point for future login work (error UX, remember-me, SSO): read { action, errorMessage } from window.__APP_STATE__ (as the built-in does) and rely on useTranslation / useSelector(branding). LoginApplication renders <Login /> with no props — an override that destructures action/errorMessage from props gets undefined for both: failed logins (errorMessage: 'invalidCredentials') silently show no error, and the form only still works because an action-less form posts back to its own URL. INFRA-790's first version had this bug (caught in review by a manual wrong-password test, before merge); the unit tests had passed because they passed the values as props.