Flutter - International 国际化,Localization 本地化, 使用Intl
新建項目,得到一個示例工程。本例中使用intl包來管理文字資源。
項目地址: https://github.com/RustFisher/localization_demo
步驟:
- 添加依賴項 - intl
- 創建文字資源文件
- 生成arb文件
- 新增和修改arb文件
- 根據arb生成dart文件
- 創建localization代理,新建一個類繼承LocalizationsDelegate,和文字資源文件聯系起來
- MaterialApp中添加本地化代理和語言類型
- 使用文字資源
添加依賴項
pubspec.yaml添加依賴項flutter_localizations,然后運行一下flutter packages get。
dependencies:flutter:sdk: flutter # 添加下面的依賴項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: '應用標題',locale: _localeName,);}String click() => Intl.message('Click',name: 'click',desc: '點擊',locale: _localeName,);String helloFromDemo() => Intl.message('Hello~',name: 'helloFromDemo',desc: '一句問候',locale: _localeName,); }此時initializeMessages方法會顯示警告,暫時不用管,生成arb文件后再添加引用。
生成arb文件
進入項目目錄,運行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,內容如下。可以看出是JSON格式的文本。
{"@@last_modified": "2018-07-15T22:13:19.218221","title": "Localization Demo","@title": {"description": "應用標題","type": "text","placeholders": {}},"click": "Click","@click": {"description": "點擊","type": "text","placeholders": {}},"helloFromDemo": "Hello~","@helloFromDemo": {"description": "一句問候","type": "text","placeholders": {}} }新增和修改arb文件
前面生成了l10n/intl_messages.arb,我們可以把它當成模板。復制粘貼一下,同目錄下得到intl_en.arb和intl_zh.arb。文件名規則可以自己定。
以intl_zh.arb為例:
這里也可以把intl_messages.arb刪掉。本例保留這個文件。
根據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.暫時無視警告。
此時在app_strings.dart中添加對l10n/intl_messages.arb的引用。
警告消失~
更新了arb文件后,需要重新生成dart文件。
創建localization代理
創建localizations_delegate.dart。新建AppLocalizationsDelegate類繼承LocalizationsDelegate,復寫方法。
泛型指定為前面的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的實例。
AppStrings appStrings = AppStrings.of(context);print(appStrings); // logcat: I/flutter ( 7478): Instance of 'AppStrings'注意,在MaterialApp中使用文字資源時,因為context的關系,要使用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', ''),],否則可能出現獲取不到AppStrings的異常。
參考:
- https://flutter.io/tutorials/internationalization/
總結
以上是生活随笔為你收集整理的Flutter - International 国际化,Localization 本地化, 使用Intl的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UICollectionView框架总结
- 下一篇: Android Studio 第六十五期