10.3. Internationalization and Localization in Flutter: String Translation
Page 136 | Listen in audio
Internationalization and localization are essential aspects of app development, especially if your target audience is global. In this context, Flutter, together with its Dart programming language, offers an efficient and effective way to translate strings and adapt your application for different markets and cultures. This process is known as internationalization (i18n) and localization (l10n).
Internationalization is the process of designing and preparing your application for use in multiple languages and regions. This involves extracting text strings from your code and preparing your application to display the translated content. On the other hand, localization is the process of translating your app content and adapting it to meet the cultural and legal expectations of a specific market.
Flutter makes it easy to internationalize and localize your app by providing a number of tools and libraries. To get started, you need to add the necessary dependencies to your pubspec.yaml file. This includes the 'flutter_localizations' library which provides classes and methods for translating strings and the 'intl' library which provides date, time and number formatting functionality.
1. Configuring Internationalization
After adding the dependencies, you need to configure the internationalization of your application. This involves defining which languages and regions your app will support. In Flutter, you can do this using the 'Locale' class. For example, if you want your application to support both English and Spanish, you can do something like this:
supportedLocales: [
const Locale('en', 'US'), // American English
const Locale('es', 'ES'), // Spanish Spain
]
2. String Translation
String translation is an important part of internationalization and localization. In Flutter, you can translate strings using the 'Intl' class. For each string you want to translate, you need to create a new instance of 'Intl' and pass the original string as an argument. For example:
String welcomeMessage = Intl.message('Welcome', name: 'welcomeMessage');
After creating the instance, you can use the 'of' method to get the translation of the string in the current language. For example:
Text(Intl.of(context).welcomeMessage);
3. Language Resource Files
For each language your application supports, you need to create a language resource file. This is a dart file that contains all your string translations. You can create this file manually or use the 'intl_translation' tool to generate it automatically.
4. Selecting Current Language
To select the current language, you can use the 'Locale' class. You can get the current language using the 'localeResolutionCallback' method. For example:
localeResolutionCallback: (Locale locale, Iterable supportedLocales) {
if (locale == null) {
return supportedLocales.first;
}
for (Locale supportedLocale in supportedLocales) {
if (supportedLocale.languageCode == locale.languageCode){
return supportedLocale;
}
}
return supportedLocales.first;
}
In summary, internationalization and localization are essential aspects of developing applications that target a global audience. Flutter offers an efficient and effective way to translate strings and adapt your app for different markets and cultures. By following these steps, you can ensure that your app will be well received by users around the world.
Now answer the exercise about the content:
What is internationalization and localization in the context of application development?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: