Flutter中文社区Flutter圈子Flutter

Flutter 国际化/多语言库 fluintl

2018-10-13  本文已影响31人  Sky24n

我的Flutter开源库:
Flutter 汉字转拼音库lpinyin
Flutter 常用工具类库common_utils
Flutter 城市列表 索引&悬停
Flutter 时间轴工具类TimelineUtil
Flutter 国际化/多语言库 fluintl

[fluintl]

fluintl 是一个为应用提供国际化的库,可快速集成实现应用多语言。该库封装了一个国际化支持类,通过提供统一方法getString(id)获取字符串。CustomLocalizations多语言支持类。LBaseState可简洁获取字符串。
使用步骤:
1.创建多语言资源字符串id管理类StringIds 和 多语言资源Map

///多语言资源id管理类.  
class StringIds {  
  static String titleHome = 'title_home';  
}  
///简单多语言资源.  
Map<String, Map<String, String>> localizedSimpleValues = {  
  'en': {    
    StringIds.titleHome: 'Home',  
  },  
  'zh': {    
    StringIds.titleHome: '主页',  
  },  
};  
///多语言资源.  
Map<String, Map<String, Map<String, String>>> localizedValues = {  
  'en': {  
    'US': {  
      StringIds.titleHome: 'Home',  
    }  
  },  
  'zh': {  
    'CN': {  
      StringIds.titleHome: '主页',  
    },  
    'HK': {  
      StringIds.titleHome: '主頁',  
    },  
  }  
};  

2.在MyApp initState配置多语言资源(可配置通用或简单多语言资源,二选一)

  void initState() {    
    super.initState();      
//    setLocalizedSimpleValues(localizedSimpleValues);//配置简单多语言资源  
    setLocalizedValues(localizedValues); //配置多语言资源      
  }   

3.在MaterialApp指定localizationsDelegates和supportedLocales:

MaterialApp(  
   home: MyHomePage(),  
   localizationsDelegates: [  
   GlobalMaterialLocalizations.delegate,  
   GlobalWidgetsLocalizations.delegate,  
   CustomLocalizations.delegate //设置本地化代理     
   ],  
   supportedLocales: CustomLocalizations.supportedLocales,//设置支持本地化语言集合     
);  

4.在MyHomePage初始化CustomLocalizations:

class _MyHomePageState extends State<MyHomePage> {  
  Widget build(BuildContext context) {    
    CustomLocalizations.init(context); //在主页初始化.      
    ...      
  }    
} 

5.字符串获取

CustomLocalizations.of(context).getString(StringIds.titleHome)  
CustomLocalizations.instance.getString(StringIds.titleHome)  

继承LBaseState()  (MyHomePageState不能使用)  
cl.getString(StringIds.titleHome)  

6.应用国际化详细使用请参考flutter_wanandroidApp。

GitHub: fluintl.

Pub: fluintl

Demo: flutter_wanandroid.

APK: 点击下载 v0.0.3

APK QR:

flutter_wanandroid

Screenshot


Add dependency

dependencies:
  flutter_localizations:
    sdk: flutter  

  fluintl: x.x.x  #latest version

APIs

setLocalizedSimpleValues(values)            : 配置简单多语言资源.
setLocalizedValues(values)                  : 配置多语言资源.
CustomLocalizations.delegate                : 自定义本地化代理.
CustomLocalizations.supportedLocales        : 本地支持的语言环境.
CustomLocalizations.init(context)           : 自定义本地化代理初始化.(在MyHomePage初始化)
CustomLocalizations.of(context)             : 获取CustomLocalizations.
getString(id, {languageCode, countryCode})  : 通过id获取字符串,可指定languageCode,countryCode.
LBaseState (extends or with LBaseState)     : 可方便简洁获取字符串cl.getString(id).(MyHomePage不能使用)

Example

import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:fluintl/fluintl.dart';

///多语言资源id管理类.
class StringIds {
  static String titleHome = 'title_home';
}
///简单多语言资源.
Map<String, Map<String, String>> localizedSimpleValues = {
  'en': {
    StringIds.titleHome: 'Home',
  },
  'zh': {
    StringIds.titleHome: '主页',
  },
};
///多语言资源.
Map<String, Map<String, Map<String, String>>> localizedValues = {
  'en': {
    'US': {
      StringIds.titleHome: 'Home',
    }
  },
  'zh': {
    'CN': {
      StringIds.titleHome: '主页',
    },
    'HK': {
      StringIds.titleHome: '主頁',
    },
    'TW': {
      StringIds.titleHome: '主頁',
    }
  }
};

class _MyAppState extends State<MyApp> {
  Locale _locale;

  @override
  void initState() {
    super.initState();
//    setLocalizedSimpleValues(localizedSimpleValues);//配置简单多语言资源
    setLocalizedValues(localizedValues);//配置多语言资源
//    _locale = new Locale('en', 'US');//English
//    _locale = new Locale('zh', 'HK');//繁體中文(香港)
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
      locale: _locale,
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        CustomLocalizations.delegate //设置本地化代理
      ],
      supportedLocales: CustomLocalizations.supportedLocales,//设置支持语言
    );
  }
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    //在主页初始化.
    CustomLocalizations.init(context);
    return Scaffold(
        appBar: AppBar(
          title: Text(
              CustomLocalizations.of(context).getString(StringIds.titleHome)),
        ),
        body: Center(
          child: new Text(
              CustomLocalizations.instance.getString(StringIds.titleHome)),
        ));
  }
}

class _SettingPageState extends LBaseState<SettingPage> {
  @override
  Widget build(BuildContext context) {
    CustomLocalizations _customLocal = CustomLocalizations.instance;
    return Scaffold(
      body: new ListView(
        children: <Widget>[
          ListTile(title: Text(CustomLocalizations.of(context).getString(StringIds.titleSetting))),
          ListTile(title: Text(CustomLocalizations.instance.getString(StringIds.titleSetting))),
          ListTile(title: Text(_customLocal.getString(StringIds.titleSetting))),
          ListTile(title: Text(cl.getString(StringIds.titleSetting))),
          ListTile(),
          ListTile(title: Text(cl.getString(StringIds.titleSetting, languageCode: 'en', countryCode: 'US'))),
          ListTile(title: Text(cl.getString(StringIds.titleSetting, languageCode: 'zh', countryCode: 'CN'))),
          ListTile(title: Text(cl.getString(StringIds.titleSetting, languageCode: 'zh', countryCode: 'HK'))),
          ListTile(title: Text(cl.getString(StringIds.titleSetting, languageCode: 'zh', countryCode: 'TW'))),
        ],
      ),
    );
  }
}

上一篇下一篇

猜你喜欢

热点阅读