react-native-i18n-workflow
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseReact Native i18n Workflow
React Native i18n 工作流
This skill handles the internationalization (i18n) workflow for the Fitness Tracker App, ensuring type-safe translations and consistent language support.
本技能为Fitness Tracker App处理国际化(i18n)工作流,确保类型安全的翻译和一致的语言支持。
When to Use This Skill
何时使用本技能
Use this skill when you need to:
- Add new translatable strings to the app
- Support a new language
- Implement interpolation (e.g., "Hello {{name}}!")
- Use pluralization in translations
- Handle RTL (Right-to-Left) language detection and layout
- Ensure type safety for translation keys
适用于以下场景:
- 向应用中添加新的可翻译字符串
- 支持新的语言
- 实现插值功能(例如:"Hello {{name}}!")
- 在翻译中使用复数形式
- 处理RTL(从右到左)语言的检测与布局
- 确保翻译键的类型安全
Translation Files
翻译文件
Translations are stored in with one file per language (e.g., , ).
app/i18n/en.tsja.ts翻译文件存储在目录下,每种语言对应一个文件(例如、)。
app/i18n/en.tsja.tsEnglish (Source) Pattern
英文(源语言)模板
typescript
// app/i18n/en.ts
const en = {
common: {
ok: "OK!",
cancel: "Cancel",
},
homeScreen: {
title: "My Collection",
deleteAlertMessage: "Delete this {{category}} workout?",
}
}
export default en
export type Translations = typeof entypescript
// app/i18n/en.ts
const en = {
common: {
ok: "OK!",
cancel: "Cancel",
},
homeScreen: {
title: "My Collection",
deleteAlertMessage: "Delete this {{category}} workout?",
}
}
export default en
export type Translations = typeof enUsing Translations
使用翻译内容
1. The translate
Helper
translate1. translate
辅助函数
translateUse the standalone function for non-component logic:
translatetypescript
import { translate } from "@/i18n"
const title = translate("homeScreen:title")
const message = translate("homeScreen:deleteAlertMessage", { category: "cat" })在非组件逻辑中使用独立的函数:
translatetypescript
import { translate } from "@/i18n"
const title = translate("homeScreen:title")
const message = translate("homeScreen:deleteAlertMessage", { category: "cat" })2. The tx
Prop
tx2. tx
属性
txMany components support a prop for automatic translation:
txtsx
<Text tx="homeScreen:title" preset="heading" />
<Button tx="common:ok" onPress={handlePress} />许多组件支持通过属性实现自动翻译:
txtsx
<Text tx="homeScreen:title" preset="heading" />
<Button tx="common:ok" onPress={handlePress} />3. The useTranslation
Hook
useTranslation3. useTranslation
钩子
useTranslationFor complex cases or dynamic content:
tsx
import { useTranslation } from "react-i18next"
const { t } = useTranslation()
return <Text>{t("homeScreen:title")}</Text>适用于复杂场景或动态内容:
tsx
import { useTranslation } from "react-i18next"
const { t } = useTranslation()
return <Text>{t("homeScreen:title")}</Text>Advanced Patterns
进阶用法
Interpolation
插值功能
Keys:
Usage:
greeting: "Hello, {{name}}!"translate("greeting", { name: "Mirza" })键示例:
用法:
greeting: "Hello, {{name}}!"translate("greeting", { name: "Mirza" })Pluralization
复数形式
Keys:
Usage:
pet_one: "{{count}} pet"pet_other: "{{count}} pets"translate("pet", { count: 5 })键示例:
用法:
pet_one: "{{count}} pet"pet_other: "{{count}} pets"translate("pet", { count: 5 })Key Path Types
键路径类型
We use to ensure keys exist at compile time.
Format: for top level, for deeper levels.
TxKeyPathsection:keysection:nested.key我们使用确保编译时键已存在。格式:顶层键使用,深层键使用。
TxKeyPathsection:keysection:nested.keyReferences
参考资料
See TRANSLATION_STRUCTURE.md for detailed file patterns.
See I18N_BEST_PRACTICES.md for naming conventions and workflow steps.
详细的文件模板请参考TRANSLATION_STRUCTURE.md。
命名规范和工作流步骤请参考I18N_BEST_PRACTICES.md。