Multi-Language support in Flutter

Kinjal Dhamat
Flutter Community
Published in
2 min readOct 14, 2020

--

Multi-language support is very important to globalize Application. There are many tutorials that provide a solution for multiple languages in a flutter. But I found a very easy way to support multiple languages. Here we do not need to use language JSON files instead we can create an abstract String class.

This is the final output of this tutorial. You can find the source code from Github.

Getting Started:

Add dependency:

We need to add flutter_localizations as a dependency to your pubspec.yaml file. This package supports 77 languages.

dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter

Create Strings file:

Create an abstract class which contains all localized strings. We need to define all localized texts in different language string files.

Now create other Languages class that support your application and extend Languages class and overrides all strings.

Create LocalizationsDelegate

AppLocalizationsDelegate is the main file for localization. Create a class and extend LocalizationsDelegate and give our created Languages class in a generic parameter.

We need to override three methods.

First, is the isSupported method which returns bool values. we give an array of language and check that selected Locale language is contained in an array or not.

Second, is the load method which returns the Languages class based on selected Locale language.

Third, is the shouldReload method returns bool values. It returns true if the resources for this delegate should be loaded again by calling the load method.

Configuration in the main file:

We need to create StatefulWidget for MyApp because when changing language at that time whole application rebuild and application language is changed. We need to set a few properties in MaterialApp Widget.

We can define all supported language in an array of Locale in supportedLocales property.

supportedLocales: [
Locale('en', ''),
Locale('ar', ''),
Locale('hi', '')
]

The full source code of the main file is below.

There are some useful methods like store language in SharedPreference and get Locale which we used in the main class. I have defined it in the locale_constant.dart file.

That’s It. In this way, we can easily implement multiple language support and not need to work with messy JSON string format. I hope you enjoy this tutorial.

https://www.twitter.com/FlutterComm

--

--

Kinjal Dhamat
Flutter Community

Flutter | FlutterFlow | Dart | Android | Kotlin | React Native | Node | Lead Software Engineer