Flutter - International 国际化,Localization 本地化, 使用Intl
新建項(xiàng)目,得到一個(gè)示例工程。本例中使用intl包來管理文字資源。
項(xiàng)目地址: https://github.com/RustFisher/localization_demo
步驟:
- 添加依賴項(xiàng) - intl
- 創(chuàng)建文字資源文件
- 生成arb文件
- 新增和修改arb文件
- 根據(jù)arb生成dart文件
- 創(chuàng)建localization代理,新建一個(gè)類繼承LocalizationsDelegate,和文字資源文件聯(lián)系起來
- MaterialApp中添加本地化代理和語言類型
- 使用文字資源
添加依賴項(xiàng)
pubspec.yaml添加依賴項(xiàng)flutter_localizations,然后運(yùn)行一下flutter packages get。
dependencies:flutter:sdk: flutter # 添加下面的依賴項(xiàng)flutter_localizations:sdk: flutterintl: 0.15.6intl_translation: 0.16.7編輯dart文件
新建app_strings.dart文件。
import 'dart:async';import 'package:intl/intl.dart'; import 'package:flutter/widgets.dart';class AppStrings {AppStrings(Locale locale) : _localeName = locale.toString();final String _localeName;static Future<AppStrings> load(Locale locale) {return initializeMessages(locale.toString()).then((Object _) {return new AppStrings(locale);});}static AppStrings of(BuildContext context) {return Localizations.of<AppStrings>(context, AppStrings);}String title() {return Intl.message('Localization Demo',name: 'title',desc: '應(yīng)用標(biāo)題',locale: _localeName,);}String click() => Intl.message('Click',name: 'click',desc: '點(diǎn)擊',locale: _localeName,);String helloFromDemo() => Intl.message('Hello~',name: 'helloFromDemo',desc: '一句問候',locale: _localeName,); }此時(shí)initializeMessages方法會(huì)顯示警告,暫時(shí)不用管,生成arb文件后再添加引用。
生成arb文件
進(jìn)入項(xiàng)目目錄,運(yùn)行intl的命令。
/e/ws/localization_demo $ flutter pub pub run intl_translation:extract_to_arb --output-dir=lib/l10n lib/app_strings.dart生成l10n/intl_messages.arb,內(nèi)容如下??梢钥闯鍪荍SON格式的文本。
{"@@last_modified": "2018-07-15T22:13:19.218221","title": "Localization Demo","@title": {"description": "應(yīng)用標(biāo)題","type": "text","placeholders": {}},"click": "Click","@click": {"description": "點(diǎn)擊","type": "text","placeholders": {}},"helloFromDemo": "Hello~","@helloFromDemo": {"description": "一句問候","type": "text","placeholders": {}} }新增和修改arb文件
前面生成了l10n/intl_messages.arb,我們可以把它當(dāng)成模板。復(fù)制粘貼一下,同目錄下得到intl_en.arb和intl_zh.arb。文件名規(guī)則可以自己定。
以intl_zh.arb為例:
這里也可以把intl_messages.arb刪掉。本例保留這個(gè)文件。
根據(jù)arb生成dart文件
$ flutter pub pub run intl_translation:generate_from_arb --output-dir=lib/l10n \--no-use-deferred-loading lib/app_strings.dart lib/l10n/intl_*.arbNo @@locale or _locale field found in intl_en, assuming 'en' based on the file name. No @@locale or _locale field found in intl_messages, assuming 'messages' based on the file name. No @@locale or _locale field found in intl_zh, assuming 'zh' based on the file name.暫時(shí)無視警告。
此時(shí)在app_strings.dart中添加對(duì)l10n/intl_messages.arb的引用。
警告消失~
更新了arb文件后,需要重新生成dart文件。
創(chuàng)建localization代理
創(chuàng)建localizations_delegate.dart。新建AppLocalizationsDelegate類繼承LocalizationsDelegate,復(fù)寫方法。
泛型指定為前面的AppStrings。
MaterialApp中添加本地化代理和語言類型
class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return new MaterialApp(title: 'Flutter Demo',theme: new ThemeData(primarySwatch: Colors.blue,),localizationsDelegates: [AppLocalizationsDelegate(), // 我們定義的代理GlobalMaterialLocalizations.delegate,GlobalWidgetsLocalizations.delegate,],supportedLocales: [ // 支持的語言類型const Locale('en', 'US'), // Englishconst Locale('zh', ''),],home: new MyHomePage(title: 'Flutter Demo Home Page'),);} }使用文字資源
獲取到AppStrings的實(shí)例。
AppStrings appStrings = AppStrings.of(context);print(appStrings); // logcat: I/flutter ( 7478): Instance of 'AppStrings'注意,在MaterialApp中使用文字資源時(shí),因?yàn)閏ontext的關(guān)系,要使用onGenerateTitle。
onGenerateTitle: (context) {return AppStrings.of(context).title();},支持語言的類型
代理isSupported方法中的語言類型最好是和App中supportedLocales的一致
@overridebool isSupported(Locale locale) =>['zh', 'en'].contains(locale.languageCode);// App中`supportedLocales`supportedLocales: [const Locale('en', 'US'), // Englishconst Locale('zh', ''),],否則可能出現(xiàn)獲取不到AppStrings的異常。
參考:
- https://flutter.io/tutorials/internationalization/
總結(jié)
以上是生活随笔為你收集整理的Flutter - International 国际化,Localization 本地化, 使用Intl的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UICollectionView框架总结
- 下一篇: MongoDB导出场景查询优化 #1