Flutter 1.22现已发布
最新版本中,随之发布了很多新的特性以及修复了一些BUG,现在我们说说其中的新的多国语插件。个人感觉新多国语操作,多了一些模板性的东西,但是也方便了很多东西。
话不多说,现在我们进入正题,如何使用新插件呢?
配置项目的yaml文件
pubspec.yaml
dependencies:
flutter:
sdk: flutter
flutter_localizations: #配置多国语支持
sdk: flutter
intl: ^0.16.1 #多国语插件,要求高于0.16.1版本
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
generate: true #新增配置,自动多国语代码生成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dependencies:
flutter:
sdk:flutter
flutter_localizations:#配置多国语支持
sdk:flutter
intl:^0.16.1#多国语插件,要求高于0.16.1版本
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design:true
generate:true#新增配置,自动多国语代码生成
项目根目录下新增配置l10n.yaml
添加位置如下图:
里面的内容如下:
l10n.yaml
arb-dir: lib/l10n #指定arb多国语文件目录
template-arb-file: app_en.arb #指定arb多国语言的模版文件,一般用英文作为默认模版,其他语言进行扩展
output-localization-file: app_localizations.dart #指定多国语配置生成的代码文件,代码中自动生成类文件AppLocalizations.dart
1
2
3
arb-dir:lib/l10n#指定arb多国语文件目录
template-arb-file:app_en.arb#指定arb多国语言的模版文件,一般用英文作为默认模版,其他语言进行扩展
output-localization-file:app_localizations.dart#指定多国语配置生成的代码文件,代码中自动生成类文件AppLocalizations.dart
l10n.yaml 文件可配置的属性有:
属性
描述
arb-dir
The directory where the template and translated arb files are located. (defaults to "lib/l10n")
output-dir
The directory where the generated localization classes will be written. This option is only relevant if you want to generate the localizations code somewhere else in the Flutter project. You will also need to set the synthetic-package flag to false.
The app must import the file specified in the 'output-localization-file' option from this directory. If unspecified, this defaults to the same directory as the input directory specified in 'arb-dir'.
template-arb-file
The template arb file that will be used as the basis for generating the Dart localization and messages files. (defaults to "app_en.arb")
output-localization-file
The filename for the output localization and localizations delegate classes. (defaults to "app_localizations.dart")
untranslated-messages-file
The location of a file that describes the localization messages have not been translated yet. Using this option will create a JSON file at the target location, in the following format:
"locale": ["message_1", "message_2" ... "message_n"]
If this option is not specified, a summary of the messages that have not been translated will be printed on the command line.
output-class
The Dart class name to use for the output localization and localizations delegate classes. (defaults to "AppLocalizations")
preferred-supported-locales
The list of preferred supported locales for the application. By default, the tool will generate the supported locales list in alphabetical order. Use this flag if you would like to default to a different locale.
For example, pass in [ en_US ] if you would like your app to default to American English if a device supports it.
synthetic-package
Determines whether or not the generated output files will be generated as a synthetic package or at a specified directory in the Flutter project.
This flag is set to true by default.
When synthetic-package is set to false, it will generate the localizations files in the directory specified by arb-dir by default.
If output-dir is specified, files will be generated there.
header
The header to prepend to the generated Dart localizations files. This option takes in a string.
For example, pass in "/// All localized files." if you would like this string prepended to the generated Dart file.
Alternatively, see the header-file option to pass in a text file for longer headers.
header-file
The header to prepend to the generated Dart localizations files. The value of this option is the name of the file that contains the header text which will be inserted at the top of each generated Dart file.
Alternatively, see the header option to pass in a string for a simpler header.
This file should be placed in the directory specified in 'arb-dir'.
[no-]use-deferred-loading
Whether to generate the Dart localization file with locales imported as deferred, allowing for lazy loading of each locale in Flutter web.
This can reduce a web app’s initial startup time by decreasing the size of the JavaScript bundle. When this flag is set to true, the messages for a particular locale are only downloaded and loaded by the Flutter app as they are needed. For projects with a lot of different locales and many localization strings, it can be a performance improvement to have deferred loading. For projects with