Simple and easy internationalization with Flutter - itell.solutions - Digital charmante Software Lösungen

Simple and easy internationalization with Flutter

Internationalization

General

Internationalization is an important part in the process of developing modern apps. It is absolutely necessary when you develop a multi-language app but it is also a good practice to prepare your app for proper internationalization even though you support only one language at the moment.
 
In general, internationalization is the process of designing and architecting your source code in a way that makes it easy to localize. The target is to structure your application so that it can be adapted for other languages or regions without having to change major parts of your source code. By translating text or adding locale-specific date formatting you are performing a localization of your internationalized application.
 
Translation of strings is only one aspect of localization but it might be the first obvious thing to do if you prepare your app for multiple languages.
 
There are major advantages using the principles, patterns and structures of proper internationalization in a single-language-app but it boils down to one significant point: retrieving any language-specific, hardcoded strings from source code by inserting another level of abstraction.
 
The target is to replace all strings with placeholders and move the strings to separate Files (for example *.arb or *.po-Files). These files are saved in a human-readable format and can be viewed in a text editor by developers and translators.
 

Internationalization with Flutter

The good news is: internationalization in Flutter is possible and well supported.
The bad news is: if you are doing it from scratch it is rather complex and it takes some time to get your head around it.
Even with the help of the packages flutter_localizations and intl it is quite complex because you have to create arb-Files at the right place, implement delegates and so on…
 

The solution

If you are developing your Flutter-application with Android Studio then internationalization becomes VERY easy.
There is a plugin for Android Studio which is called flutter_intl which is easy to use and creates all the boilderplate-code that is necessary for proper internationalization.

 

1. Install flutter_intl in Android Studio

2. Initialize plugin

First open your project and then initialize the plugin by running the “Initialize for the Project” command from Tools – Flutter Intl menu.
The plugin creates a file lib/l10n/intl_en.arb which is used for the default locale “en”.
Files inside the newly generated folder lib/generated/ should not be edited.

3. Add locale for german

Select Tools – Flutter Intl – Add locale and enter the name of the new locale. In this case “de”. The command then creates a new file: lib/l10n/.arb
 

4. Code integration

Add dependencies:

Adapt your top-level flutter-widget to configure internationalization:

 

5. Usage

First create a string inside your source-code as you would normally do it:
				
					return Text("GEMEINDE");
				
			
1. Position your cursor inside the string and press `ALT+ENTER`
2. Select option`Extract to ARB File`
3. Select a name for the variable that should replace the string

4. The Plugin will then replace the string with a variable.

				
					return Text(S.of(context).categoryNotFound);
				
			

5. The arb-File for each locale contains the variable for the given string. Here you can enter your translations.

And that’s it – you have successfully internationalized your Flutter application.

Want more?

For further information about our Software Development, find us on itell.solutions. For more information about this usefull plugin visit flutter_intl for Android Studio

You might also enjoy

Tausche Tarnfarbe gegen Python

Mit diesem Blogeintrag will ich dir die Angst nehmen, dich mit der IT zu beschäftigen und dich von dieser Karrieremöglichkeit überzeugen.