https://github.com/erf/toml_localizations
TOML localization for Flutter
https://github.com/erf/toml_localizations
dart flutter flutter-localization flutter-localizations localization toml translation
Last synced: 10 months ago
JSON representation
TOML localization for Flutter
- Host: GitHub
- URL: https://github.com/erf/toml_localizations
- Owner: erf
- License: mit
- Created: 2020-05-21T22:10:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-02-14T19:19:25.000Z (almost 2 years ago)
- Last Synced: 2025-03-09T02:50:30.914Z (10 months ago)
- Topics: dart, flutter, flutter-localization, flutter-localizations, localization, toml, translation
- Language: Dart
- Homepage: https://pub.dev/packages/toml_localizations
- Size: 187 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# toml_localizations
A minimal [TOML](https://github.com/toml-lang/toml) localization package for
Flutter.
TOML is a minimal, easy to read, configuration file format, which allows you to
represent [strings](https://github.com/toml-lang/toml#user-content-string) (and
other types) as key/value pairs.
## Install
Add `toml_localizations` and `flutter_localizations` as dependencies to your `pubspec.yaml`.
```yaml
dependencies:
flutter_localizations:
sdk: flutter
toml_localizations:
```
### Add assets
Add a TOML file per language to a asset path and define it in your `pubspec.yaml`.
```yaml
flutter:
assets:
- assets/toml_translations
```
The TOML file name must match exactly the combination of language and country
code described in `supportedLocales`.
That is `Locale('en', 'US')` must have a corresponding `assetPath/en-US.toml` file.
### Add localization delegate and supported locales
Add `TomlLocalizationsDelegate` to `MaterialApp` and set `supportedLocales` to supported language/country codes.
```Dart
MaterialApp(
localizationsDelegates: [
// delegate from flutter_localization
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
// toml localizations delegate
TomlLocalizationsDelegate(path: 'assets/toml_translations')
],
supportedLocales: [
Locale('en', 'GB'),
Locale('en', 'US'),
Locale('en'),
Locale('nb'),
],
}
```
### Note on **iOS**
Add supported languages to `ios/Runner/Info.plist` as described
[here](https://flutter.dev/docs/development/accessibility-and-localization/internationalization#specifying-supportedlocales).
Example:
```
CFBundleLocalizations
en
en-GB
en-US
nb
```
## Format
Example TOML file:
```toml
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.
'''
list = [ 'one', 'two', 'three' ]
```
> Tip: Toml supports several ways of expressing strings. See Toml documentation for more info.
### API
Translate strings or other types using:
```Dart
TomlLocalizations.of(context)!.value(this);
```
We keep the API simple, but you can easily add an extension method to `String` like this:
```Dart
extension LocalizedString on String {
String tr(BuildContext context) => TomlLocalizations.of(context)!.value(this);
}
```
Se you could translate strings like this:
```Dart
'Hi'.tr(context)
```
## Example
See [example](example)