Skip to content

国际化 (i18n)

SWUST Code 支持 17 种语言的国际化系统。

支持的语言

代码语言代码语言
enEnglishzh简体中文
zht繁體中文ja日本語
ko한국어deDeutsch
esEspañolfrFrançais
ruРусскийptPortuguês
arالعربيةdaDansk
plPolskinoNorsk
thไทยtrTürkçe

设置语言

方式 1:环境变量

bash
export SWUST_CODE_LOCALE=zh
swust-code

方式 2:TUI 内切换

在 TUI 中输入 /language 命令,从列表中选择语言。

方式 3:自动检测

默认设置为 auto,系统会根据以下顺序自动检测:

  1. 时区 — 根据系统时区推断语言
  2. 环境变量 — 检查 LC_ALLLC_MESSAGESLANG
  3. Intl API — 使用浏览器/运行时的 locale 信息

添加新语言

  1. packages/tui/src/i18n/ 下创建新的翻译文件(如 ja.ts
  2. en.ts 导入 Keys 类型
  3. 使用 satisfies Partial<Record<Keys, string>> 确保类型安全
  4. locales.ts 中注册新的 Locale 类型
  5. context/language.tsxloadDict 中添加动态导入

翻译字典结构

typescript
// en.ts - 英文字典(canonical keys)
const en = {
  "tui.prompt.placeholder": "Type a message...",
  "tui.command.memory.title": "Memory",
  "tui.tip.1": "Use /memory to search...",
} as const

// zh.ts - 中文字典
import type { Dictionary } from "./en"
const zh: Dictionary = {
  "tui.prompt.placeholder": "输入消息...",
  "tui.command.memory.title": "记忆",
}

缺失的 key 会自动回退到英文。