Migration Guide: Flutter gen_l10n → anas_localization¶
This guide migrates a project that currently uses:
- l10n.yaml
- ARB files (for example lib/l10n/app_en.arb)
- generated AppLocalizations
to:
- JSON locale files (assets/lang/*.json)
- generated typed dictionary (lib/generated/dictionary.dart)
- runtime wrapper (AnasLocalization)
Prerequisites¶
- Existing app compiles with current
gen_l10nsetup. - You have
l10n.yamlcommitted. - You can run Flutter/Dart CLI from the app root.
Step 1: Import ARB into JSON¶
Fast path with the converter command:
Import directly using your currentl10n.yaml:
This reads:
- arb-dir
- template-arb-file
- preferred-supported-locales (if present)
Then writes:
- assets/lang/en.json
- assets/lang/ar.json
- etc.
Step 2: Validate translations¶
Run validation before code generation:
For CI:
Step 3: Generate typed dictionary API¶
Optional development watch mode:
Step 4: Replace app bootstrap¶
Old:
MaterialApp(
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
)
New:
AnasLocalization(
fallbackLocale: const Locale('en'),
assetPath: 'assets/lang',
assetLocales: const [Locale('en'), Locale('ar')],
app: MaterialApp(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
DictionaryLocalizationsDelegate(),
],
supportedLocales: const [Locale('en'), Locale('ar')],
),
)
Step 5: Replace lookups incrementally¶
Old:
New:
You can migrate screen-by-screen. Both systems can temporarily coexist during the transition window.
Compatibility Notes¶
- ARB metadata (
@key,@@locale) is preserved by ARB import/export utilities. - Existing translator ARB workflow can stay active:
- Nested keys from ARB (
home.title) are converted into JSON nested maps.
Rollback Plan¶
If migration must be reverted:
- Keep your original ARB folder untouched (
lib/l10n). - Re-enable
gen_l10ndelegates and generated class usage. - Remove
AnasLocalizationwrapper andDictionaryLocalizationsDelegate. - Remove generated dictionary file and related imports.
Because ARB files remain the source of truth during migration, rollback is low-risk.
Verification Checklist¶
flutter analyzeflutter testdart run anas_localization:anas_cli validate assets/lang --profile=strict --fail-on-warnings- Smoke-test locale switching on device/simulator