Files
openmotion/docs/i18n.md
open-design-crew[bot] 0bbac58419 perf(studio): lazy-load i18n dictionaries by locale and add governance checks (#820)
* 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>
2026-11-22 09:59:39 +08:00

5.9 KiB
Raw Permalink Blame History

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.json files (plus base <locale>.json dictionaries, skill-library/<locale>.json, and shared/asset-name-ui.json). Keeping source files modular avoids merge conflicts across parallel PRs.
  • Build & runtime layout (by locale): studio/vite-i18n-plugin.ts reads the canonical manifest in studio/src/i18n/manifest.ts and 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-entry singular plural map stays in the main shell).
    • Non-English sessions dynamically import() only the 1 active locale chunk during startup in studio/src/main.tsx while 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.

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, and hashMessageKey.
  • studio/vite-i18n-plugin.ts — Vite/Vitest plugin emitting virtual modules virtual:openmotion-i18n/locale/<locale> and virtual: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 by npm run check.

Rules for adding or modifying UI copy

  1. Always use English source strings with useI18n(). Call t('Create project') or t('Files waiting for import: {count}', { count }) instead of hardcoding localized text in .tsx files.
  2. Choose the right module JSON file. Add new keys to the existing feature-domain studio/src/i18n/*-ui.json file (for example settings-ui.json, design-ui.json, scenario-ui.json, studio-navigation-ui.json). Only create a new <feature>-ui.json file when introducing a distinct domain, and register it in I18N_EXTENSION_SOURCES inside studio/src/i18n/manifest.ts.
  3. Provide all 10 non-English locales for every new file and key. Every new *-ui.json module 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.
  4. Preserve {var} placeholders and technical identifiers. Keep {count}, {value0}, {message}, and tool identifiers such as openmotion_* exact in every locale. Use plural objects ({ "one": "...", "few": "...", "many": "...", "other": "..." }) when a language needs CLDR plural forms, and add English singular overrides to singular in studio/src/i18n/messages.ts when {count} copy needs a singular form.
  5. 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.json or base <locale>.json. Reuse existing keys rather than redefining the same key with conflicting translations across files.
  6. Never import Studio locale JSON files statically in runtime code. Production modules must read translations through useI18n() or translate() from studio/src/i18n/I18n / messages.ts — never directly import studio/src/i18n/*.json, skill-library/*, or sourceCatalog.ts. studio/src/shellBoundary.test.ts blocks static locale leaks into studio/src/main.tsx.
  7. Do not add new locale === 'zh-CN' UI copy branches. UI labels and feedback belong in studio/src/i18n/. Checking locale directly 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).
  8. 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-CN loads, ensure its source key is covered by LEGACY_ZH_REVERSE_FILES or LEGACY_ZH_EXTRA_SOURCE_KEYS in studio/src/i18n/manifest.ts.

Verification checklist

When adding or changing Studio translations or i18n plumbing:

  • Run npm run check (executes scripts/check-i18n.ts alongside repo and design checks).
  • Run npm run typecheck:studio and npx vitest run --config studio/vitest.config.ts studio/src/i18n/ studio/src/shellBoundary.test.ts.
  • Run npm run build:studio and verify studio/build/editor-assets/ emits 10 separate locale-*-*.js chunks without pulling locale JSON back into index-*.js.