国际化方案化繁为简,介绍目前快捷有效的国际化方案Flutter Intl。
国际化的英文称呼:internationalization(简称为i18n)
一、国际化的配置
1,xcode的配置,修改iOS的info.plist文件配置。
找到info.plist下面的Localizations选项,添加需要的语言。
2,Android Studio 配置,安装Flutter Intl。
在Android Studio的Plugins中搜索安装插件Flutter Intl:
二,项目配置
1、Flutter SDK中的组件默认仅提供美国英语本地化资源。要添加对其他语言的支持,你的应用程序须添加一个名为“flutter_localizations”的包依赖。
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
2,配置项目参数
-
在localizationsDelegates中指定哪些Widget需要进行国际化
-
supportedLocales指定要支持哪些国际化
return MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
S.delegate
],
supportedLocales:S.delegate.supportedLocales,
);
supportedLocales也可以这样写:
supportedLocales: [
Locale("en"),
Locale("zh")
],
三,使用Flutter Intl
1,初始化Flutter Intl,找到Tools -> Flutter Intl -> Initialize for the Project
配置完成后会生成:
1. 会在 pubspec.yaml中增加以下配置
flutter_intl:
enabled: true
2,会在lib文件夹下成generated目录,generated会自动生成代码。
还会生成I10n目录,生成对应的arb文件目录。
在生成的I10n下的.arb文件,我们用来添加具体的国家化字符串
如intl_zh.arb中填写如下:
在项目中使用:
var ss = S.of(context);
Text(ss.device),
或者直接调用S.of(context)
Text(S.of(context).device)
到这里,我们的国际化就完成了。