i18n.md 6.8 KB

i18n Runtime

Goal

ZCode needs localization for human-facing CLI and TUI copy without changing the agent protocol, model-visible control text, tool schemas, or event contracts. The first slice keeps localization small, typed, and deterministic so the NL to code loop remains stable while UI copy can move to Chinese.

Supported Locales

P0 supports:

  • en-US
  • zh-CN

The default fallback is en-US. The config value may also be auto, which resolves from adapter-provided locale hints when available and otherwise falls back to en-US.

Configuration

Config shape:

{
  "ui": {
    "locale": "zh-CN"
  }
}

ui.locale accepts en-US, zh-CN, or auto.

Effective priority follows normal config precedence:

  1. system defaults
  2. user config
  3. project config
  4. session override
  5. CLI override

No ZCODE_* environment variable is added. If a future environment override is needed, it must be specified here first with precedence, validation, error behavior, and test coverage.

Auto Detection

auto is resolved only at adapter or entry boundaries. Business modules, core, and TUI never read environment variables or platform locale APIs directly.

Detection uses the first supported natural-language locale from these hints:

  1. an explicit adapter-provided detected locale hint
  2. LC_ALL
  3. LC_MESSAGES
  4. LANG
  5. LANGUAGE entries, in colon-separated order
  6. Intl.DateTimeFormat().resolvedOptions().locale

Locale parsing strips encoding and modifiers such as .UTF-8 and @calendar=gregorian, maps _ to -, and accepts common language tags such as zh, zh_CN, zh-Hans-CN, en, and en_GB. C and POSIX locale values are ignored because they do not describe a user-facing language preference. Unsupported detected values are skipped; if no supported value is found, the effective locale is en-US.

Boundary

Localized:

  • TUI status text, sidebar labels, transcript role labels, selection panels, approval panels, short error labels, slash command descriptions, and local command-center pickers such as /login Coding Plan setup.
  • CLI human-readable help, usage, and error text.

Not localized:

  • JSON output keys and protocol enum values.
  • Tool names, tool schemas, MCP names, event types, session ids, trace ids, message ids, model ids, provider ids, file paths, command names, and command arguments.
  • Model-visible system/developer instructions and tool result semantics.
  • Logs and traces unless a field is explicitly documented as display-only.

Slash command names remain stable English protocol tokens such as /help, /model, and /mcp; summaries and help body text may be localized.

Slash Command

The command center owns /locale; TUI only forwards the prompt and applies the returned locale projection. Supported forms:

  • /locale: show the effective locale, the supported configured values, and whether switching is available in the current client.
  • /locale auto
  • /locale en-US
  • /locale zh-CN

/language is an input alias for /locale; help and suggestions should present /locale as the canonical command.

Switching locale is an app/server contract, not a TUI state mutation. The command calls ZCodeApp.setLocale(), and the app persists the requested config value before changing the effective in-memory locale exposed by ZCodeApp.getLocale(). For auto, the requested value stored on disk remains auto, while the command result returns the resolved effective locale for the current process.

Persistence target:

  • If a project config explicitly defines ui.locale, update that nearest loaded project config file so project-specific policy stays round-trippable.
  • Otherwise update the user config path, creating the file if needed.
  • Environment variables are not a supported locale config source.
  • CLI --locale remains a process override, but /locale may supersede it for the active TUI process and for future sessions created in that process.

Config writes patch only ui.locale; unrelated model, provider, mcp, skills, permission, and other user content must be preserved. Write failure returns a command error and must not update the active effective locale.

Runtime Contract

@zcode/i18n exports a pure typed catalog plus pure locale-resolution helpers. It has no filesystem, network, environment, or async initialization path. Callers select a locale once at their presentation boundary and pass the resulting copy object to formatters or components.

The CLI must resolve the effective startup locale before rendering the TUI, even when login is required and ZCodeApp is intentionally not created yet. This pre-login path still reads the same config precedence as app startup, applies CLI --locale overrides, resolves auto from adapter-provided detection, and passes the supported effective locale into local panels such as the Coding Plan setup picker. This prevents persisted ui.locale: "zh-CN" from being ignored on the first unauthenticated screen.

Dynamic copy is represented as typed functions, not ad-hoc string interpolation at call sites. Values passed into those functions must already be bounded or redacted by the formatter that understands terminal width and sensitivity.

Missing, unsupported, or malformed locale values resolve to en-US. Invalid config file values are rejected by the config schema rather than silently accepted.

Module Boundaries

  • contracts defines UiLocale and config keys.
  • adapters/config parses and merges ui.locale.
  • bootstrap exposes the effective locale on ZCodeApp.
  • cli passes CLI/session locale to TUI and uses localized CLI copy for human-readable output. It provides adapter-level locale hints from standard OS locale signals.
  • tui renders from the typed copy object and does not read config or environment directly.
  • core remains locale-agnostic.

Tests

  • Config schema accepts supported locale values and rejects invalid values.
  • Env parsing ignores undeclared locale environment variables.
  • Locale resolver maps auto, OS locale hints, and common language tags to supported locales with deterministic fallback.
  • CLI --locale auto uses detected locale hints for help and TUI startup.
  • Bootstrap resolves config-file ui.locale: "auto" before exposing ZCodeApp.getLocale().
  • /locale can query or switch the effective locale from the command center, including TUI slash command flow, and persists the requested value to config.
  • Catalog parity checks ensure every supported locale has the same key shape.
  • Representative TUI render tests assert English default and Chinese localized labels/status.
  • CLI startup tests cover the login-required path so persisted ui.locale is honored before ZCodeApp exists.
  • CLI help can render with an explicit CLI locale override.
  • JSON output and protocol tokens stay stable across locales.