i18n
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseYou make an app ready for any language and region, so adding a language later is translation work, not a refactor. Use the platform's own localisation and formatting APIs as the source of truth — never a custom framework around them.
让应用做好支持任何语言和地区的准备,这样以后添加语言就属于翻译工作,而不是重构。应以平台自身的本地化和格式化 API 为唯一依据——绝不要在其外围构建自定义框架。
1. Pick the Mode
1. 选择模式
Decide from the request; do not ask.
- Build mode — the task creates or changes UI or formatting code. Apply the rules as constraints while writing it. Mention a rule only when it changed a decision, in one line.
- Adopt mode — the user asks to internationalise, prepare for translation, or fix i18n across the app. Work through Step 4.
Ship no new languages unless asked. Making strings localisable and adding translations are separate jobs.
根据请求自行判断;不要询问。
- 构建模式 — 任务需要创建或修改 UI 或格式化代码。编写代码时,将以下规则作为约束条件。只有当某条规则影响了决策时,才用一行提及该规则。
- 采用模式 — 用户要求对整个应用进行国际化、做好翻译准备或修复 i18n 问题。按照步骤 4 执行。
除非用户明确要求,否则不要交付新语言。将字符串改为可本地化和添加翻译是两件不同的工作。
2. Detect the Platform
2. 检测平台
Read the manifest as prose and load only the matching guide:
| Platform | Detected by | Load |
|---|---|---|
| Web / JS / TS | | |
| Apple platforms | | |
| Android | | |
If the project already uses an i18n library (, , , , …), keep it and follow its conventions; the guide covers what to check, not which library to swap to. React Native/Expo uses for strings and formatting. Other stacks: apply the rules using that platform's ICU/CLDR-backed APIs.
next-intlreact-i18nextvue-i18nFormatJSreferences/js.md阅读项目清单并据此判断,只加载匹配的指南:
| 平台 | 检测依据 | 加载 |
|---|---|---|
| Web / JS / TS | | |
| Apple 平台 | | |
| Android | | |
如果项目已经使用某个 i18n 库(、、、 等),则保留该库并遵循其约定;本指南介绍的是需要检查的内容,而不是应该替换成哪个库。React Native/Expo 的字符串和格式化处理使用 。对于其他技术栈,应使用相应平台基于 ICU/CLDR 的 API 应用这些规则。
next-intlreact-i18nextvue-i18nFormatJSreferences/js.md3. Rules
3. 规则
| Rule | Impact | Applies to | File |
|---|---|---|---|
| Localisable strings | CRITICAL | every user-facing string, plurals, interpolation | |
| Time zones and timestamps | CRITICAL | stored events, history, timelines, schedules | |
| Locale-aware formatting | HIGH | dates, times, numbers, percentages, currency, durations | |
| Units and measurements | HIGH | distance, speed, weight, temperature | |
| Text and layout | HIGH | user-generated text, layout, RTL, long translations | |
| Deterministic tests | MEDIUM | tests touching dates, numbers, or strings | |
| 规则 | 影响程度 | 适用范围 | 文件 |
|---|---|---|---|
| 可本地化字符串 | 关键 | 所有面向用户的字符串、复数形式、插值 | |
| 时区和时间戳 | 关键 | 存储的事件、历史记录、时间线、计划任务 | |
| 区域设置感知格式化 | 高 | 日期、时间、数字、百分比、货币、持续时间 | |
| 单位和测量值 | 高 | 距离、速度、重量、温度 | |
| 文本和布局 | 高 | 用户生成内容、布局、RTL、过长翻译 | |
| 确定性测试 | 中 | 涉及日期、数字或字符串的测试 | |
4. Adopt Mode
4. 采用模式
- Inventory — find hardcoded user-facing strings, hand-rolled date/number formats, string concatenation, fixed units, and /
leftlayout. Report the counts before editingright - Set up the resource — one string catalog or message file in the platform's native format, English (or the project's development language) only
- Fix in impact order — CRITICAL first. Change only what international correctness needs; keep the existing design and behaviour
- Never change persisted data semantics (timestamp meaning, stored units) without an explicit migration the user has approved — propose it instead
- Test — add the deterministic tests from ; build and run the suite, and report real results
rules/deterministic-tests.md - Validate the UI — pseudolocalisation, the longest text expansion, RTL, and the largest text size, using the tools in the platform guide
End with what changed, what is still hardcoded and why, and any migration you proposed but did not apply.
- 盘点 — 找出硬编码的用户可见字符串、手写的日期或数字格式、字符串拼接、固定单位以及 /
left布局。编辑前先报告各项数量right - 设置资源 — 创建一个采用平台原生格式的字符串目录或消息文件,且只包含英语(或项目的开发语言)
- 按影响程度修复 — 先处理关键项。只进行确保国际化正确性所必需的更改;保留现有设计和行为
- 绝不改变持久化数据的语义(时间戳含义、存储单位),除非用户明确批准迁移;此时应先提出迁移方案
- 测试 — 根据 添加确定性测试;构建并运行测试套件,并报告实际结果
rules/deterministic-tests.md - 验证 UI — 使用平台指南中的工具验证伪本地化、最长文本扩展、RTL 和最大字号
最后说明改了什么、哪些内容仍为硬编码及其原因,以及提出了但未应用的迁移。
Boundaries
边界
- Accessibility labels must be localisable like any string; what they should say belongs to .
accessibility - User-generated content (names, notes, place names) is never translated, transliterated, or normalised unless the user asks.
- Translation itself — machine translation, translation services, and translator workflow are out of scope. Do not send app strings or user content to an external service.
- 辅助功能标签必须像其他字符串一样支持本地化;标签的具体措辞属于 的范畴。
accessibility - 用户生成内容(姓名、备注、地点名称)绝不进行翻译、转写或规范化,除非用户提出要求。
- 翻译本身——机器翻译、翻译服务和译员工作流——不在范围内。不要将应用字符串或用户内容发送到外部服务。