用途:将 Flutter 应用配置为支持多语言与多地区,面向国际市场与不同用户区域。
核心配置:在 pubspec.yaml 添加 flutter_localizations(sdk: flutter)与 intl: any,并设置 flutter: generate: true 以启用 l10n 代码生成;在项目根目录创建 l10n.yaml,配置 arb-dir、template-arb-file、output-localization-file,可选 synthetic-package: false 将文件生成到 lib/。
ARB 资源:在 arb-dir 内放置 .arb 文件,模板文件(如 app_en.arb)定义键与 @key 元数据,翻译文件(如 app_es.arb)提供对应译文。
应用集成:在 MaterialApp/CupertinoApp 配置 localizationsDelegates(AppLocalizations.delegate 及 GlobalMaterial/Widgets/CupertinoLocalizations.delegate)与 supportedLocales,通过 AppLocalizations.of(context)!.key 读取文案。使用 WidgetsApp 时省略 GlobalMaterialLocalizations.delegate。
高级格式化:支持占位符、plural、select,以及通过 format 与 optionalParameters 使用 intl 的日期与数字格式化。
工作流:新增语言时创建 .arb、翻译模板全部键、加入 supportedLocales,运行 flutter gen-l10n 校验并重新生成;iOS 需在 Xcode 的 Runner 项目 Info 标签 Localizations 中添加语言并构建验证。
限制与注意:TextField、CupertinoTabBar 等需要 Localizations 祖先,否则报错,可在独立组件树中包裹 Localizations 并传入对应默认 delegate;多书写系统语言(如中文)建议用 Locale.fromSubtags 明确 scriptCode 与 countryCode。资料未提供收费、价格与效果信息。