-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Description
Update
The proposal that follows led to a new i18n system for Flutter applications. The new system is part of Flutter 1.19beta and will be part of the following stable release.
A user guide for the final system can be found here: https://flutter.dev/go/i18n-user-guide. A web tutorial is forthcoming.
Introduction
This is a proposal to provide a tool that simplifies setting up and maintaining an internationalized Flutter app. It is based on the Dart intl package. The overall goal is to reduce the number of steps required to use that package in an app with modest i18n requirements.
Current Internationalization Process
Currently, internationalized apps which are based on the intl package must follow a procedure similar to the one outlined in the stocks example. Roughly speaking there are 5 steps:
- Add calls to the static
Intl.messageandIntl.pluralmethods where message strings are needed by the app. - Generate a template ".arb" (application resource bundle) file with
intl_translation:extract_to_arb. - Make copies of the template ".arb" file for each supported locale, and add message translations.
- Generate several source files which collectively encode all of the localizations using
intl_translation:generate_from_arb. - Before the app starts, call
initializeMessages()for each supported locale.
Proposal
The proposed approach is to start with handwritten ".arb" files. One of these files will contain the standard per-message metadata required by the intl package.
A new tool will be used to generate a single class that contains one method per message.
Message lookup is based on the inherited locale, using the current BuildContext:
MyLocalizations.of(context).myMessage()The generated class will provide a LocalizationDelegate which is to be included in the app's list of localization delegates. The generated class itself will depend on intl package; the rest of the app doesn't have to.
This approach, based on segregating the app's localizations in a single class, is similar to the stocks example.
Prototype App and Tool
A prototype of the tool is #41432.
The main elements of the app, including the generated code, are as follows.
The English locale ".arb" file, lib/l10n/demo_en.arb defines the messages. The Spanish locale arb file, lib/l10n/demo_es.arb, just defines message translations.
The generated localizations class, DemoLocalizations in lib/1l0n/demo_localizations.dart, provides all of the messages.
The app includes DemoLocalizations.delegate in its localizationDelegates list, and looks up its messages with DemoLocalizations.of(context).