Coder Social home page Coder Social logo

fluintl's Introduction

README of English

fluintl (Flutter 国际化)

Pub

fluintl 是一个为应用提供国际化的库,可快速集成实现应用多语言。该库封装了一个国际化支持类,通过提供统一方法getString(id)获取字符串。CustomLocalizations多语言支持类。LBaseState可简洁获取字符串。

使用方式:

dependencies:
  fluintl: ^2.0.0  

v0.1.3(2019.03.25)

支持复用。

 //替换字符串格式要求:'%\${index}\$s' ,{index} 第几个参数,从0开始。

 Ids.click_times: '%\$0\$s点击了%\$1\$s次',   
   
 IntlUtil.getString(context, Ids.click_times, params: ['Tom', '$_counter'])  
 // print: Tom点击了0次

使用步骤:

1.创建多语言资源字符串id管理类Ids 和 多语言资源Map

///多语言资源id管理类.  
class Ids {  
  static String titleHome = 'title_home'; 
}  
///简单多语言资源.  
Map<String, Map<String, String>> localizedSimpleValues = {  
  'en': {    
    Ids.titleHome: 'Home',  
  },  
  'zh': {    
    Ids.titleHome: '主页',  
  },  
};  
///多语言资源.  
Map<String, Map<String, Map<String, String>>> localizedValues = {  
  'en': {  
    'US': {  
      Ids.titleHome: 'Home',  
    }  
  },  
  'zh': {  
    'CN': {  
      Ids.titleHome: '主页',  
    },  
    'HK': {  
      Ids.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.字符串获取

 IntlUtil.getString(context, Ids.titleHome);
 IntlUtil.getString(context, Ids.titleHome, params: [param1, param2]); 
 CustomLocalizations.of(context).getString(Ids.titleHome)

5.其他(不推荐使用)

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

 // 字符串获取
 CustomLocalizations.instance.getString(Ids.titleHome)
 extends or with LBaseState()  (MyHomePageState不能使用)
 cl.getString(Ids.titleHome)

6.应用国际化详细使用请参考flutter_wanandroidApp。
Q:Flutter国际化系统切换iOS不生效问题?
A:在Xcode项目Localizations下添加支持语言即可,原文

Add dependency

dependencies:
  flutter_localizations:
    sdk: flutter  
    
  fluintl: x.x.x  #latest version

APIs

setLocalizedSimpleValues(values)                    : 配置简单多语言资源.
setLocalizedValues(values)                          : 配置多语言资源.
CustomLocalizations.delegate                        : 自定义本地化代理.
CustomLocalizations.supportedLocales                : 本地支持的语言环境.
CustomLocalizations.of(context)                     : 获取CustomLocalizations.
getString(id, {languageCode, countryCode, params})  : 通过id获取字符串,可指定languageCode,countryCode.
IntlUtil.getString(context, id, params)             : 通过id获取字符串,可指定languageCode,countryCode.

//不推荐使用
CustomLocalizations.init(context)                   : 自定义本地化代理初始化.(在MyHomePage初始化)
LBaseState (extends or with LBaseState)             : 可方便简洁获取字符串
cl.getString(id).(MyHomePage不能使用)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.