A minimal TOML localization package for Flutter

  packages, Packages

toml_localizations

A minimal TOML localization package for Flutter.

Usage

See example.

Install

Add to your pubspec.yaml

dependencies:
  toml_localizations:

Add a TOML file per language

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

flutter:
  assets:
    - {path}/{languageCode}.toml
Example TOML file
str = "The quick brown fox jumps over the lazy dog."

literal_str = 'C:\Users\nodejs\templates'

multiline_str = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""

literal_multiline_str = '''
The first newline is
trimmed in raw strings.
   All other whitespace
   is preserved.
'''

Tip: Toml supports several ways of expressing strings. See Toml documentation for more info.

MaterialApp

Add TomlLocalizationsDelegate to MaterialApp and set supportedLocales using language codes.

MaterialApp(
  localizationsDelegates: [
    ... // global delegates
    TomlLocalizationsDelegate(
      TomlLocalizations(
        assetPath: 'toml_translations',
        supportedLanguageCodes: [ 'en', 'nb', ],
      ),
    ),
  ],
  supportedLocales: [ Locale('en'), Locale('nb'), ],
}

API

Translate strings using

TomlLocalizations.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) => TomlLocalizations.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>

Download source code on GitHub

https://github.com/erf/toml_localizations