mirror of
https://github.com/nexu-io/openmotion.git
synced 2026-12-01 23:51:42 +08:00
* perf(studio): lazy-load i18n dictionaries by locale and add governance checks * fix(studio): emit numeric delta hashes for i18n keys to avoid gitleaks false positives --------- Co-authored-by: 罗高 <luogao@users.noreply.github.com>
5.9 KiB
5.9 KiB
Studio Internationalization (i18n) Architecture & Developer Guide
This guide defines how OpenMotion Studio authors, validates, and lazy-loads UI translations across all eleven supported languages. Follow these rules whenever adding or editing user-facing copy.
Architecture: source by module, build by locale
OpenMotion Studio supports 11 locales: en (canonical source keys), zh-CN, ja, ko, de, fr, ru, es, pt-BR, it, and tr.
- Authoring layout (by feature module): Translations live under
studio/src/i18n/as feature-scoped*-ui.jsonfiles (plus base<locale>.jsondictionaries,skill-library/<locale>.json, andshared/asset-name-ui.json). Keeping source files modular avoids merge conflicts across parallel PRs. - Build & runtime layout (by locale):
studio/vite-i18n-plugin.tsreads the canonical manifest instudio/src/i18n/manifest.tsand transposes all feature files at build time into 10 independent lazy locale chunks (locale-zh-CN-*.js,locale-ja-*.js, ...,locale-tr-*.js):- English (
en) requires zero translation chunks because English copy is the source key itself (only a lightweight 16-entrysingularplural map stays in the main shell). - Non-English sessions dynamically
import()only the 1 active locale chunk during startup instudio/src/main.tsxwhile the native HTML splash screen remains visible, avoiding both unused-locale download overhead (~88%–91% savings) and English-to-target text flashes. - Switching languages via
useI18n().setLocale(next)asynchronously fetches the target locale chunk while keeping the current locale rendered until the new chunk resolves, then updates copy in place without unmounting editor DOM or state.
- English (
Key files
studio/src/i18n/manifest.ts— Single source of truth for supported locales, extension JSON override order (I18N_EXTENSION_SOURCES), legacy Chinese backend reverse-lookup metadata, andhashMessageKey.studio/vite-i18n-plugin.ts— Vite/Vitest plugin emitting virtual modulesvirtual:openmotion-i18n/locale/<locale>andvirtual:openmotion-i18n/meta(plus HMR invalidation when editing JSON files in dev mode).studio/src/i18n/messages.ts— Runtime translation API (translate,sourceMessage,isKnownMessage,ensureLocaleLoaded,isLocaleLoaded) with zero static imports of locale JSON files.studio/src/i18n/I18n.tsx— React context (I18nProvider,useI18n,readLocale,LOCALE_STORAGE_KEY) and<title>/<meta>synchronization.studio/src/i18n/sourceCatalog.ts— Test-only static dictionary loader for integrity tests; never imported by production code.scripts/check-i18n.ts— Automated manifest, completeness, placeholder parity, and cross-file conflict gate invoked bynpm run check.
Rules for adding or modifying UI copy
- Always use English source strings with
useI18n(). Callt('Create project')ort('Files waiting for import: {count}', { count })instead of hardcoding localized text in.tsxfiles. - Choose the right module JSON file. Add new keys to the existing feature-domain
studio/src/i18n/*-ui.jsonfile (for examplesettings-ui.json,design-ui.json,scenario-ui.json,studio-navigation-ui.json). Only create a new<feature>-ui.jsonfile when introducing a distinct domain, and register it inI18N_EXTENSION_SOURCESinsidestudio/src/i18n/manifest.ts. - Provide all 10 non-English locales for every new file and key. Every new
*-ui.jsonmodule must define identical key sets across all 10 non-English locales (zh-CN,ja,ko,de,fr,ru,es,pt-BR,it,tr) with non-empty trimmed values. - Preserve
{var}placeholders and technical identifiers. Keep{count},{value0},{message}, and tool identifiers such asopenmotion_*exact in every locale. Use plural objects ({ "one": "...", "few": "...", "many": "...", "other": "..." }) when a language needs CLDR plural forms, and add English singular overrides tosingularinstudio/src/i18n/messages.tswhen{count}copy needs a singular form. - Avoid cross-file key conflicts. Before adding a common UI label (such as a button or status label), check whether the English key already exists in another
*-ui.jsonor base<locale>.json. Reuse existing keys rather than redefining the same key with conflicting translations across files. - Never import Studio locale JSON files statically in runtime code. Production modules must read translations through
useI18n()ortranslate()fromstudio/src/i18n/I18n/messages.ts— never directly importstudio/src/i18n/*.json,skill-library/*, orsourceCatalog.ts.studio/src/shellBoundary.test.tsblocks static locale leaks intostudio/src/main.tsx. - Do not add new
locale === 'zh-CN'UI copy branches. UI labels and feedback belong instudio/src/i18n/. Checkinglocaledirectly is reserved for genuinely regional/structural behavior (such as community link ordering between Feishu and Discord, regional pricing URLs, CJK speaking-duration estimation, or passing a locale query to an external endpoint). - Legacy Chinese backend messages. New backend errors and warnings should emit canonical English keys. If a retained backend/catalog surface still emits a legacy Chinese string that must translate in non-Chinese sessions before
zh-CNloads, ensure its source key is covered byLEGACY_ZH_REVERSE_FILESorLEGACY_ZH_EXTRA_SOURCE_KEYSinstudio/src/i18n/manifest.ts.
Verification checklist
When adding or changing Studio translations or i18n plumbing:
- Run
npm run check(executesscripts/check-i18n.tsalongside repo and design checks). - Run
npm run typecheck:studioandnpx vitest run --config studio/vitest.config.ts studio/src/i18n/ studio/src/shellBoundary.test.ts. - Run
npm run build:studioand verifystudio/build/editor-assets/emits 10 separatelocale-*-*.jschunks without pulling locale JSON back intoindex-*.js.