I've tried various solutions to implement localization, and the best I've come across is the Flutter Intl plugin for VS Code or Android Studio/IntelliJ made by Localizely.com (not affiliated).
With it, basically you install the plugin using the marketplace/plugin library, then initialize for your project using the menu option. This creates a default english locale in lib/l10n/intl_en.arb (which sounds scary but is actually just JSON) and sets up all the scaffolding for the internationalization in lib/generated
.
You also have to add the following to your dependencies.
flutter_localizations:
sdk: flutter
You can then add keys to this file and they'll be automatically available in your app, by importing generated/l10n.dart
which contains a class called S.
To get flutter to use it, wherever it is that you initialize your MaterialApp, make sure to pass S.delegate
into MaterialApp's localizationsDelegates parameter (most likely as part of an array with GlobalMaterialLocalizations.delegate
, GlobalWidgetsLocalizations.delegate
, and possibly GlobalCupertinoLocalizations.delegate
.) You also have to add S.delegate.supportedLocales
to MaterialApp's supportedLocales
.
To add more locales, use the option in the menu (in intellij at least) or simply create more intl_.arb files, and the plugin will automatically recognize this and set up the relevant code.
Say you have an intl_en file with the following:
{ "name": "Name" }
You'd then use S.of(context).name
to use the string in your code.
All this is more eloquently explained on localizely's website.
Now, to use keys in these .arb files, you simply have to wrap it in {...}. So for example:
{ "choose1OfNumOptions": "Choose 1 of {numoptions} options" }
would lead to a usage of S.of(context).choose1OfNumOptions(numOptions);
. I don't know that the plugin supports the full ARB specification but it does support at least the basics.
Also, I'm not using Localizely but it seems like it'd be a pretty useful way to manage the translations and the plugin integrates automatically, although I think it's also pretty horrendously overpriced - at least for my app, which happens to have a ton of text. I actually just have a google sheet where I store all my translations, and when it's time to update it I download it as a .tsv and wrote a simple little parser to write out to the .arb files.