Localization
Localization (l10n) is done through the Flutter intl package. This page shows how to create localized entries, parametrize them and access them in code after generating localization files. Also, some project-individual localization features are shown.
Create and Access Localizations
Localization ARB files can be found in lib/l10n/ folder. Localized entries are created as key-value-pairs, looking like this:
After localizations have been edited, corresponding Dart files have to be re-generated using the following command:
A localized entry is loaded using S.of(context), suffixing the name of the key like that:
One can add parameters to a localized entry by writing a parameter in curly braces. In such cases, a descriptive localization entry should be created, emphasizing the parameter's meaning:
Parameters are passed to the key when accessing the localized item:
LocaleController
The LocaleController is the central class for handling all logical operations related to localization within the app. The controller can be accessed anytime using the widget tree over the current BuildContext like so:
There are some interesting data you may access:
locale: Returns the currentLocaleobject, reflecting the selected language.currentLocaleIsGerman: Utility to quickly access whether the user currently has a German language selection entry.translations: Loads current localized entries without the need ofBuildContext. This can be useful when accessing localizations in static repositories.
TranslatedText
TranslatedText is a utility model reflecting API-sent entries which are available in multiple languages. Each TranslatedText therefore stores any amount of Translation-entries, each reflecting a specific translation text with its corresponding languageCode.
TranslatedText offers the following utilities:
translations: The full list of allTranslationitems.getTextForLanguageCode: Returns the relevant translation for a given locale code. If there is no fitting translation, the English localization is returned. If there is no translation at all, returnsnull.getTextForControllerLanguage: Same asgetTextForLanguageCode, but considers the locale currently set in theLocaleController.getTextForCurrentLanguage: Same asgetTextForControllerLanguage, but loads theLocaleControllerthrough the passedBuildContext.german: Same asgetTextForLanguageCode, but tries to load the German entry.english: Same asgetTextForLanguageCode, but tries to load the English entry.
There also is a service model implementation for the HSMWmobil app API called AppApiTranslatedText with a fitting fromJson constructor.