跳转至

i18n Strategy Analysis

重要性:⭐⭐ 目标读者:所有用户(i18n 关注者) 关联topics/i18n-strategy.md


1. 概览

本文档深入分析 Claude Code 的 i18n 策略。

5 主题: - 当前状态 - 字符编码 - 文本方向 - Locale 推断 - UI 文本


2. 当前状态(推测)

2.1 已支持

  • ✅ UTF-8(bashParser 字节偏移)
  • ✅ RTL(Yoga Edge 抽象)
  • ✅ CJK(measure text)
  • ✅ 任何 Unicode 输入

2.2 未支持(推测)

  • ❌ UI 文本翻译
  • ❌ Locale 推断
  • ❌ 翻译文件
  • ❌ 多语言文档

3. 字符编码详解

3.1 bashParser 字节偏移

type Lexer = {
  byteLen: number
  charIdx: number       // 字符索引
  byteIdx: number       // 字节偏移
  byteAt(L, charIdx)    // 字符 → 字节
}

双索引

3.2 为什么字节

  • bash 工具输出用字节
  • 与 tree-sitter 兼容
  • UTF-8 安全

3.3 3449 黄金语料库

// Validated against 3449-input golden corpus

大规模验证

3.4 Ink 字符宽度

// measureText
// CJK = 2
// emoji = 1-2
// ASCII = 1

宽度计算


4. RTL 支持

4.1 Yoga Edge 抽象

const EDGE_LEFT = 0
const EDGE_TOP = 1
const EDGE_RIGHT = 2
const EDGE_BOTTOM = 3

物理边

4.2 逻辑边

// start / end
function resolveEdge(...) {
  if (direction === 'rtl') {
    // 镜像
  }
}

逻辑

4.3 flexDirection

isReverse(dir: FlexDirection): boolean

自动 reverse

4.4 ink/bidi.ts

// 139 行 BiDi 支持

双向文本


5. Locale 推断

5.1 推测实现

// 推测
function getLocale(): string {
  return process.env.LANG || process.env.LC_ALL || 'en_US'
}

env 优先

5.2 Bash Locale

$ echo $LANG
en_US.UTF-8

UTF-8 默认

5.3 macOS Locale

$ defaults read -g AppleLocale
en_US

系统

5.4 Windows Locale

$ Get-WinSystemLocale
en-US

系统


6. UI 文本(推测英文)

6.1 推测英文

// settings.json
"description": "Enable debug mode with optional category filtering..."

// 错误信息
throw new Error('Invalid input')

英文

6.2 推测无 t() 函数

// ❌ 推测未发现
t('errors.invalid_input')

未发现

6.3 推测无 locales/

locales/
├── en.json
├── zh.json

未发现


7. 4 个 i18n 维度

7.1 Locale

  • ⚠️ 弱

7.2 字符编码

  • ✅ 强

7.3 文本方向

  • ✅ 强

7.4 UI 翻译

  • ❌ 推测无

8. 5 个未来方向

8.1 引入 i18next

import i18next from 'i18next'
import en from './locales/en.json'
import zh from './locales/zh.json'
i18next.init({ resources: { en, zh } })

i18next

8.2 翻译文件

locales/
├── en.json
├── zh-CN.json
├── ja.json
└── ...

locales

8.3 命令 help 翻译

.option('-p, --print', t('options.print'))

help

8.4 错误码翻译

const errorMsg = t(`errors.${errorCode}`)

errors

8.5 文档多语言

docs/
├── en/
├── zh/
└── ja/

docs


9. 5 个最佳实践

9.1 用户设置

{ "preferredLanguage": "zh-CN" }

user

9.2 工具接受 Unicode

// 不限制 ASCII

unicode

9.3 文件 UTF-8

// 写文件用 UTF-8
fs.writeFileSync(path, content, 'utf-8')

UTF-8

9.4 终端 i18n

# 支持 LANG

locale

9.5 文档双语

# English | 中文

双语


10. 5 个反模式

10.1 假设 ASCII

// ❌ 假设只 ASCII

ASCII-only

10.2 不用 byte offset

// ❌ 用 char index

wrong

10.3 不用 Unicode normalize

// ❌ 不 NFC

no-norm

10.4 不用宽度计算

// ❌ 假设 1 char = 1 col

wrong-width

10.5 UI 硬编码英文

// ❌ 不翻译

hardcode


11. 5 个 Claude Code 优点

11.1 UTF-8 完善

bashParser + measure text。

11.2 RTL 完整

Yoga Edge 抽象。

11.3 字符宽度

CJK 2 倍。

11.4 Unicode 安全

3449 黄金语料库。

11.5 接受任意输入

不限制字符集。


12. 5 个不足

12.1 UI 英文

无翻译。

12.2 Locale 推断弱

仅 env。

12.3 无翻译文件

推测无。

12.4 文档英文

无多语言。

12.5 无用户配置

无 preferredLanguage。


13. 4 维 trade-off

i18n  ↔  简单
locale  ↔  默认
翻译  ↔  维护

3 维


14. 关键 insight

14.1 i18n 基础设施级

UTF-8 + RTL 已实现。

14.2 UI 文本是缺口

最缺 i18n 的部分。

14.3 bashParser 完整

字节偏移是亮点。

14.4 Yoga RTL 完整

支持 6 种 RTL 文本。

14.5 改进空间大

i18next 集成即可大幅提升。


15. 总结

i18n Strategy = 3 强 1 弱

核心: - 字符编码强 - RTL 强 - 接受输入强 - UI 翻译弱

下一步: - 引入 i18next - 加 locales - 翻译 help