Skip to content

Commit 2b8faf9

Browse files
committed
Merge branch 'ysz' into test
2 parents c226dd1 + ba6674e commit 2b8faf9

File tree

15 files changed

+236
-207
lines changed

15 files changed

+236
-207
lines changed

lib/components/widgetComp.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
22
import 'package:efox_flutter/store/index.dart' show Store;
33
import 'header.dart' as Header;
44
import 'package:efox_flutter/components/markdownComp.dart' as MarkDownComp;
5-
import 'package:efox_flutter/lang/app_translations.dart' show AppTranslations;
5+
import 'package:efox_flutter/lang/index.dart' show AppLocalizations;
66
import 'package:efox_flutter/components/baseComp.dart' as BaseComp;
77
import 'package:efox_flutter/components/exampleComp.dart' as ExampleComp;
88
import 'package:efox_flutter/utils/file.dart' as FileUtils;
@@ -184,7 +184,7 @@ class IndexState extends State<Index> {
184184
Container(
185185
padding: const EdgeInsets.fromLTRB(16.0, 16.0, 16.0, 0),
186186
child: Text(
187-
AppTranslations.of(context).t('loading'),
187+
AppLocalizations.$t('loading'),
188188
style: TextStyle(
189189
color: Color(AppTheme.secondColor), fontSize: 20.0),
190190
),

lib/lang/Application.dart

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/lang/app_translations.dart

Lines changed: 0 additions & 66 deletions
This file was deleted.

lib/lang/app_translations_delegate.dart

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/lang/config.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:flutter/material.dart';
2+
3+
class ConfigLanguage {
4+
static List<Locale> supportedLocales = [
5+
Locale('zh', 'CH'),
6+
Locale('en', 'US'),
7+
];
8+
9+
static Map<String, dynamic> sopportLanguage = {
10+
"zh": {"code": "zh", "country_code": "CH"},
11+
"en": {"code": "en", "country_code": "US"}
12+
};
13+
14+
static dynamic defualtLanguage = {
15+
"code": "zh",
16+
"country_code": "CH"
17+
};
18+
}

lib/lang/index.dart

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import 'package:flutter/material.dart';
2+
import 'dart:async';
3+
import 'dart:convert';
4+
import 'package:efox_flutter/lang/config.dart' as I18NConfig;
5+
import 'package:flutter/services.dart' show rootBundle;
6+
7+
class AppLocalizations {
8+
Locale _locale; // language
9+
static Function _setState; // setState
10+
static AppLocalizationsDelegate _delegate;
11+
static Map<String, dynamic> jsonLanguage;
12+
static AppLocalizations _inst; // inst
13+
14+
AppLocalizations(this._locale);
15+
16+
// init localizations
17+
static Future<AppLocalizations> init(Locale locale) async {
18+
_inst = AppLocalizations(locale);
19+
await getLanguageJson();
20+
return _inst;
21+
}
22+
23+
// 设置语言切换代理
24+
static void setProxy(Function setState, AppLocalizationsDelegate delegate) async {
25+
_setState = setState;
26+
_delegate = delegate;
27+
}
28+
29+
static get languageCode => _inst._locale.languageCode;
30+
31+
static void getLanguageJson([Locale locale]) async {
32+
Locale _tmpLocale = _inst._locale;
33+
print(_tmpLocale.languageCode);
34+
String jsonLang;
35+
try {
36+
jsonLang = await rootBundle
37+
.loadString('locale/${_tmpLocale.languageCode}.json');
38+
} catch (e) {
39+
_inst._locale = Locale(I18NConfig.ConfigLanguage.defualtLanguage.code);
40+
jsonLang = await rootBundle
41+
.loadString('locale/${I18NConfig.ConfigLanguage.defualtLanguage.code}.json');
42+
}
43+
json.decode(jsonLang);
44+
jsonLanguage = json.decode(jsonLang);
45+
print("当前语言: ${_inst._locale}");
46+
print("Json数据: ${jsonLanguage}");
47+
}
48+
49+
static void changeLanguage([Locale locale]) {
50+
if (locale == null) {
51+
locale = AppLocalizations.languageCode == 'zh'
52+
? Locale('en', "US")
53+
: Locale("zh", "CH");
54+
}
55+
_inst._locale = locale;
56+
getLanguageJson(); // 根据语言获取对应的国际化文件
57+
_setState(() {
58+
_delegate = AppLocalizationsDelegate(locale);
59+
});
60+
}
61+
62+
// get local language
63+
_t(String key) {
64+
var _array = key.split('.');
65+
var _dict = jsonLanguage;
66+
var retValue = '';
67+
try {
68+
_array.forEach((item) {
69+
if (_dict[item].runtimeType == Null) {
70+
retValue = key;
71+
return;
72+
}
73+
if (_dict[item].runtimeType != String) {
74+
_dict = _dict[item];
75+
} else {
76+
retValue = _dict[item];
77+
}
78+
});
79+
retValue = retValue.isEmpty ? _dict : retValue;
80+
} catch (e) {
81+
print('i18n exception');
82+
print(e);
83+
retValue = key;
84+
}
85+
// print('key ${key} value: ${retValue}');
86+
return retValue ?? '';
87+
}
88+
89+
static String $t(String key) {
90+
return _inst._t(key);
91+
}
92+
}
93+
94+
class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
95+
final Locale locale;
96+
97+
AppLocalizationsDelegate ([this.locale]);
98+
99+
@override
100+
bool isSupported(Locale locale) {
101+
return I18NConfig.ConfigLanguage.sopportLanguage.keys.toList().contains(locale.languageCode);
102+
}
103+
104+
@override
105+
Future<AppLocalizations> load(Locale _locale) async {
106+
Locale _tmpLocale = locale ?? _locale;
107+
return await AppLocalizations.init(_tmpLocale);
108+
}
109+
110+
@override
111+
bool shouldReload(LocalizationsDelegate<AppLocalizations> old) {
112+
// false时 不执行上述重写函数
113+
return false;
114+
}
115+
}

lib/main.dart

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import 'package:flutter/material.dart';
2+
23
//语言包实例化
34
import 'package:flutter_localizations/flutter_localizations.dart';
4-
import 'package:efox_flutter/lang/application.dart';
5-
import 'package:efox_flutter/lang/app_translations_delegate.dart';
5+
import 'package:efox_flutter/lang/index.dart'
6+
show AppLocalizationsDelegate, AppLocalizations;
7+
import 'package:efox_flutter/lang/config.dart' show ConfigLanguage;
8+
69
//引用Store 层
710
import 'package:efox_flutter/store/index.dart' show model, Store;
11+
812
//路由
913
import 'package:efox_flutter/router/index.dart' show FluroRouter;
14+
1015
//主题
1116
import 'package:efox_flutter/config/theme.dart' show AppTheme;
17+
1218
//统计
1319
import 'package:efox_flutter/utils/analytics.dart' as Analytics;
1420

@@ -18,27 +24,20 @@ class MainApp extends StatefulWidget {
1824
MainApp() {
1925
FluroRouter.initRouter();
2026
}
27+
2128
@override
2229
MainAppState createState() => MainAppState();
2330
}
2431

2532
class MainAppState extends State<MainApp> {
2633
// 定义全局 语言代理
27-
AppTranslationsDelegate _newLocaleDelegate;
34+
AppLocalizationsDelegate _delegate;
2835

2936
@override
3037
void initState() {
3138
//实例化多语言
32-
_newLocaleDelegate = AppTranslationsDelegate(newLocale: null);
33-
application.onLocaleChanged = onLocaleChange;
3439
super.initState();
35-
}
36-
37-
// 切换语言方法
38-
void onLocaleChange(Locale locale) {
39-
setState(() {
40-
_newLocaleDelegate = AppTranslationsDelegate(newLocale: locale);
41-
});
40+
_delegate = AppLocalizationsDelegate();
4241
}
4342

4443
@override
@@ -49,18 +48,23 @@ class MainAppState extends State<MainApp> {
4948
localeResolutionCallback: (deviceLocale, supportedLocales) {
5049
print(
5150
'deviceLocale=$deviceLocale supportedLocales=$supportedLocales');
52-
return deviceLocale ?? Locale('en');
51+
Locale _locale = supportedLocales.contains(deviceLocale)
52+
? deviceLocale
53+
: Locale('en');
54+
return _locale;
55+
},
56+
onGenerateTitle: (context) {
57+
// 设置多语言代理
58+
AppLocalizations.setProxy(setState, _delegate);
59+
return '-';
5360
},
5461
localizationsDelegates: [
55-
_newLocaleDelegate,
5662
GlobalMaterialLocalizations.delegate,
5763
GlobalWidgetsLocalizations.delegate,
64+
_delegate,
5865
],
59-
supportedLocales: [
60-
const Locale('en'),
61-
const Locale('zh'),
62-
],
63-
title: 'Flutter Demo',
66+
supportedLocales: ConfigLanguage.supportedLocales,
67+
// title: 'Flutter Demo',
6468
theme: AppTheme.themData,
6569
onGenerateRoute: FluroRouter.router.generator,
6670
navigatorObservers: <NavigatorObserver>[Analytics.observer],

0 commit comments

Comments
 (0)