开始的时候我以为没有必要使用插件来实现国际化 , 几个json文件一加载就完事了,而后配置一下语言相关的处理. 可是使用了插件以后我真香了 , 真的是简单了太多了.git
添加依赖:github
dev_dependencies:
...
//add this
flutter_localizations:
sdk: flutter
复制代码
在菜单栏的Tool下找到Flutter Intl 并选择Initalize for the project, 配置结束后效果以下:json
flutter_intl:
enabled: true
复制代码
使用Add Locale
生成其余语言的arb文件markdown
例如:zh_CN , 会在l10n包下生成Intl_zh_CN.arb文件 , 可在其中填入对应的文案.oop
return MaterialApp(
...
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
supportedLocales: S.delegate.supportedLocales,
localeListResolutionCallback: (locales, supportedLocales) {
print(locales);
return;
},
...
);
复制代码
在须要配置国际化的地方调用S.of(context).key
便可.ui
没有context
时可使用S.current.key
this
具体使用方式请参考本项目的代码spa
注意 :.net
zh_TW
, 项目并不支持,会优先寻找zh
的其余语言 , 以supportedLocales
顺序从上向下寻找.i18n
文件夹 , 而flutter intl
生成的为l10n
, 首字母不一样 , 须要注意.intl
包 , 但不提供intl
相关功能. 须要额外功能才须要依赖intl
.i18n
中的代码,但可能须要修改类名.l10n
文件夹下, 并修更名字 ,例如:intl_en.arb , intl_zh_CN.arb${param}
,而flutter intl插件为{param}
,要注意去掉$.