flutter-localization by flutter/skills
npx skills add https://github.com/flutter/skills --skill flutter-localization在 Flutter 应用程序中配置并实现国际化(i18n)和本地化(l10n)。此技能管理依赖注入(flutter_localizations、intl)、代码生成配置(l10n.yaml)、根部件设置(MaterialApp、CupertinoApp 或 WidgetsApp)、.arb 翻译文件管理以及平台特定配置(iOS Xcode 项目更新)。它确保正确的区域设置解析,并防止在特定部件(如 TextField 和 CupertinoTabBar)中因缺少本地化委托而导致的常见断言错误。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
MaterialApp、CupertinoApp 还是 WidgetsApp,以便注入正确的全局委托。Info.plist / project.pbxproj),以向 App Store 公开支持的区域设置。TextField 或 CupertinoTabBar 部件。如果找到,则用显式的 Localizations 部件包装它们。zh_Hans_CN),请使用 Locale.fromSubtags 而不是默认的 Locale 构造函数。配置依赖项 更新 pubspec.yaml 以包含所需的包并启用代码生成。
dependencies: flutter: sdk: flutter flutter_localizations: sdk: flutter intl: any
flutter: generate: true
配置代码生成 在项目根目录中创建一个 l10n.yaml 文件,以定义本地化工具的行为。
arb-dir: lib/l10n template-arb-file: app_en.arb output-localization-file: app_localizations.dart synthetic-package: false
定义支持的区域设置 请暂停并询问用户: “您希望支持哪些语言和地区?请提供语言代码列表(例如 'en', 'es', 'zh_Hans_CN')。”
创建 ARB 文件 生成模板 .arb 文件(例如 lib/l10n/app_en.arb)和相应的翻译文件。根据需要实现占位符、复数和选择器。
{ "helloWorld": "Hello World!", "@helloWorld": { "description": "标准问候语" }, "greeting": "Hello {userName}", "@greeting": { "description": "带参数的问候语", "placeholders": { "userName": { "type": "String" } } }, "nWombats": "{count, plural, =0{no wombats} =1{1 wombat} other{{count} wombats}}", "@nWombats": { "placeholders": { "count": { "type": "num", "format": "compact" } } } }
初始化根应用 导入生成的本地化文件并配置根 MaterialApp 或 CupertinoApp。
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:your_app_name/l10n/app_localizations.dart'; // 根据 synthetic-package 设置调整路径
// 在根部件的构建方法中: return MaterialApp( title: 'Localized App', localizationsDelegates: const [ AppLocalizations.delegate, GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], supportedLocales: const [ Locale('en', ''), // 英语 Locale('es', ''), // 西班牙语 Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN'), ], home: const MyHomePage(), );
处理独立部件(如适用) 如果 TextField 或 CupertinoTabBar 抛出缺少 MaterialLocalizations 或 Localizations 祖先的错误,请在其正上方直接注入一个 Localizations 部件。
Localizations( locale: const Locale('en', 'US'), delegates: const <LocalizationsDelegate<dynamic>>[ DefaultWidgetsLocalizations.delegate, DefaultMaterialLocalizations.delegate, DefaultCupertinoLocalizations.delegate, ], child: CupertinoTabBar( items: const <BottomNavigationBarItem>[...], ), )
配置 iOS 项目 请暂停并询问用户: “此项目是否针对 iOS?如果是,我将提供更新 Xcode 项目的说明。” 如果是,请指导用户执行以下操作:
1. 在 Xcode 中打开 `ios/Runner.xcodeproj`。
2. 在项目导航器中选择 `Runner` 项目。
3. 转到 `Info` 选项卡。
4. 在 **Localizations** 下,点击 `+` 添加所有支持的语言。
8. 验证与修复 运行 flutter gen-l10n。验证 app_localizations.dart 是否成功生成。如果编译失败并出现“未找到 MaterialLocalizations”或“CupertinoTabBar 需要一个 Localizations 父部件”的错误,请从出错的部件开始向上遍历部件树,并确保正确提供了 localizationsDelegates。
synthetic-package: false,或者对于现代 Flutter 版本,依赖标准的 generate: true 行为。如果设置了 synthetic-package: false,则不要使用 package:flutter_gen 导入。TextField 必须 有一个 MaterialLocalizations 祖先。CupertinoTabBar 必须 有一个 Localizations 祖先。zh_Hans、zh_Hant),始终 使用 Locale.fromSubtags。.arb 字符串中使用的所有占位符都在相应的 @ 元数据对象中明确定义。.arb 文件中需要字面量花括号 {} 或单引号 ',请在 l10n.yaml 中启用 use-escaping: true,并使用连续的单引号 '' 进行转义。每周安装量
978
代码仓库
GitHub 星标数
792
首次出现
2026年3月4日
安全审计
已安装于
codex950
opencode946
cursor946
gemini-cli945
github-copilot945
kimi-cli943
Configures and implements internationalization (i18n) and localization (l10n) in a Flutter application. This skill manages dependency injection (flutter_localizations, intl), code generation configuration (l10n.yaml), root widget setup (MaterialApp, CupertinoApp, or WidgetsApp), .arb translation file management, and platform-specific configurations (iOS Xcode project updates). It ensures proper locale resolution and prevents common assertion errors related to missing localization delegates in specific widgets like TextField and CupertinoTabBar.
MaterialApp, CupertinoApp, or WidgetsApp to inject the correct global delegates.Info.plist / project.pbxproj) must be updated to expose supported locales to the App Store.TextField or CupertinoTabBar widgets that might exist outside the root app's localization scope. If found, wrap them in explicit Localizations widgets.zh_Hans_CN), use instead of the default constructor.Configure Dependencies Update pubspec.yaml to include required packages and enable code generation.
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: any
flutter:
generate: true
Configure Code Generation Create an l10n.yaml file in the project root to define the localization tool's behavior.
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
synthetic-package: false
Define Supported Locales STOP AND ASK THE USER: "Which languages and regions do you want to support? Please provide a list of language codes (e.g., 'en', 'es', 'zh_Hans_CN')."
Create ARB Files Generate the template .arb file (e.g., lib/l10n/app_en.arb) and corresponding translation files. Implement placeholders, plurals, and selects as needed.
synthetic-package: false is considered if the user's environment requires direct source generation, or rely on standard generate: true behavior for modern Flutter versions. Do not use package:flutter_gen imports if synthetic-package: false is set.TextField MUST have a MaterialLocalizations ancestor. CupertinoTabBar MUST have a Localizations ancestor.Locale.fromSubtags for languages requiring script codes (e.g., Chinese , ).Weekly Installs
978
Repository
GitHub Stars
792
First Seen
Mar 4, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex950
opencode946
cursor946
gemini-cli945
github-copilot945
kimi-cli943
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
Grimoire CLI 使用指南:区块链法术编写、验证与执行全流程
940 周安装
Grimoire Uniswap 技能:查询 Uniswap 元数据与生成代币/资金池快照的 CLI 工具
940 周安装
Grimoire Aave 技能:查询 Aave V3 元数据和储备快照的 CLI 工具
941 周安装
Railway CLI 部署指南:使用 railway up 命令快速部署代码到 Railway 平台
942 周安装
n8n Python 代码节点使用指南:在自动化工作流中编写 Python 脚本
943 周安装
Flutter Platform Views 实现指南:Android/iOS/macOS原生视图与Web嵌入教程
943 周安装
Locale.fromSubtagsLocale{
"helloWorld": "Hello World!",
"@helloWorld": {
"description": "Standard greeting"
},
"greeting": "Hello {userName}",
"@greeting": {
"description": "Greeting with a parameter",
"placeholders": {
"userName": {
"type": "String"
}
}
},
"nWombats": "{count, plural, =0{no wombats} =1{1 wombat} other{{count} wombats}}",
"@nWombats": {
"placeholders": {
"count": {
"type": "num",
"format": "compact"
}
}
}
}
Initialize Root App Import the generated localizations file and configure the root MaterialApp or CupertinoApp.
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:your_app_name/l10n/app_localizations.dart'; // Adjust path based on synthetic-package setting
// Inside your root widget build method:
return MaterialApp(
title: 'Localized App',
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: const [
Locale('en', ''), // English
Locale('es', ''), // Spanish
Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN'),
],
home: const MyHomePage(),
);
Handle Isolated Widgets (If Applicable) If a TextField or CupertinoTabBar throws a missing MaterialLocalizations or Localizations ancestor error, inject a Localizations widget directly above it.
Localizations(
locale: const Locale('en', 'US'),
delegates: const <LocalizationsDelegate<dynamic>>[
DefaultWidgetsLocalizations.delegate,
DefaultMaterialLocalizations.delegate,
DefaultCupertinoLocalizations.delegate,
],
child: CupertinoTabBar(
items: const <BottomNavigationBarItem>[...],
),
)
Configure iOS Project STOP AND ASK THE USER: "Does this project target iOS? If yes, I will provide instructions for updating the Xcode project." If yes, instruct the user to:
ios/Runner.xcodeproj in Xcode.Runner project in the Project Navigator.Info tab.+ to add all supported languages.Validate and Fix Run flutter gen-l10n. Verify that app_localizations.dart is generated successfully. If compilation fails with "No MaterialLocalizations found" or "CupertinoTabBar requires a Localizations parent", traverse up the widget tree from the failing widget and ensure localizationsDelegates are properly provided.
zh_Hanszh_Hant.arb strings are explicitly defined in the corresponding @ metadata object.{} or single quotes ' are needed in .arb files, enable use-escaping: true in l10n.yaml and use consecutive single quotes '' for escaping.