美文网首页
Flutter系列笔记-8.Flutter国际化问题的处理

Flutter系列笔记-8.Flutter国际化问题的处理

作者: 缘焕 | 来源:发表于2019-12-29 22:55 被阅读0次

    没有注释的代码不是好代码

    没有demo的文章不是好文章

    本文demo请点击github

    Flutter国际化

    国际化一般指界面上显示的文字可以按需要切换成中文简体,英文,中文繁体这三种语言文字,在Flutter里国际化的处理官方推荐使用intl

    intl包的使用

    1.添加依赖

    dependencies:
      flutter_localizations:
        sdk: flutter
      flutter_cupertino_localizations: ^1.0.1
      intl: ^0.16.0
    

    2. 继承 LocalizationsDelegate类

    定义简单的中英繁字符串资源

    static String message(String message_str,
              {String desc: '',
              Map<String, Object> examples,
              String locale,
              String name,
              List<Object> args,
              String meaning,
              bool skip})
    

    例如 定义一个key是theme值是"Theme"的字符串资源

    String get theme => Intl.message("Theme", name: "theme"); //主题
    

    带有占位符的字符串资源

      String greetingMessage(name) => Intl.message(
          'Hello $name!',
          name: 'greetingMessage',
          args: [name],
          desc: 'Greet the user as they first open the application',
          examples: const {'name': 'Emily'});
    

    带有占位符的字符串资源 并且有单复数区别的字符串资源

      String remainingEmailsMessage(int howMany, String userName) =>  Intl.message(
              """${Intl.plural(howMany,
                  zero: 'There are no emails left for $userName.',
                  one: 'There is $howMany email left for $userName.',
                  other: 'There are $howMany emails left for $userName.')}""",
              name: 'remainingEmailsMessage',
              args: [howMany, userName],
              desc: "How many emails remain after archiving.",
          examples: const {'howMany': 42, 'userName': 'Fred'});
    

    根据包含Intl.message()方法的dart类生成arb文件

    在命令行运行以下命令,下面示例是把my_localizations_delegate.dart里根据Intl.message方法定义的字符串资源在i18n-arb文件夹下生成arb文件,运行命令前,请先把i18n-arb文件夹提前手动创建

    flutter pub run intl_translation:extract_to_arb --output-dir=i18n-arb  lib/intl_test/my_localizations_delegate.dart
    

    上面的命令,会在i18n-arb文件夹下生成 intl_messages.arb文件,把intl_messages.arb复制两份,分别命名为 intl_zh_hans_cn.arb(中文简体资源) intl_zh_hant_tw.arb(中文繁体资源),然后把里面对应的英文资源翻译成中文简体中文繁体

    根据arb文体生成dart文件

    运行以下代码前,要保证output-dir指向的文件夹已经创建好,可以修改output-dir以指定生成的dart文件生成到哪一个文件夹

    flutter pub run intl_translation:generate_from_arb --output-dir=lib/intl_test --generated-file-prefix=my_ --no-use-deferred-loading lib/intl_test/my_localizations_delegate.dart i18n-arb/intl_messages.arb i18n-arb/intl_zh_hans_cn.arb i18n-arb/intl_zh_hant_tw.arb
    

    在MaterialApp或CupertinoApp里配置相关的属性,以使切换多语言功能生效

     ...省略相关代码
     return MaterialApp(
          //设置当前应用要使用的区域设置,跟随系统时,最好不要设置成null
          locale: localeModel.locale,
          localizationsDelegates: [
            // 本地化的代理类
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            MyLocalizationsDelegate.delegate,
          ],
          //设置支持的语言列表,一般中英繁三种
          supportedLocales: LocaleUtil.locales,
          onGenerateTitle: (context){
            //最近运行任务里的标题
            return MyLocalizations.of(context).persistentStorage;
          },
          //  语言环境列表解析回调
          localeListResolutionCallback:(List<Locale> locales, Iterable<Locale> supportedLocales){
            return getDefaultLocale(localeModel.locale,locales,supportedLocales);
          } ,
          //  语言环境列表解析回调
          localeResolutionCallback: (Locale locale, Iterable<Locale> supportedLocales){
            return getDefaultLocale(localeModel.locale,[locale],supportedLocales);
          },
          ...省略无关代码
        );
    

    总结

    Platform.localeName可以得到当前语言环境的localName,但是获取到的localName有些手机中文简体是 zh_Hans_CN 繁体是 zh_Hant_TW 有些手机中文简体是 zh_CN 繁体是 zh_TW,所以要做一下兼容。
    完整代码,请查看demo

    相关文章

      网友评论

          本文标题:Flutter系列笔记-8.Flutter国际化问题的处理

          本文链接:https://www.haomeiwen.com/subject/yfiuoctx.html