yaml_localizations

A minimal YAML localization package for Flutter.

YAML is a human-readable, configuration file format with a minimal syntax, which allows you to represent strings as key/value pairs. String can be be both unquoted, quoted and span multiple lines. Both basic and literal strings are supported.

Consider toml_localizations, which does not depend on string indentation, or csv_localizations if you want to support multiple languages in a single file.

Usage

See example.

Install

Add to your pubspec.yaml

dependencies:
  yaml_localizations:

Add a YAML file per language

Add a YAML file per language you support in an asset path and describe it in your pubspec.yaml

flutter:
  assets:
    - {path}/{languageCode}.yaml
Example YAML file
Hi: Hi
Text: |
  There once was a tall man from Ealing
  Who got on a bus to Darjeeling
      It said on the door
      "Please don't sit on the floor"
  So he carefully sat on the ceiling
Long: >
  Wrapped text
  will be folded
  into a single
  paragraph

  Blank lines denote
  paragraph breaks

Tip: Yaml supports several ways of expressing strings. Use the vertical bar character to indicate that a string will span several lines. Use the greater-than character to break up long lines.

MaterialApp

Add YamlLocalizationsDelegate to MaterialApp and set supportedLocales using language codes.

MaterialApp(
  localizationsDelegates: [
    ... // global delegates
    YamlLocalizationsDelegate(
      YamlLocalizations(
        assetPath: 'yaml_translations',
        supportedLanguageCodes: [ 'en', 'nb', ],
      ),
    ),
  ],
  supportedLocales: [ Locale('en'), Locale('nb'), ],
}

API

Translate strings using

YamlLocalizations.of(context).string('Hi')

We keep the API simple, but you can easily add an extension method to String like this:

extension LocalizedString on String {
  String tr(BuildContext context) => YamlLocalizations.of(context).string(this);
}

Note on iOS

Add supported languages to ios/Runner/Info.plist as described here.

Example:

<key>CFBundleLocalizations</key>
<array>
	<string>en</string>
	<string>nb</string>
</array>

Libraries

yaml_localizations