Flutter 國際化相關

2019-10-12 15:58:05async

1、純SDK實現:

一、直接使用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 }

二、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 }

2、使用Intl庫

一、建立一個獨立的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 }

二、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 }

三、最終的代碼結構圖:

相關文章
相關標籤/搜索