Flutter Internationalization
When you create a mobile app and it hits the international market, you have to internationalize the app which is an important part of app development. Let discuss how to implement internationalization in Flutter.
Add following dependency by below commands
flutter pub add flutter_localizations --sdk=flutter
flutter pub add intl:any
These command will add the following dependecy in Pubsec.yaml
dependencies:
flutter:
flutter_localizations:
sdk: flutter
intl: any
Now add a new yaml file in the root directory with the name i18n.yaml and add the following codes.
arb-dir: lib/l10n
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart
create a folder in the lib directory with the name I10n and then create a file named app_en.arb for english and app_es.arb for spanish and add following codes.
for english
{
"helloWorld": "Hello World!",
"title": "Flutter demo",
"you_have_click_button_this_many_time": "You have clicked button this many times",
"flutter_demo_page": "Flutter Demo Home Page",
"increment": "Increment"
}
for spanish
{
"helloWorld": "?Hola Mundo!",
"title": "Demostración Flutter",
"you_have_click_button_this_many_time": "Has hecho clic en este botón muchas veces.",
"flutter_demo_page": "Página de demostración flutter",
"increment": "INCREMENTO"
}
Now run command
flutter gen-l10n
to generate the translation
now apply the translation
to Widget
var localization =AppLocalizations.of(context);
Text(localization!.title);
that's done .
like that you can enjoy Internationalization in your app. You can checkout the example in github with given below repository.
bye bye enjoy coding