10.2. Internationalization and localization in Flutter: Environment configuration for internationalization
Page 135 | Listen in audio
Internationalization and localization are fundamental aspects in application development, as they allow the application to be used by people from different regions and who speak different languages. Flutter and Dart offer powerful tools to facilitate internationalization and localization of applications. In this text, we will learn how to configure the environment for internationalization in Flutter.
Before you start, it's important to understand what internationalization and localization are. Internationalization (i18n) is the process of designing and preparing your application to be used in different languages and regions. Localization (l10n) is the process of translating internationalized content and adapting the application for specific regions.
Environment configuration for internationalization
To start internationalization in Flutter, we need to do some initial setup. First, you'll need to add the flutter_localizations dependency to your pubspec.yaml file:
dependencies: flutter: sdk: flutter flutter_locations: sdk: flutter
Next, you'll need to import the flutter_localizations package into your main.dart file and add the localizations the app will support:
import 'package:flutter_localizations/flutter_localizations.dart'; void main() { runApp(MaterialApp( localizationsDelegates: [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, ], supportedLocales: [ const Locale('en', 'US'), // English const Locale('es', 'ES'), // Spanish // ... other locales the app supports ], )); }
Location delegates are classes that produce localized collections of specific values. GlobalMaterialLocalizations.delegate provides localized strings and layouts for the Material Components library. GlobalWidgetsLocalizations.delegate defines the default directional text for the MaterialApp widget.
Now, you need to create a Dart file for each language that the application will support. Each Dart file must contain a class that extends Localizations and implements all the strings that will be used in the application. For example, for English, you can create a file called app_localizations_en.dart:
class AppLocalizationsEn extends Localizations { // ... put your localized strings here }
For each string you want to find, you need to add a getter property. For example, if you have a string "hello", you can add the following code:
String get hello => "Hello";
To use the localized string in your application, you can use the Localizations.of() method. For example:
Text(Localizations.of(context, AppLocalizationsEn).hello)
This will display the string "Hello" if the current application language is English.
To add support for another language, you need to create another Dart file and another class that extends Localizations. For example, for Spanish you can create a file called app_localizations_es.dart and add the following class:
class AppLocalizationsEs extends Localizations { // ... put your localized strings here }
Next, you need to add the new location to the app's supported list:
supportedLocales: [ const Locale('en', 'US'), // English const Locale('es', 'ES'), // Spanish const Locale('fr', 'FR'), // French // ... other locales the app supports ],
In summary, internationalization and localization in Flutter involves configuring the environment to support multiple languages, creating Dart files for each language with classes that extend Localizations, and using the Localizations.of() method to display localized strings . This process allows you to create applications that can be used by users from different regions and who speak different languages.
Now answer the exercise about the content:
What is needed to configure internationalization in Flutter?
You are right! Congratulations, now go to the next page
You missed! Try again.
Next page of the Free Ebook: