Skip to main content

L10n (Localization)

This command facilitates the generation of localization classes from ARB (Application Resource Bundle) files. It acts as a powerful wrapper around Flutter's gen-l10n, adding the capability to split your ARB files by feature for better maintainability.

morpheme l10n

Features

  • Standard Generation: Wraps flutter gen-l10n for standard ARB setups.
  • Split ARB Support: Allows breaking down a large monolithic ARB file into smaller, feature-specific files (e.g., profile_en.arb, home_en.arb) which are then merged automatically.

Configuration

The localization configuration is managed in morpheme.yaml.

morpheme.yaml
localization:
arb_dir: assets/l10n # Directory containing ARB files
template_arb_file: app_en.arb # The template ARB file (usually English)
output_localization_file: app_localizations.dart # Output filename
output_class: AppLocalizations # Generated class name
output_dir: core/lib/src/l10n # Directory for generated Dart code
replace: true # Whether to merge split ARB files into the main file

Directory Structure Strategies

1. Single ARB (Basic)

Suitable for smaller applications where all translations are kept in a single file per language.

assets/
└── l10n/
├── app_en.arb
└── app_id.arb

In this mode, replace should be set to false (or simply not used if you don't have subdirectories).

For larger apps, you can organize translations by feature. The command will search for subdirectories matching your locale codes and merge all ARB files within them into the main ARB file.

Structure:

assets/
└── l10n/
├── app_en.arb # Generated/Merged file (Do not edit manually if replace: true)
├── app_id.arb # Generated/Merged file
├── en/ # English source files
│ ├── common.arb
│ ├── auth.arb
│ └── profile.arb
└── id/ # Indonesian source files
├── common.arb
├── auth.arb
└── profile.arb

How it works:

  1. Set replace: true in morpheme.yaml.
  2. Create a folder assets/l10n/en and place your partial ARB files there.
  3. Run morpheme l10n.
  4. The command merges all files in assets/l10n/en/*.arb into assets/l10n/app_en.arb.
  5. It then runs flutter gen-l10n to generate the Dart code.
caution

When using Split ARB with replace: true, do not edit app_en.arb directly. Your changes will be overwritten by the merged content from the subdirectories. Always edit the files inside the en/ or id/ folders.

Usage

Run the command to generate your localizations:

morpheme l10n

Options

morpheme l10n [options]

To see all available options and flags, run morpheme l10n --help.

Available Options

OptionDescription
--morpheme-yaml [path]Path to a custom configuration file (default: morpheme.yaml).

VS Code Extension Tip

For a better editing experience, we recommend the i18n-arb-editor extension. It provides a UI for editing ARB files.