Translation

EZ Flutter supports the usage of multiple languages for a app.

Basics

Translation is handled by different .json files that contain the translation for each language supported by the app. The translation files are automatically loaded by the EzRunner at startup. Create a .json file with the pattern locale_$loacle.json under the locale folder for each supported language.

Example: locale_en.json && locale_de.json

//locale_en.json
{
  "SOME_MESSAGE": "This is some message!"
}
//locale_de.json
{
  "SOME_MESSAGE": "Dies ist eine Nachricht!"
}

Usage

Define supported languages

Add all supported locales to the EzRunner at startup.

EzRunner.run(CustomWidget(), [Locale('en'), Locale('de'), Locale('en')]);

Access translation

Access the translation within your app via the EzTranslator.

EzTranslator.of(context).text("SOME_MESSAGE");

Use placeholder in your translation

The translation feature offers the possibility to use placeholders inside your translations. Add placeholders to your translation that start with a $ sign.


{
  "WELCOME_MESSAGE": "Hello $fname $lname, how are you?"
}
//locale_de.json
{
  "WELCOME_MESSAGE": "Hallo $fname $lname, wie geht es dir?"
}

Add the optional parameter replace to the text() method.

EzTranslator.of(context).text("WELCOME_MESSAGE", replace: {"fname": "Jon", "lname": "Doe"});

Update translation files for missing translation labels

EZ Flutter provides an easy to use package for automatically detecting translations within your code and updating the extisting translation files at /locales with the missing labels.

dev_dependencies: ez_flutter_translation_generator: ^1.0.1

Run the following command in your command line:

flutter pub run build_runner build