Flutter: application localization using Android Studio

  • Tutorial


Quite a lot has already been written about the localization (internationalization) of Flutter applications.
The official documentation dwells on this issue in some detail, in addition to this, a number of enthusiasts describe a variety of approaches .


This article does not pretend to be exhaustive, but is a description of a practice that, in my opinion, very effectively reduces manual input.


We are talking about localization using flutter_i18n , an Android Studio plugin that can be used to get rid of routine work and boilerplate code.


Training


Install the flutter-i18n plugin first


image


After rebooting, the generated / i18n.dart and res / values ​​/ strings_en.arb files will automatically be added to the project structure


image


( I manually added the strings_ru.arb file for the Russian language)


Please note that Android Studio 3.3.1 - 3.4 contains some kind of jamb, because of which localization files are not generated. I described this problem on stackoverflow, and got a solution . Another solution is to temporarily stay on AS 3.2, but this is an amateur.


You will also need to install the flutter_localizations package to localize the internal Flutter widgets (this will be done for you at the system level).


dev_dependencies:
  flutter_localizations:
    sdk: flutter

After that, it will be enough for you to add localization parameters to the constructor:


    return MaterialApp(
      localizationsDelegates: [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
      ],
      supportedLocales: S.delegate.supportedLocales,

and do not forget to import the package


import 'package:f_latte/generated/i18n.dart';

Localization


Just start adding the necessary lines, observing the JSON notation, like this:


strings_en.arb:


{
  "title": "F-Latte",
  "greeting": "Hi, $name!",
  "pushingZero": "You pressed NONE (",
  "pushingOne": "You pressed once",
  "pushingTwo": "You pressed twice",
  "pushingOther": "You pressed $cnt times",
}

By the way, the plugin implements the logic of working with plurals ( plurals ), for this you should name keys in arb files in a certain way. How this works you will understand from the example code.


To add languages, just add the necessary files to the same package, and add localized strings with the same keys there, like this:


strings_ru.arb:


{
  "title": "F-Латте",
  "greeting": "Привет, ${name}!",
  "pushingZero": "Ничего не нажималось",
  "pushingOne": "Нажато разок",
  "pushingTwo": "Нажато дважды",
  "pushingOther": "Нажато $cnt раз(а)"
}

With any changes in these files, the plugin will rebuild the i18n.dart file . By the way, I recommend at least viewing it, much will become clearer.


That's all. In any place where access to localized strings is required, simply call the static localizer method:


/// так
title: Text(S.of(context).title),
...
/// или так
Text(S.of(context).greeting('User'), ),
...
/// или вот так
Text(
    S.of(context).pushing(_counter),
    style: Theme.of(context).textTheme.display1,
,

This is how it looks


Code with an example is here (branch iter_0001_localiz).




Source of illustration.




PS
According to awaik

localization will not work under iOS. To make it work, you need to add languages ​​to the info section in Xcode. Otherwise, it does not determine anything and does not produce errors.

here

Also popular now: