Viewing version 0.13.1. Open the latest package page.
flutter_i18n
0.13.1i18n made easy for Flutter. With flutter_i18n you can make your app international, using just a simple .json file!
Readme
flutter_i18n
I18n made easy, for Flutter!
Why you should use flutter_i18n?
The main goal of flutter_i18n is to simplify the i18n process in Flutter. I would like to recreate the same experience that you have with the Angular i18n: simple json files, one for each language that you want to support.
Loaders
Loader is a class which loads your translations from specific source. You can easy override loader and create your own.
Available loaders:
| Class name | Purpose |
|---|---|
FileTranslationLoader |
Loads translation files from JSON or YAML format |
NetworkFileTranslationLoader |
Loads translations from the remote resource |
NamespaceFileTranslationLoader |
Loads translations from separate files |
E2EFileTranslationLoader |
Special loader for solving isolates problem with flutter drive |
FileTranslationLoader configuration
To use this library, you must create a folder in your project's root: the basePath. Some examples:
/assets/flutter_i18n (the default one)
/assets/i18n
/assets/locales
Inside this folder, you'll put the json or yaml files containing the translated keys. You have two options:
-
If you want to specify the country code
basePath/{languageCode}_{countryCode}.json -
otherwise
basePath/{languageCode}.json
If the json file is not available, we will look for a yaml file with the same name. In case both exist, the json file will be used.
Of course, you must declare the subtree in your pubspec.yaml as assets:
flutter:
assets:
- {basePath}
The next step consist in the configuration of the localizationsDelegates; to use flutter_i18n, you should configure as follows:
localizationsDelegates: [
FlutterI18nDelegate(translationLoader: FileTranslationLoader(...parameters...)),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
Below you can find the name and description of the accepted parameters.
The useCountryCode parameter depends on the json configuration:
- if you used the pattern {languageCode}_{countryCode}, useCountryCode must be true
- if you used the pattern {languageCode}, useCountryCode must be false
The fallbackFile parameter was entroduces with the version 0.1.0 and provide a default language, used when the translation for the current running system is not provided. This should contain the name of a valid json file in assets folder.
The basePath parameter is optionally used to set the base path for translations. If this option is not set, the default path will be assets/flutter_i18n. This path must be the same path as the one defined in your pubspec.yaml.
The forcedLocale parameter is optionally used to force a locale instead finding the system one.
If there isn't any translation available for the required key, the same key is returned.
NetworkFileTranslationLoader configuration
Behaviour of this loader very similar as FileTranslationLoader. The main difference that we load translations from NetworkAssetBundle instead of CachingAssetBundle.
Below you can find the name and description of the accepted parameters.
The baseUri parameter provide base Uri for your remote translations.
The useCountryCode parameter depends on the json configuration:
- if you used the pattern {languageCode}_{countryCode}, useCountryCode must be true
- if you used the pattern {languageCode}, useCountryCode must be false
The fallbackFile parameter provide a default language, used when the translation for the current running system is not provided.
The forcedLocale parameter is optionally used to force a locale instead finding the system one.
For example if your translation files located at
https://example.com/static/en.json you should configure as follows:
localizationsDelegates: [
FlutterI18nDelegate(translationLoader:
NetworkFileTranslationLoader(baseUri: Uri.https("example.com", "static")),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
NamespaceFileTranslationLoader configuration
Behaviour of this loader very similar as FileTranslationLoader. The main difference that we load translations from separate files per each language.
For example FileTranslationLoader format:
/assets/flutter_i18n/en.json
/assets/flutter_i18n/it.json
NamespaceFileTranslationLoader format:
/assets/flutter_i18n/en/home_screen.json
/assets/flutter_i18n/en/about_screen.json
/assets/flutter_i18n/it/home_screen.json
/assets/flutter_i18n/it/about_screen.json
Example configuration:
localizationsDelegates: [
FlutterI18nDelegate(translationLoader:
NamespaceFileTranslationLoader(namespaces: ["home_screen", "about_screen"]),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
Below you can find the name and description of the accepted parameters.
The namespaces provide a list of filenames for the specific language directory.
The useCountryCode parameter depends on the json configuration:
- if you used the pattern {languageCode}_{countryCode}, useCountryCode must be true
- if you used the pattern {languageCode}, useCountryCode must be false
The fallbackDir provide a default language directory, used when the translation for the current running system is not provided.
The basePath parameter is optionally used to set the base path for translations. If this option is not set, the default path will be assets/flutter_i18n. This path must be the same path as the one defined in your pubspec.yaml.
The forcedLocale parameter is optionally used to force a locale instead finding the system one.
E2EFileTranslationLoader configuration
The same as FileTranslationLoader configuration. This loader can be used for solving problem with flutter drive testing.
It removes using separate isolate for loading translations (detailed issue described here: issues/24703).
The useE2E parameter:
- if you are in flutter drive testing mode – must be true
- if you are in normal mode – must be false, in this case
FileTranslationLoaderwill be used
flutter_i18n in action
After the configuration steps, the only thing to do is invoke the following method:
FlutterI18n.translate(buildContext, "your.key")
Where:
- buildContext is the BuildContext instance of the widget
- your.key is the key to translate
Other examples of use:
Force a language to be loaded at run-time:
await FlutterI18n.refresh(buildContext, languageCode, {countryCode});
Plural translations:
FlutterI18n.plural(buildContext, "your.key", pluralValue);
Text widget shorthand:
I18nText("your.key", child: Text(""))
I18nText("your.key", translationParams: {"user": "Flutter lover"})
I18nPlural("clicked.times", 1)
I18nPlural("clicked.times", 2, child: Text(""))
For more informations and details, read the CHANGELOG.md.
Known problems:
- In iOS simulator, is always used the en_US locale: see here.
Changelog
[0.2.0]
flutter_i18n now manage strings that contain parameters; an example can be: "Hello, {user}!" For a correct translation, you must use the third parameter of the translate method, a Map<String, String> where:
- the keys are the placeholders used in the .json file (i.e. user)
- the values are what you want to display
[0.3.0]
flutter_i18n now supports language change at runtime. To use it, you must invoke the method
await FlutterI18n.refresh(buildContext, languageCode, {countryCode});
NOTE: countryCode is optional.
[0.4.0]
flutter_i18n now supports plurals. To use it, invoke the method:
FlutterI18n.plural(buildContext, "your.key", pluralValue);
Where pluralValue is the integer value that will be used to determinate the plural form of the translation.
Here is an example of configuration of the .json file:
"clicked": {
"times-0": "You clicked {times} times!",
"times-1": "You clicked {time} time!",
"times-2": "You clicked {times} times!"
}
With this configuration, you must invoke the plural method as follow:
FlutterI18n.plural(buildContext, "clicked.times", pluralValue);
FlutterI18n will choose the right key using the value of pluralValue: it will match the last key with a value <= of pluralValue.
[0.5.0]
flutter_i18n now supports the basePath configuration.
The default one is: assets/flutter_i18n.
To configure it, use the third optional parameter of FlutterI18nDelegate.
[0.5.1]
Print in log when a key is not found.
[0.5.2]
Fix for bug #10.
[0.6.0]
Exposed new method that return the locale used by the library, as requested in #16:
FlutterI18n.currentLocale();
NOTE: the refresh method now accept a Locale as second parameter, instead of two strings. NOTE: the constructor of FlutterI18nDelegate now accept only named parameter.
[0.6.1]
Fix for bug #22
[0.6.2]
Fix for the delegate
[0.6.3]
Fix for #17
[0.6.4]
Fix for #41
[0.7.0]
Fix for #48 and implemented feature for #49. From this version of FlutterI18n, you can use as 4th parameter the forcedLanguage parameter, in order to avoid to call the refresh method on application startup.
[0.8.0]
Support for yml language files
[0.8.1]
Improved json and yml loading
[0.8.2]
Bugfix and code improvement
[0.8.3]
Library will print debug messages only in development mode
[0.9.0]
Added a new optional parameter to the translate method: fallbackKey. You can use it when the translation key is not found.
[0.10.0]
Added stateless widgets in order to avoid to deal with BuildContext.
I18nText("your.key", Text(""))
I18nText("your.key", Text(""), translationParams: {"user": "Flutter lover"})
I18nPlural("clicked.times", 2, Text(""))
[0.11.0]
New translation mechanism, with different and customizable loaders provided:
- FileTranslationLoader
- NetworkFileTranslationLoader
- E2EFileTranslationLoader
[0.11.1]
Plural translator improvements
[0.12.0]
New loader provided: NamespaceFileTranslationLoader
[0.13.0]
In the I18nText and I18nPlural widgets, the child parameter is now optional.
The default value of the child parameter is now Text("")
[0.13.1]
Fix for #83.
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'flutter_i18n:0.13.1'pubspec.yaml
name: flutter_i18n
description: i18n made easy for Flutter. With flutter_i18n you can make your app international, using just a simple .json file!
version: 0.13.1
homepage: https://github.com/ilteoood/flutter_i18n
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
intl: ^0.16.0
yaml: ^2.2.0
dev_dependencies:
flutter_test:
sdk: flutter
environment:
sdk: '>=2.3.0 <3.0.0'Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.1 | Cached · Upstream SHA-256 matched | Download | bc62ed0c…6b4262 |
| 0.0.2 | Cached · Upstream SHA-256 matched | Download | 53d2a591…76baee |
| 0.0.3 | Cached · Upstream SHA-256 matched | Download | 4f262b0d…c3d320 |
| 0.1.0 | Cached · Upstream SHA-256 matched | Download | c8a7e81b…80768b |
| 0.2.0 | Cached · Upstream SHA-256 matched | Download | 74c6b69c…045ed4 |
| 0.3.0 | Cached · Upstream SHA-256 matched | Download | 4eba6520…f1c244 |
| 0.4.0 | Cached · Upstream SHA-256 matched | Download | 32557618…ba7775 |
| 0.5.0 | Cached · Upstream SHA-256 matched | Download | 2b118d26…b206e5 |
| 0.5.1 | Cached · Upstream SHA-256 matched | Download | 7cfdec9f…4888b4 |
| 0.5.2 | Cached · Upstream SHA-256 matched | Download | e3fcf081…0ebfcc |
| 0.6.0 | Cached · Upstream SHA-256 matched | Download | 65c9167c…f84f62 |
| 0.6.1 | Cached · Upstream SHA-256 matched | Download | de342cf3…7fb15e |
| 0.6.2 | Cached · Upstream SHA-256 matched | Download | 620a8242…5d4929 |
| 0.6.3 | Cached · Upstream SHA-256 matched | Download | 95d2ec26…a2f65e |
| 0.6.4 | Cached · Upstream SHA-256 matched | Download | 9b0231b6…0f9de4 |
| 0.7.0 | Cached · Upstream SHA-256 matched | Download | 4e21b4d8…850d78 |
| 0.8.0 | Cached · Upstream SHA-256 matched | Download | 88a9bab0…622bff |
| 0.8.1 | Cached · Upstream SHA-256 matched | Download | 61dc2387…9d4a4c |
| 0.8.2 | Cached · Upstream SHA-256 matched | Download | 4f1217f1…a0305c |
| 0.8.3 | Cached · Upstream SHA-256 matched | Download | 411807a5…403fd6 |
| 0.9.0 | Cached · Upstream SHA-256 matched | Download | 84b7bb76…6f1f2e |
| 0.10.0 | Cached · Upstream SHA-256 matched | Download | 79bb2c69…9b5f61 |
| 0.10.1 | Cached · Upstream SHA-256 matched | Download | f862efcd…113a8d |
| 0.11.0 | Cached · Upstream SHA-256 matched | Download | 575ab368…85f14d |
| 0.11.1 | Cached · Upstream SHA-256 matched | Download | 5ea04898…f7e32e |
| 0.12.0 | Cached · Upstream SHA-256 matched | Download | 8b93a285…d0bc7e |
| 0.13.0 | Cached · Upstream SHA-256 matched | Download | c6391b80…6c927c |
| 0.13.1 | Cached · Upstream SHA-256 matched | Download | c48f9f5e…8b6bd5 |
| 0.13.2 | Cached · Upstream SHA-256 matched | Download | fb23273a…6f6aea |
| 0.14.0 | Cached · Upstream SHA-256 matched | Download | 8eda74b3…23c255 |
| 0.15.0 | Cached · Upstream SHA-256 matched | Download | 0b8452f6…b81b80 |
| 0.16.0 | Cached · Upstream SHA-256 matched | Download | 3821b4a2…d770cb |
| 0.16.1 | Cached · Upstream SHA-256 matched | Download | 92d94b69…79e702 |
| 0.16.2 | Cached · Upstream SHA-256 matched | Download | a1578783…e42e4d |
| 0.16.3 | Cached · Upstream SHA-256 matched | Download | 480a953b…17aeab |
| 0.17.0 | Cached · Upstream SHA-256 matched | Download | e087afc1…5858bc |
| 0.18.0 | Cached · Upstream SHA-256 matched | Download | d1659c8b…4d2f18 |
| 0.19.0 | Cached · Upstream SHA-256 matched | Download | fa476f0a…79426c |
| 0.19.1 | Cached · Upstream SHA-256 matched | Download | f0a5815e…cac246 |
| 0.19.2 | Cached · Upstream SHA-256 matched | Download | 6ec2e77e…199745 |
| 0.19.3 | Cached · Upstream SHA-256 matched | Download | 548e327f…b56326 |
| 0.19.4 | Cached · Upstream SHA-256 matched | Download | 9532ebbc…6e732e |
| 0.20.0 | Cached · Upstream SHA-256 matched | Download | ecef39ae…defb5b |
| 0.20.1 | Cached · Upstream SHA-256 matched | Download | 9b1507fd…d995fa |
| 0.21.0-intl.nullsafety | Cached · Upstream SHA-256 matched | Download | 3baaed18…dbd00a |
| 0.21.0 | Cached · Upstream SHA-256 matched | Download | 5322101a…054f68 |
| 0.21.1 | Cached · Upstream SHA-256 matched | Download | 139750e7…51ea63 |
| 0.22.0 | Cached · Upstream SHA-256 matched | Download | 9f2455cc…cabd5a |
| 0.22.1 | Cached · Upstream SHA-256 matched | Download | 0ff3150a…420d8c |
| 0.22.2 | Cached · Upstream SHA-256 matched | Download | 4bf68422…75d59b |
| 0.22.3 | Cached · Upstream SHA-256 matched | Download | 94aed869…20f07b |
| 0.22.4 | Cached · Upstream SHA-256 matched | Download | ae96d3f6…33d5ad |
| 0.30.0 | Cached · Upstream SHA-256 matched | Download | 5f2203e5…1b5418 |
| 0.31.0 | Cached · Upstream SHA-256 matched | Download | 454d2c4e…56810d |
| 0.31.1 | Cached · Upstream SHA-256 matched | Download | 3dc20ca9…4ebcf8 |
| 0.32.0 | Cached · Upstream SHA-256 matched | Download | 3f498993…ddc25b |
| 0.32.1 | Cached · Upstream SHA-256 matched | Download | d0dbf49b…6bac44 |
| 0.32.2 | Cached · Upstream SHA-256 matched | Download | 3eeb3d57…69725c |
| 0.32.3 | Cached · Upstream SHA-256 matched | Download | 143b397a…58d269 |
| 0.32.4 | Cached · Upstream SHA-256 matched | Download | 5f453b25…25af66 |
| 0.33.0 | Cached · Upstream SHA-256 matched | Download | b71fe887…f20363 |
| 0.34.0 | Cached · Upstream SHA-256 matched | Download | c7e9752c…b29636 |
| 0.35.0 | Cached · Upstream SHA-256 matched | Download | 091af697…bdcfa6 |
| 0.35.1 | Cached · Upstream SHA-256 matched | Download | c823a9c9…05f1c2 |
| 0.36.0 | Cached · Upstream SHA-256 matched | Download | 0e88d479…ec5062 |
| 0.36.1 | Cached · Upstream SHA-256 matched | Download | eed5e5d2…4663bc |
| 0.36.2 | Cached · Upstream SHA-256 matched | Download | 37334bfa…44be46 |
| 0.36.3 | Cached · Upstream SHA-256 matched | Download | fe2bd740…66570c |
| 0.37.0 | Cached · Upstream SHA-256 matched | Download | 9c2e4cbf…9f56b8 |
| 0.37.1 | Cached · Upstream SHA-256 matched | Download | af7d625e…7bd9b6 |
Dependencies
flutter {
"sdk": "flutter"
}
flutter_localizations {
"sdk": "flutter"
}
intl "^0.16.0"
yaml "^2.2.0"