easy-localization

The preset for easy_localization in Flutter. It reads tr() and plural() calls in Dart, nested plural and gender keys, and {} placeholders.

The easy-localization preset supports easy_localization for Flutter.

Detection

The preset applies when pubspec.yaml has the dependency easy_localization.

x18n.config.ts
import { defineConfig } from '@x18n/core'

export default defineConfig({
  presets: ['easy-localization'],
})

Locale files

If your config has no resources, the preset uses assets/translations/{locale}.{ext}. The locale can have a country, such as en-US.json. New files use nested keys.

Supported syntax

In Dart files, the preset reads the string and Text extensions, the tr and plural functions, and context.tr and context.plural:

lib/main.dart
Text('title').tr();
'msg'.tr(namedArgs: {'lang': 'Dart'});
tr('menu.open');
plural('day', 2);
context.tr('gender', gender: 'male');

These usages are title, msg, menu.open, day and gender. The generated LocaleKeys constants are not read.

easy_localization keeps plural and gender forms as nested keys. The preset adds the derived keys {key}.zero, {key}.one, {key}.two, {key}.few, {key}.many, {key}.other, {key}.male and {key}.female. For this file, plural('day', 2) uses day.one and day.other, so the unused-key check reports neither:

assets/translations/en-US.json
{
  "day": {
    "one": "{} day",
    "other": "{} days"
  }
}

Placeholders

Positional braces: {}. Braces: {name}. Linked messages: @:key and @.upper:key.

Extraction templates

KindsTemplate
any'{key}'.tr()