1.7 i18n 多语言配置
实现步骤:
第 1 步:yaml 配置
pubspec.yaml
1 2 3
| dependencies: flutter_localizations: sdk: flutter
|
第 2 步:定义 keys
lib/common/i18n/locale_keys.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class LocaleKeys { static const commonBottomSave = 'common_bottom_save'; static const commonBottomRemove = 'common_bottom_remove'; static const commonBottomCancel = 'common_bottom_cancel'; static const commonBottomConfirm = 'common_bottom_confirm'; static const commonBottomApply = 'common_bottom_apply'; static const commonBottomBack = 'common_bottom_back'; static const commonSearchInput = 'common_search_input'; static const commonSelectTips = 'common_select_tips'; static const commonMessageSuccess = 'common_message_success'; static const commonMessageIncorrect = 'common_message_incorrect';
static const stylesTitle = 'styles_title'; }
|
定义 key 的规则可以是 界面代码+[组件代码]+[业务代码]
第 3 步:英文词典
lib/common/i18n/locales/locale_en.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import '../locale_keys.dart';
Map<String, String> localeEn = { LocaleKeys.commonSearchInput: 'Enter keyword', LocaleKeys.commonBottomSave: 'Save', LocaleKeys.commonBottomRemove: 'Remove', LocaleKeys.commonBottomCancel: 'Cancel', LocaleKeys.commonBottomConfirm: 'Confirm', LocaleKeys.commonBottomApply: 'Apply', LocaleKeys.commonBottomBack: 'Back', LocaleKeys.commonSelectTips: 'Please select', LocaleKeys.commonMessageSuccess: '@method successfully', LocaleKeys.commonMessageIncorrect: '@method incorrect',
LocaleKeys.stylesTitle: 'Sytles && Function', };
|
第 4 步:中文词典
lib/common/i18n/locales/locale_zh.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| import '../locale_keys.dart';
Map<String, String> localeZh = { LocaleKeys.commonSearchInput: '输入关键字', LocaleKeys.commonBottomSave: '保存', LocaleKeys.commonBottomRemove: '删除', LocaleKeys.commonBottomCancel: '取消', LocaleKeys.commonBottomConfirm: '确认', LocaleKeys.commonBottomApply: '应用', LocaleKeys.commonBottomBack: '返回', LocaleKeys.commonSelectTips: '请选择', LocaleKeys.commonMessageSuccess: '@method 成功', LocaleKeys.commonMessageIncorrect: '@method 不正确',
LocaleKeys.stylesTitle: '样式 && 功能 && 调试', };
|
第 5 步:翻译类 Translations
lib/common/i18n/translation.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Translation extends Translations { static Locale? get locale => Get.deviceLocale; static const fallbackLocale = Locale('en', 'US'); static const supportedLocales = [ Locale('en', 'US'), Locale('zh', 'CN'), ]; static const localizationsDelegates = [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ]; @override Map<String, Map<String, String>> get keys => { 'en': localeEn, 'zh': localeZh, }; }
|
第 6 步:main 配置
lib/main.dart
1 2 3 4 5 6
| translations: Translation(), localizationsDelegates: Translation.localizationsDelegates, supportedLocales: Translation.supportedLocales, locale: ConfigService.to.locale, fallbackLocale: Translation.fallbackLocale,
|
第 7 步:常量配置
lib/common/values/constants.dart
1 2 3 4 5 6 7
| class Constants { ...
static const storageLanguageCode = 'language_code'; }
|
第 8 步:ConfigService 配置
lib/common/services/config.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| class ConfigService extends GetxService { static ConfigService get to => Get.find();
Locale locale = PlatformDispatcher.instance.locale;
...
void initLocale() { var langCode = Storage().getString(Constants.storageLanguageCode); if (langCode.isEmpty) return; var index = Translation.supportedLocales.indexWhere((element) { return element.languageCode == langCode; }); if (index < 0) return; locale = Translation.supportedLocales[index]; }
void onLocaleUpdate(Locale value) { locale = value; Get.updateLocale(value); Storage().setString(Constants.storageLanguageCode, value.languageCode); }
...
@override void onReady() { super.onReady(); ... initLocale(); }
|
最后:调用
lib/pages/styles/styles_index/view.dart
1 2 3 4 5 6 7 8 9 10 11
| Widget _buildView() { return Column(children: [ ListTile( onTap: controller.onLanguageSelected, title: Text( "语言 : ${ConfigService.to.locale.toLanguageTag()}", ), ), ]); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @override Widget build(BuildContext context) { return GetBuilder<StylesIndexController>( init: StylesIndexController(), id: "styles_index", builder: (_) { return Scaffold( appBar: AppBar(title: Text(LocaleKeys.stylesTitle.tr)), body: SafeArea( child: _buildView(), ), ); }, ); }
|
lib/pages/styles/styles_index/controller.dart
1 2 3 4 5 6 7 8 9 10 11
| onLanguageSelected() { var en = Translation.supportedLocales[0]; var zh = Translation.supportedLocales[1];
ConfigService.to.onLocaleUpdate( ConfigService.to.locale.toLanguageTag() == en.toLanguageTag() ? zh : en); update(["styles_index"]); }
|
提交代码到 git