• Flutter 国际化相关


    2019-10-12 15:58:05

    一、纯SDK实现:

    1、直接使用SDK提供的API,代码如下:

     1 abstract class LangangeBase {
     2   Map<String, String> langStr = new HashMap();
     3 }
     4 
     5 class LangangeEn extends LangangeBase {
     6   LangangeEn() {
     7     langStr['title'] = 'Title';
     8     langStr['desc'] = 'Description';
     9     langStr['content'] = 'Content:Hello World!';
    10   }
    11 }
    12 
    13 class LangangeZh extends LangangeBase {
    14   LangangeZh() {
    15     langStr['title'] = '标题';
    16     langStr['desc'] = '描述';
    17     langStr['content'] = '内容:世界,你好!';
    18   }
    19 }

    2、Delegate及包装类代码:

     1 class Translations {
     2   static LangangeBase _langangeBase;
     3 
     4   // 默认使用简体中文
     5   static Future<Translations> load(Locale locale) async {
     6     print('加载语言 langCode = ${locale.languageCode}');
     7     Translations translations = new Translations();
     8     if (locale.languageCode == 'en') {
     9       _langangeBase = new LangangeEn();
    10     } else {
    11       _langangeBase = new LangangeZh();
    12     }
    13 
    14     return translations;
    15   }
    16 
    17   String text(String key) {
    18     return _langangeBase.langStr[key];
    19   }
    20 
    21   static Translations of(BuildContext context) {
    22     Translations ret = Localizations.of(context, Translations);
    23     print('Translations of result = $ret');
    24     return ret;
    25   }
    26 }
    27 
    28 class MyLocalizations extends LocalizationsDelegate<Translations> {
    29   @override
    30   bool isSupported(Locale locale) {
    31     print('isSupported ${locale.languageCode}');
    32     // 只支持简体中文、美式英语
    33     return /*['zh', 'en'].contains(locale.languageCode)*/true;
    34   }
    35 
    36   @override
    37   bool shouldReload(LocalizationsDelegate<Translations> old) {
    38     return false;
    39   }
    40 
    41   @override
    42   Future<Translations> load(Locale locale) {
    43     return Translations.load(locale);
    44   }
    45 
    46   static MyLocalizations delegate = new MyLocalizations();
    47 }

    二、使用Intl库

    1、创建一个独立的dart字符串类,便于生产arb文件,如下:

     1 class LocalStr {
     2   /**
     3    * 这里的是兜底文案,在没有相对应的本地化资源时使用
     4    */
     5   static String get title =>
     6       Intl.message('Default:天王盖地虎', name: 'title', desc: 'APP标题');
     7 
     8   static String get desc =>
     9       Intl.message('Default:智取威虎山', name: 'desc', desc: '标题的描述');
    10 
    11   static String get content => Intl.message('Default:电影讲述了东北解放军如何消灭盘踞山岭的土匪的故事',
    12       name: 'content', desc: 'App的主要内容');
    13 }

    2、Delegate及包装类代码:

     1 class IntlLocalizations {
     2 
     3   static Future<IntlLocalizations> load(Locale locale) {
     4     String name = locale.countryCode.isEmpty ? locale.languageCode : locale.toString();
     5     print('local = ${Intl.canonicalizedLocale(name)}');
     6     return initializeMessages(name).then((b) {
     7       Intl.defaultLocale = name;
     8       return new IntlLocalizations();
     9     });
    10   }
    11 
    12   String get title1 => LocalStr.title;
    13 
    14   String get desc1 => LocalStr.desc;
    15 
    16   String get content1 => LocalStr.content;
    17 
    18   static IntlLocalizations of(BuildContext context) {
    19     return Localizations.of<IntlLocalizations>(context, IntlLocalizations);
    20   }
    21 }
    22 
    23 class IntlLocalizatoinsDelegate extends LocalizationsDelegate<IntlLocalizations> {
    24   @override
    25   bool isSupported(Locale locale) {
    26     print('lang code = ${locale.languageCode}');
    27     return /*['zh', 'en'].contains(locale.languageCode)*/true;
    28   }
    29 
    30   @override
    31   Future<IntlLocalizations> load(Locale locale) {
    32     return IntlLocalizations.load(locale);
    33   }
    34 
    35   @override
    36   bool shouldReload(LocalizationsDelegate old) {
    37     return false;
    38   }
    39 
    40   static IntlLocalizatoinsDelegate delegate = new IntlLocalizatoinsDelegate();
    41 }

    3、最终的代码结构图:

  • 相关阅读:
    框架Frameset 的JS 跳转 刷新 [转]
    Javascript的变量与delete操作符
    字符串替换方法的优劣
    Nutch介绍(译)
    权限系统—数据授权模型
    Apache Synapse介绍(译)
    java动态代理
    ASM简介
    maven使用技巧——构件搜索
    springSecurity源码分析——org.springframework.security.web.FilterInvocation类
  • 原文地址:https://www.cnblogs.com/wlrhnh/p/11662157.html
Copyright © 2020-2023  润新知