How to quickly and easily localize an application on flutter. riverpod + slang

Hello. In this article, I want to share my knowledge on how to quickly localize an application on flutter. The foundation of this knowledge was laid in the development of a product called Weather Today.

As an introduction, I want to draw attention to the difference between the terms localization and internationalization. Internationalization (internationalization, i18n) is the process of developing an application so that it can be adapted to different languages ​​and regions without engineering changes. Localization (localization, L10n) is characterized as the process of adapting internationalized software for a particular region or language by translating text and adding region-specific components. The difference is that localization is done multiple times (for example, when a new language is added) and is based on an internationalization infrastructure that should be done once in well-designed software. Now I will show exactly the process of localization.

This material is part of a series of articles about creating an application weather today (Google Play) – a concise and free product for monitoring weather conditions in your smartphone.


This is how the official approach to flutter app localization looks like: “Internationalizing Flutter apps”. In addition, even more detailed material is offered: Flutter Internationalization User Guide. The official approach is quite verbose and wants a lot from the developer. Therefore, it was decided to pick up something more meek. And, most importantly, functional.

The options considered were as follows:

  • easy_localization link (v3.0.1 of May 13, 2022)

  • localization link (v2.1.0 of Feb 3, 2022)

  • fast_i18n link. Now it’s slang link(v3.12.0 of Feb 13, 2023).

Plastic bag localization too simple and not functional. No, well, almost nothing. However, I will note that the developer tried to make the package even better by providing an application for setting keys (link):

We have an application to help you configure your translation keys. The project is also open-source, so be fine if you want to help it evolve!

The easy_localizationperhaps the most popular package, however, out of the box it cannot achieve adequate reactivity even using BuildContext see issue 370. And this is outrageous for such a well-liked and publicized package. Nevertheless, it supports (judging by the description) various formats of stored translations (JSON, CSV, XML, Yaml), is able to pluralize, has some useful methods for flutter (reset locale, get device locale, etc.), there is code generation and even logger. Using it is quite simple (with code generation):

print(LocaleKeys.title.tr()); //String
//or
Text(LocaleKeys.title).tr(); //Widget

and without:

Text('title').tr(); //Text widget

print('title'.tr()); //String

var title = tr('title'); // Static function

At the same time, without forgetting:

  1. Run code generation with the command flutter pub run easy_localization:generate (by the way, I did not find the command watch)

  2. Add to main a couple of lines

    WidgetsFlutterBinding.ensureInitialized();
    await EasyLocalization.ensureInitialized();
  3. Wrap entire application in EasyLocalization with the right settings

  4. Add to MaterialApp few lines:

    Widget build(BuildContext context) {
      return MaterialApp(
      ...
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      locale: context.locale,
      ...
      );
    }

But all this was poorly combined with the fantasies of the author of this article. In my case it was important that:

  1. using the package was not frustrating and could clutter code

  2. was friendly and customized to use

  3. with good and (importantly) detailed documentation

  4. reactivity was maintained. This desire to break away BuildContext and use your state controller based on riverpod

  5. there was independence from the flutter framework (only dart). This feature would come in very handy, for example, in console applications, so that you don’t have to fence your bikes with localization.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *