toml_localizations 0.1.0+3 copy "toml_localizations: ^0.1.0+3" to clipboard
toml_localizations: ^0.1.0+3 copied to clipboard

outdated

A minimal TOML localization package for Flutter built on LocalizationsDelegate.

toml_localizations #

A minimal TOML localization package for Flutter.

Rationale #

TOML is a minimal, easy to read, configuration file format, which allows you to represent strings as key/value pairs. That is, as basic, multi-line basic, literal and multi-line literal strings. I believe TOML has the best format to represent a set of long, multiline and complex strings. Compared to YAML, strings are not indented, which is an advantage when working with text.

For simple strings consider csv_localizations, to support all languages in a single document - you can use both libraries!

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>
3
likes
0
pub points
40%
popularity

Publisher

verified publisherapptakk.com

A minimal TOML localization package for Flutter built on LocalizationsDelegate.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, toml

More

Packages that depend on toml_localizations