AssetLocalizations
Localization support using assets files.
Use translate to translate any text.
All translations should be put in the assets/lang/ folder as json files, one for each local (e.g., en.jsonor da.json). Note that only the languageCode of a Locale is used.
Remember to add the assets/lang/ folder to the list of assets in the pubspec.yaml file like this:
flutter:
assets:
- assets/lang/
...
Translations may be nested, and are addressed with a dot-separated path. These two files are equivalent:
{ "pages": { "task_list": { "title": "Tasks" } } }
{ "pages.task_list.title": "Tasks" }
A translation can contain {{placeholder}} values, filled in from the args of translate, and a key can be given per plural category using the _zero, _one, _two, _few, _many and _other suffixes, selected by the count of translate:
{
"greeting": "Hello {{name}}",
"tasks_one": "{{count}} task left",
"tasks_other": "{{count}} tasks left"
}
- Implementers
Constructors
AssetLocalizations
- Locale locale
Create an assets localization based on locale.
Constants
delegate
A default LocalizationsDelegate for AssetLocalizations.
This default delegate loads translations from the assets/lang file path.
Properties
filename
The file name of the localization asset.
locale
translations
A map of available translations for this locale.
Nested translations are flattened into dot-separated keys, so this map is always flat regardless of how the translations were written.
Methods
canTranslate
Can this key be translated by this localization?
flatten
Flattens a - possibly nested - map of translations into a flat map whose keys are dot-separated paths.
flatten({'pages': {'task_list': {'title': 'Tasks'}}});
// {'pages.task_list.title': 'Tasks'}
A map which is already flat is returned unchanged, which is what makes nested and dot-separated translation files interchangeable. Values which are neither a map nor a string are converted with toString().
load
of
- BuildContext context
Returns the localized resources object of type AssetLocalizations for the widget tree that corresponds to the given context.
Returns null if no resources object of type AssetLocalizations exists within the given context.
translate
Translate key to this locale.
If key is not translated, key is returned 'as-is'. This makes it safe to pass text which may be either a translation key or a literal.
Nested translations are addressed with a dot-separated path, e.g. pages.task_list.title.
If args is given, every {{placeholder}} in the translation is replaced with the matching entry. A placeholder with no matching entry is left in place, so that a forgotten argument is visible rather than silently blank.
translate('greeting', args: {'name': 'Bo'}); // 'Hello Bo'
If count is given, the plural form of key is used - that is the key suffixed with the plural category of count in this locale, one of _zero, _one, _two, _few, _many or _other. count is also available to the translation as {{count}}, without having to pass it in args as well.
translate('tasks', count: 3); // '3 tasks left' from 'tasks_other'
The categories which apply depend on the language - English only ever uses _one and _other - except for _zero, which is used for a count of exactly 0 in any language when present. A category which is not translated falls back to _other, and a key with no plural forms at all falls back to key itself.