fluintl 0.1.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new75

README of English

fluintl (Flutter 国际化)

Pub

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。

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'))),
        ],
      ),
    );
  }
}

Demo: flutter_wanandroid.

APK:点击下载 v0.0.2

APK QR:

flutter_wanandroid

Screenshot

image

image

[0.1.1]

  • TODO: readme update.

[0.1.0]

  • TODO: fluintl initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

import 'package:fluintl/fluintl.dart';

class StringIds {
  static String titleHome = 'title_home';
  static String titleSetting = 'title_setting';
}

Map<String, Map<String, String>> localizedSimpleValues = {
  'en': {
    StringIds.titleHome: 'Home',
    StringIds.titleSetting: 'Setting',
  },
  'zh': {
    StringIds.titleHome: '主页',
    StringIds.titleSetting: '设置',
  },
};

Map<String, Map<String, Map<String, String>>> localizedValues = {
  'en': {
    'US': {
      StringIds.titleHome: 'Home',
      StringIds.titleSetting: 'Setting',
    }
  },
  'zh': {
    'CN': {
      StringIds.titleHome: '主页',
      StringIds.titleSetting: '设置',
    },
    'HK': {
      StringIds.titleHome: '主頁',
      StringIds.titleSetting: '設置',
    },
    'TW': {
      StringIds.titleHome: '主頁',
      StringIds.titleSetting: '設置',
    }
  }
};

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new MyAppState();
  }
}

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,//未导入flutter_localizations,暂时屏蔽.
//        GlobalWidgetsLocalizations.delegate,//未导入flutter_localizations,暂时屏蔽.
        CustomLocalizations.delegate
      ],
      supportedLocales: CustomLocalizations.supportedLocales,
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  State createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    //HomePage init.
    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)),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {
            Navigator.push<String>(context,
                new CupertinoPageRoute(builder: (ctx) => new SettingPage()));
          },
          child: Icon(Icons.navigate_next),
        ));
  }
}

class SettingPage extends StatefulWidget {
  @override
  State createState() => _SettingPageState();
}

class _SettingPageState extends LBaseState<SettingPage> {
  @override
  Widget build(BuildContext context) {
    CustomLocalizations _customLocal = CustomLocalizations.instance;
    return Scaffold(
      appBar: AppBar(
        title: Text(cl.getString(StringIds.titleSetting)),
      ),
      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'))),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  fluintl: ^0.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:fluintl/fluintl.dart';
  
Version Uploaded Documentation Archive
0.1.1 Oct 15, 2018 Go to the documentation of fluintl 0.1.1 Download fluintl 0.1.1 archive
0.1.0 Oct 12, 2018 Go to the documentation of fluintl 0.1.0 Download fluintl 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
49
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Oct 15, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4
  • Flutter: 0.9.5

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8