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.字符串获取
IntlUtil.getString(context, StringIds.titleHome);
CustomLocalizations.of(context).getString(StringIds.titleHome)
5.其他(不推荐使用)
//在主页初始化.
class _MyHomePageState extends State<MyHomePage> {
Widget build(BuildContext context) {
CustomLocalizations.init(context);
...
}
// 字符串获取
CustomLocalizations.instance.getString(StringIds.titleHome)
extends or with LBaseState() (MyHomePageState不能使用)
cl.getString(StringIds.titleHome)
6.应用国际化详细使用请参考flutter_wanandroidApp。
dependencies:
flutter_localizations:
sdk: flutter
fluintl: x.x.x #latest version
setLocalizedSimpleValues(values) : 配置简单多语言资源.
setLocalizedValues(values) : 配置多语言资源.
CustomLocalizations.delegate : 自定义本地化代理.
CustomLocalizations.supportedLocales : 本地支持的语言环境.
CustomLocalizations.of(context) : 获取CustomLocalizations.
getString(id, {languageCode, countryCode}) : 通过id获取字符串,可指定languageCode,countryCode.
IntlUtil.getString(context, id) : 通过id获取字符串,可指定languageCode,countryCode.
//不推荐使用
CustomLocalizations.init(context) : 自定义本地化代理初始化.(在MyHomePage初始化)
LBaseState (extends or with LBaseState) : 可方便简洁获取字符串
cl.getString(id).(MyHomePage不能使用)
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'))),
],
),
);
}
}
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'))),
],
),
);
}
}
Add this to your package's pubspec.yaml file:
dependencies:
fluintl: ^0.1.2
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.
Now in your Dart code, you can use:
import 'package:fluintl/fluintl.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
0.1.2 | Nov 21, 2018 |
|
|
0.1.1 | Oct 15, 2018 |
|
|
0.1.0 | Oct 12, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
89
|
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]
|
94
|
We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
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 | 1.1.7 | |
sky_engine | 0.0.99 | ||
typed_data | 1.1.6 | ||
vector_math | 2.0.8 |