Sitemap
Ali Rajab

Ali Rajab

Software Engineer interested in new technologies and how to use them to solve real-world problems.

Flutter Localization

3 min readAug 7, 2023

Localization is one of many factors that affect your app's popularity because it’s targeting more users who speak different languages and that will help users to use your app smoothly and understand the content clearly.

To allow your Flutter app to support many languages based on customized texts you can do that by following the steps:

1- Add the flutter_localizations package to your project, this package allows in-app localization with map data:

flutter pub add flutter_localizations --sdk=flutter

2- Add intl package that provides internationalization and localization facilities, including message translation, plurals and genders, date/number formatting and parsing, and bidirectional text.

flutter pub add intl:any

3- Open the pubspec.yaml file and enable the generate flag. This flag is found in the flutter section in the pubspec file.

# The following section is specific to Flutter.
flutter:
generate: true # Add this line

4- Add a new yaml file to the root directory of the Flutter project. Name this file l10n.yaml and include the following content:

arb-dir: lib/languages/I10n 
template-arb-file: app_en.arb
output-localization-file: app_localizations.dart

5- Create your App Resource Bundle files(based on JSON) to customize your messages for each language:

— lib
— — langugaes
— — — I10n
— — — — app_ar.arb
— — — — app_en.arb
— — — — app_es.arb

  • app_ar.arb:
{
"hello": "!مرحباً"
}
  • app_en.arb:
{
"hello": "Hello"
}
  • app_es.arb:
{
"hello": "Hola!"
}

6- Inside your Material or Cupertino app import the flutter_localizations library and specify localizationsDelegates and supportedLocales :

MaterialApp(
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: [
Locale('ar'), // Arabic
Locale('en'), // English
Locale('es'), // Spanish
],
home: HomeScreen()
);

7- Now, run flutter run and codegen takes place automatically. You should find generated files in ${FLUTTER_PROJECT}/.dart_tool/flutter_gen/gen_l10n. Alternatively, you can also run flutter gen-l10n to generate the same files without running the app.

  • After running your app add AppLocalizations.delegate to your localizationsDelegates
MaterialApp(
localizationsDelegates: const [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],

Get Ali Rajab’s stories in your inbox

Join Medium for free to get updates from this writer.

8- To access your customized messages anywhere inside the app you should first import the generated class by codegen (AppLocalizations):

import 'package:flutter_gen/gen_l10n/app_localizations.dart';

Then you call it this way:

import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(AppLocalizations.of(context)!.helloWorld),
),
);
}
}
Press enter or click to view image in full size
  • Note: Now you can replace
supportedLocales:[                    
Locale('ar'), // Arabic
Locale('en'), // English
Locale('es'), // Spanish
],

by supportedLocales: AppLocalizations.supportedLocales,

To deal with singular and plural you can do that by following the steps:

1- Declare the key inside your arb files as follows:

           
"numOfWorkers": "{count, plural, =1{1 worker} other{{count} workers}}",
"@numOfWorkers": {
"description": "Number of workers",
"placeholders": {
"count": {
"example": "1"
}
}
}
  • If the number of workers 1 it will display 1 worker otherwise it will display (number of workers) workers
  • Example :
 Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(AppLocalizations.of(context)!.hello),
Text(AppLocalizations.of(context)!.numOfWorkers(1)),
Text(AppLocalizations.of(context)!.numOfWorkers(5))
],
),

If you have set of values and you want to choose the correct localization based on the value you are receiving ( as an example a value from the server) you can do that also:

1- Declare the key inside your arb files as follows:

 "workType": "{workType, select, Permanent{Permanent} Contract{Contract} other{Unknown}}",
"@workType": {
"description": "work Type",
"placeholders": {
"workType":{}

}
}
  • Now you can call it this way:
Text(AppLocalizations.of(context)!.workType("contract")),
Text(AppLocalizations.of(context)!.workType("permanent")),
Text(AppLocalizations.of(context)!.workType("part-time")),
  • The final code and result:
Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(AppLocalizations.of(context)!.helloWorld),
Text(AppLocalizations.of(context)!.numOfWorkers(1)),
Text(AppLocalizations.of(context)!.numOfWorkers(5)),
Text(AppLocalizations.of(context)!.workType("contract")),
Text(AppLocalizations.of(context)!.workType("permanent")),
Text(AppLocalizations.of(context)!.workType("part-time")),
],
),
),
);
Press enter or click to view image in full size
  • I hope this information was helpful for more information follow me on LinkedIn and feel free to ask me any question you have directly or by email: alirajab.dev@gmail.com
  • My channel on Youtube
  • Source code: GitHub

--

--

Ali Rajab
Ali Rajab

Written by Ali Rajab

Software Engineer interested in new technologies and how to use them to solve real-world problems.