国际化 (i18n)
SWUST Code 支持 17 种语言的国际化系统。
支持的语言
| 代码 | 语言 | 代码 | 语言 |
|---|---|---|---|
| en | English | zh | 简体中文 |
| zht | 繁體中文 | ja | 日本語 |
| ko | 한국어 | de | Deutsch |
| es | Español | fr | Français |
| ru | Русский | pt | Português |
| ar | العربية | da | Dansk |
| pl | Polski | no | Norsk |
| th | ไทย | tr | Türkçe |
设置语言
方式 1:环境变量
bash
export SWUST_CODE_LOCALE=zh
swust-code方式 2:TUI 内切换
在 TUI 中输入 /language 命令,从列表中选择语言。
方式 3:自动检测
默认设置为 auto,系统会根据以下顺序自动检测:
- 时区 — 根据系统时区推断语言
- 环境变量 — 检查
LC_ALL、LC_MESSAGES、LANG - Intl API — 使用浏览器/运行时的 locale 信息
添加新语言
- 在
packages/tui/src/i18n/下创建新的翻译文件(如ja.ts) - 从
en.ts导入Keys类型 - 使用
satisfies Partial<Record<Keys, string>>确保类型安全 - 在
locales.ts中注册新的 Locale 类型 - 在
context/language.tsx的loadDict中添加动态导入
翻译字典结构
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 会自动回退到英文。