Internationalization

Flutter comes with a flexible internationalization system.
You have choice between using intl or i18n.
We recommend you to use i18n because it's more flexible and more powerful.

As we will modify our main file we recommand to run this as soon as you start the project.

We use slang to manage our translations.
It allows us to generate a dart file containing all our translations and to use it in our project. You have the choice of the format and it contains a complete command line to find missing translations or unused translations...

Here is the complete documentation of slang: https://pub.dev/packages/slang

Getting started

👋 The setup command will ask you if you want to use internationalization but you can also run it manually.

Using our CLI we can add everything we need to start using internationalization in our project.

dart pub global run apparence_cli translations .

This will install the following dependencies:

  • slang
  • slang flutter

We will also modify our main file to add the translation provider and run the Material App with the translations configured.

How to use it (fast version)

  • You translations will be in the i18n folder.
  • Your main language translations are be in the i18n/strings.i18n.json file.

Once you have a translation key in your file run the following command to generate the dart file containing all your translations:

Using the Slang CLI (prefer this method):

dart run slang

Configuration file

The configuration file is slang.yaml. It is placed at the root of your project.

By default we have the following configuration:

# slang.yaml
base_locale: en # The main language of your app 
fallback_strategy: base_locale # The fallback strategy (fallback to the main language)
input_directory: lib/i18n # The folder containing your translations
input_file_pattern: .i18n.json # The file extension of your translations
output_directory: lib/i18n # The folder where the dart file will be generated
output_file_name: translations.g.dart # The name of the dart file generated
output_format: single_file 

Add a language

  • For every language you need create a translation file like i18n/string_fr.i18n.json folder.

Don't forget to add every language to iOS support

Open File: ios/Runner/Info.plist with XCode and add the following lines for every language you want to support:

<key>CFBundleLocalizations</key>
<array>
   <string>en</string>
   <string>fr</string>
</array>

Use the translations

You can now use the translations in your project.

for example if we have a translation file with

{
  "home": {
    "title": "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
  }
}

We can use it in our project like this:

Translations.of(context).home.title,

For more complex usage please refer to the slang documentation https://pub.dev/packages/slang