flutter-localization
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLocalization Setup
本地化配置
- Use Flutter's built-in and
flutter_localizationspackagesintl - Enable in
generate: truefor automatic code generationpubspec.yaml - Configure at project root:
l10n.yamlyamlarb-dir: lib/l10n template-arb-file: app_en.arb output-localization-file: app_localizations.dart output-class: AppLocalizations nullable-getter: false
- 使用Flutter内置的和
flutter_localizations包intl - 在中启用
pubspec.yaml以开启自动代码生成generate: true - 在项目根目录配置:
l10n.yamlyamlarb-dir: lib/l10n template-arb-file: app_en.arb output-localization-file: app_localizations.dart output-class: AppLocalizations nullable-getter: false
ARB File Structure
ARB文件结构
- Place all files in
.arblib/l10n/ - Use naming convention (e.g.,
app_{locale}.arb,app_en.arb,app_es.arb)app_hi.arb - The template ARB file () MUST contain
app_en.arbmetadata for every key:@json{ "loginTitle": "Sign In", "@loginTitle": { "description": "Title on the login screen" }, "itemCount": "{count, plural, =0{No items} =1{1 item} other{{count} items}}", "@itemCount": { "description": "Item count with pluralization", "placeholders": { "count": { "type": "int" } } } } - Key naming: Use format (e.g.,
featureName_context,settings_title)login_emailHint - Group keys by feature in the ARB file with comment separators
- 所有文件放置在
.arb目录下lib/l10n/ - 使用命名规范(例如
app_{locale}.arb、app_en.arb、app_es.arb)app_hi.arb - 模板ARB文件()必须为每个键包含
app_en.arb开头的元数据:@json{ "loginTitle": "Sign In", "@loginTitle": { "description": "Title on the login screen" }, "itemCount": "{count, plural, =0{No items} =1{1 item} other{{count} items}}", "@itemCount": { "description": "Item count with pluralization", "placeholders": { "count": { "type": "int" } } } } - 键名命名:使用格式(例如
功能模块名_上下文、settings_title)login_emailHint - 在ARB文件中通过注释分隔符按功能模块对键进行分组
Usage Rules
使用规则
- Access strings via using a
context.l10n.keyNameextensionBuildContext - STRICTLY prohibit hardcoded user-facing strings anywhere in the codebase
- STRICTLY prohibit using in
.l10nor outside the widget tree —initState()must be availablecontext - For date/time/number formatting, use package formatters with the current locale
intl
- 通过扩展,使用
BuildContext访问字符串context.l10n.keyName - 严格禁止在代码库的任何位置硬编码面向用户的字符串
- 严格禁止在中或widget树之外使用
initState()——必须保证.l10n可用context - 对于日期/时间/数字格式化,结合当前区域设置使用包的格式化工具
intl
Plural & Select Forms
复数与选择形式
- Use ICU plural syntax for countable items:
{count, plural, =0{...} =1{...} other{...}} - Use ICU select syntax for gender/category:
{gender, select, male{...} female{...} other{...}} - Always include the case as a fallback
other
- 对可数项目使用ICU复数语法:
{count, plural, =0{...} =1{...} other{...}} - 对性别/分类场景使用ICU选择语法:
{gender, select, male{...} female{...} other{...}} - 始终包含分支作为兜底
other
RTL Support
RTL支持
- Use and
Directionality-aware widgetsTextDirection - Prefer over
EdgeInsetsDirectionalfor padding/marginsEdgeInsets - Use /
startinstead ofend/leftinrightandMainAxisAlignmentCrossAxisAlignment - Test with RTL locales during widget testing
- 使用支持和
Directionality的组件TextDirection - 内边距/外边距优先使用而非
EdgeInsetsDirectionalEdgeInsets - 在和
MainAxisAlignment中使用CrossAxisAlignment/start替代end/leftright - 在组件测试时使用RTL区域设置进行测试
Adding a New Language
新增一门语言
- Create with all keys translated
app_{locale}.arb - Run (or rely on auto-generation)
flutter gen-l10n - Add the to
LocaleinsupportedLocalesMaterialApp - Verify with widget tests using the new locale
- 创建文件,完成所有键的翻译
app_{locale}.arb - 运行(或依赖自动生成机制)
flutter gen-l10n - 在的
MaterialApp中添加对应的supportedLocalesLocale - 使用新区域设置编写组件测试验证功能