返回提示词目录第 1516 页

配置 Flutter 应用本地化

指导 Flutter 项目接入多语言资源、生成代码、切换语言,并完成平台声明与测试验证。

完整提示词

<context>
Flutter's standard path is `flutter_localizations` plus the `intl` package, with messages in ARB files and Dart classes generated by `gen-l10n` from an `l10n.yaml`. What goes wrong: placeholders declared without `@key` metadata get no type, so numbers and dates are passed as pre-formatted strings and lose locale formatting; plurals written as separate keys instead of one ICU `{count, plural, ...}` message fail in languages with `few` and `many`; descriptions are skipped, so translators get no context. Recent Flutter versions changed where generated code lives (the synthetic `flutter_gen` package is being phased out in favour of an explicit output directory), so copying an old tutorial's import breaks. `AppLocalizations.of(context)` returns null when called with a context above `MaterialApp` or in `initState`. Cupertino widgets need their own delegate. On iOS the supported languages must also be listed in `Info.plist` (`CFBundleLocalizations`), or iOS will not show the app in those languages and the per-app language setting will not list them. An in-app picker only works if the chosen `Locale` is passed to `MaterialApp.locale` from app state and persisted.
</context>

<task>
Set up localisation in this Flutter project:

{{project}}

Locales: {{locales}}. In-app language picker: {{in_app_language_picker}}.

1. Run `flutter analyze` and `flutter test` and record the baseline. Stop and report if they fail.
2. Read `pubspec.yaml`, any `l10n.yaml`, the `MaterialApp` or `CupertinoApp` setup and any existing localisation code.
3. Configure: add `flutter_localizations` (SDK) and `intl`, enable generation in `pubspec.yaml`, and write `l10n.yaml` with `arb-dir`, `template-arb-file`, `output-localization-file`, an explicit output directory if the installed Flutter version expects one, `nullable-getter: false`, and `untranslated-messages-file` so gaps are reported. Check the installed Flutter version's documentation for current option names.
4. ARB files: one per locale in {{locales}}. In the template, give each message a `@key` entry with a `description` and typed `placeholders` (`int` or `num` with a number format for counts and amounts, `DateTime` with a date format for dates, `String` otherwise). Write counts as ICU plurals with every category the target languages need and choices as ICU selects. Non-template files contain only the messages, not the metadata.
5. App wiring: set `localizationsDelegates` and `supportedLocales` from the generated class (including the Cupertino delegate if Cupertino widgets are used), set `onGenerateTitle` for the localized app title, and access strings with `AppLocalizations.of(context)` below the app widget only, never in `initState`.
6. Locale switching: if {{in_app_language_picker}} is true, hold the selected `Locale` in the app's existing state management, persist it, pass it to `MaterialApp.locale`, and offer "system default" as an option. Add a `localeResolutionCallback` or rely on the default matching, and state which.
7. Platform declarations: add `CFBundleLocalizations` to the iOS `Info.plist` for every locale; on Android 13 and later, declare supported locales for the per-app language setting; for right-to-left locales confirm widgets use directional values (`EdgeInsetsDirectional`, `AlignmentDirectional`).
8. Convert a small representative slice (one screen with a plural, a date and a price) and list remaining hard-coded strings.
9. Run code generation, `flutter analyze` and `flutter test` again, add a widget test that pumps the slice in two locales with different plural rules, and report results.
</task>

<constraints>
- Never concatenate localized fragments or format numbers and dates before passing them as placeholders.
- Do not invent translations; non-template ARB files start with the template text and are listed in the untranslated report.
- Do not claim iOS or Android system-setting behaviour was checked without a device or simulator.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Baseline
Analyze and test results before changes.

## Configuration
`pubspec.yaml` and `l10n.yaml` changes.

## ARB files
Template with metadata, plural and select examples.

## App wiring
Delegates, supported locales, title and access pattern.

## Locale switching
State, persistence and resolution.

## Platform declarations
iOS, Android and directionality changes.

## Verification
Commands and widget tests with real results.

## Follow-ups
Remaining strings and decisions.
</output_format>

可替换内容

  • {{project}}
  • {{locales}}
  • {{in_app_language_picker}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、Flutter开发、多语言应用、本地化配置

前往 AI 图片创作