Intl实现多语言切换
安装 Intl 插件
安装完成再重启AndroidStudio后生效
添加项目依赖
flutter_localizations: sdk: flutter
自动生成目录
点击Tools->Flutter Intl -> Initialze for the Project 此时会自动在pubspec.yaml文件中添加 flutter_intl: enabled: true,并且生成 lib->generated-> intl 和 l10n 目录以及相对应的文件,请勿手动修改
添加语言支持
Tools -> Flutter Intl -> Add locale 选择对应语言,例如zh,en。如果有繁体支持,港台分别为zh_TW/zh_HK. 选定后,会在l10n文件夹下自动创建对应文件,例如 intl_en.arb/intl_zh.arb
项目代码应用
编辑arb文件
intl_en.arb
{
"test" : "测试"
}
intl_zh.arb
{
"test" : "Test"
}
中英文切换
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: const Locale('zh', ''),
localizationsDelegates: const [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
supportedLocales: [
const Locale('zh', ''),
...S.delegate.supportedLocales
],
title: 'Flutter Demo',
theme: ThemeData(primarySwatch: Colors.blue),
home: const MyHomePage(title: 'Flutter Demo Home Page'));
}
调用字符串
S.current.test
S.of(context).test
自定义实现多语言切换
代码实现
abstract class BaseString {
String get home;
String get list;
String get more;
}
class CNString extends BaseString {
@override
String get home => '首页';
@override
String get list => '列表';
@override
String get more => '更多';
}
class ENString extends BaseString {
@override
String get home => 'Home';
@override
String get list => 'List';
@override
String get more => 'More';
}
class String591 {
static const String languageCN = 'CN';
static const String languageEN = 'EN';
static final BaseString cnString = CNString();
static final BaseString enString = ENString();
static changeLanguage(String language) {
if (null != language && language.isNotEmpty) {
SpUtil().put(SpKey.SP_APP_LANGUAGE, language);
}
}
static String getLanguageType() {
return SpUtil().getString(SpKey.SP_APP_LANGUAGE, defaultValue: languageCN);
}
static BaseString create() {
String language =
SpUtil().getString(SpKey.SP_APP_LANGUAGE, defaultValue: languageCN);
if (language == languageEN) {
return enString;
} else {
return cnString;
}
}
static getString({String cn = '', String en = ''}) {
String language =
SpUtil().getString(SpKey.SP_APP_LANGUAGE, defaultValue: languageCN);
if (language == languageEN) {
return en;
} else {
return cn;
}
}
}
字符串使用
String591.create().home
缓存类引用
# 轻量级缓存
# https://pub.dev/packages/shared_preferences/install
shared_preferences: ^2.0.6
import 'package:shared_preferences/shared_preferences.dart';
class SpUtil {
SharedPreferences _prefs;
static final SpUtil _instance = SpUtil._internal();
factory SpUtil() {
return _instance;
}
SpUtil._internal() {
_init();
}
void _init() async {
_prefs = await SharedPreferences.getInstance();
}
void put(String key, Object value) {
if (null != value && null != _prefs) {
if (value is int) {
_prefs.setInt(key, value);
} else if (value is double) {
_prefs.setDouble(key, value);
} else if (value is bool) {
_prefs.setBool(key, value);
} else if (value is String) {
_prefs.setString(key, value);
} else if (value is List) {
_prefs.setStringList(key, value);
}
}
}
int getInteger(String key, {int defaultValue = 0}) {
if (null != _prefs &&
null != key &&
key.isNotEmpty &&
_prefs.containsKey(key)) {
return _prefs.getInt(key);
} else {
return defaultValue;
}
}
String getString(String key, {String defaultValue = ''}) {
if (null != _prefs &&
null != key &&
key.isNotEmpty &&
_prefs.containsKey(key)) {
return _prefs.getString(key);
} else {
return defaultValue;
}
}
bool getBoolean(String key, {bool defaultValue = false}) {
if (null != _prefs &&
null != key &&
key.isNotEmpty &&
_prefs.containsKey(key)) {
return _prefs.getBool(key);
} else {
return defaultValue;
}
}
double getDouble(String key, {double defaultValue = 0}) {
if (null != _prefs &&
null != key &&
key.isNotEmpty &&
_prefs.containsKey(key)) {
return _prefs.getDouble(key);
} else {
return defaultValue;
}
}
List<String> getList(String key, {List<String> defaultValue}) {
if (null != _prefs &&
null != key &&
key.isNotEmpty &&
_prefs.containsKey(key)) {
return _prefs.getStringList(key);
} else {
return defaultValue;
}
}
void removeKey(String key) {
if (null != _prefs &&
null != key &&
key.isNotEmpty &&
_prefs.containsKey(key)) {
_prefs.remove(key);
}
}
void clearAll() {
if (null != _prefs) {
_prefs.clear();
}
}
static asyncPut(String key, Object value) async {
if (null != value) {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (value is int) {
await prefs.setInt(key, value);
} else if (value is double) {
await prefs.setDouble(key, value);
} else if (value is bool) {
await prefs.setBool(key, value);
} else if (value is String) {
await prefs.setString(key, value);
} else if (value is List) {
await prefs.setStringList(key, value);
}
}
}
static Future<int> asyncGetInteger(String key, {int defaultValue = 0}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (null != prefs &&
null != key &&
key.isNotEmpty &&
prefs.containsKey(key)) {
return prefs.getInt(key);
} else {
return defaultValue;
}
}
static Future<String> asyncGetString(String key,
{String defaultValue = ''}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (null != prefs &&
null != key &&
key.isNotEmpty &&
prefs.containsKey(key)) {
return prefs.getString(key);
} else {
return defaultValue;
}
}
static Future<bool> asyncGetBoolean(String key,
{bool defaultValue = false}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (null != prefs &&
null != key &&
key.isNotEmpty &&
prefs.containsKey(key)) {
return prefs.getBool(key);
} else {
return defaultValue;
}
}
static Future<double> asyncGetDouble(String key,
{double defaultValue}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (null != prefs &&
null != key &&
key.isNotEmpty &&
prefs.containsKey(key)) {
return prefs.getDouble(key);
} else {
return defaultValue;
}
}
static Future<List<String>> asyncGetList(String key,
{List<String> defaultValue}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (null != prefs &&
null != key &&
key.isNotEmpty &&
prefs.containsKey(key)) {
return prefs.getStringList(key);
} else {
return defaultValue;
}
}
static void asyncRemoveKey(String key) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (null != prefs &&
null != key &&
key.isNotEmpty &&
prefs.containsKey(key)) {
prefs.remove(key);
}
}
static void asyncClearAll() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
if (null != prefs) {
prefs.clear();
}
}
}
|