Skip to content

Commit a70c780

Browse files
committed
多语言切换完成
1 parent 3a9ea2d commit a70c780

File tree

9 files changed

+81
-15
lines changed

9 files changed

+81
-15
lines changed

lib/common/config/Config.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ class Config {
1313
static const LANGUAGE_SELECT_NAME = "language-select-name";
1414
static const REFRESH_LANGUAGE = "refreshLanguageApp";
1515
static const THEME_COLOR = "theme-color";
16+
static const LOCALE = "locale";
1617
}

lib/common/dao/UserDao.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ class UserDao {
6767
CommonUtils.pushTheme(store, int.parse(themeIndex));
6868
}
6969

70+
///切换语言
71+
String localeIndex = await LocalStorage.get(Config.LOCALE);
72+
if (localeIndex != null && localeIndex.length != 0) {
73+
CommonUtils.changeLocale(store, int.parse(localeIndex));
74+
}
75+
7076
return new DataResult(res.data, (res.result && (token != null)));
7177
}
7278

lib/common/redux/GSYState.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class GSYState {
2929

3030
Locale locale;
3131

32+
Locale platformLocale;
33+
3234
///构造方法
3335
GSYState({this.userInfo, this.eventList, this.trendList, this.themeData, this.locale});
3436
}

lib/common/style/GSYStringBase.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
* Date: 2018-08-15
44
*/
55
abstract class GSYStringBase {
6-
76
String welcomeMessage;
87

98
String app_name;
109

11-
1210
String app_ok;
1311

1412
String app_cancel;
@@ -45,6 +43,8 @@ abstract class GSYStringBase {
4543

4644
String home_reply;
4745

46+
String home_change_language;
47+
4848
String home_about;
4949

5050
String home_check_update;
@@ -55,6 +55,10 @@ abstract class GSYStringBase {
5555

5656
String home_change_theme;
5757

58+
String home_language_default;
59+
String home_language_zh;
60+
String home_language_en;
61+
5862
String home_theme_default;
5963

6064
String home_theme_1;
@@ -217,4 +221,4 @@ abstract class GSYStringBase {
217221
String search_type;
218222
String search_sort;
219223
String search_language;
220-
}
224+
}

lib/common/style/GSYStringEn.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class GSYStringEn extends GSYStringBase {
5858
@override
5959
String home_reply = "Feedback";
6060
@override
61+
String home_change_language = "language";
62+
@override
6163
String home_about = "About";
6264
@override
6365
String home_check_update = "CheckUpdate";
@@ -67,6 +69,12 @@ class GSYStringEn extends GSYStringBase {
6769
String home_user_info = "Profile";
6870
@override
6971
String home_change_theme = "Theme";
72+
@override
73+
String home_language_default= "Default";
74+
@override
75+
String home_language_zh= "Chinese";
76+
@override
77+
String home_language_en= "English";
7078

7179
@override
7280
String home_theme_default = "Default";

lib/common/style/GSYStringZh.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class GSYStringZh extends GSYStringBase {
5858
@override
5959
String home_reply = "问题反馈";
6060
@override
61+
String home_change_language = "语言切换";
62+
@override
6163
String home_about = "关于";
6264
@override
6365
String home_check_update = "检测更新";
@@ -67,6 +69,12 @@ class GSYStringZh extends GSYStringBase {
6769
String home_user_info = "个人信息";
6870
@override
6971
String home_change_theme = "切换主题";
72+
@override
73+
String home_language_default= "默认";
74+
@override
75+
String home_language_zh= "中文";
76+
@override
77+
String home_language_en= "英文";
7078

7179
@override
7280
String home_theme_default = "默认主题";

lib/common/utils/CommonUtils.dart

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import 'package:flutter/services.dart';
66
import 'package:fluttertoast/fluttertoast.dart';
77
import 'package:gsy_github_app_flutter/common/localization/DefaultLocalizations.dart';
88
import 'package:gsy_github_app_flutter/common/net/Address.dart';
9+
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
10+
import 'package:gsy_github_app_flutter/common/redux/LocaleRedux.dart';
911
import 'package:gsy_github_app_flutter/common/redux/ThemeRedux.dart';
1012
import 'package:gsy_github_app_flutter/common/style/GSYStringBase.dart';
1113
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
@@ -92,8 +94,20 @@ class CommonUtils {
9294
store.dispatch(new RefreshThemeDataAction(themeData));
9395
}
9496

95-
static changeLocale() {
96-
97+
/**
98+
* 切换语言
99+
*/
100+
static changeLocale(Store<GSYState> store, int index) {
101+
Locale locale = store.state.platformLocale;
102+
switch (index) {
103+
case 1:
104+
locale = Locale('zh', 'CH');
105+
break;
106+
case 2:
107+
locale = Locale('en', 'US');
108+
break;
109+
}
110+
store.dispatch(RefreshLocaleAction(locale));
97111
}
98112

99113
static GSYStringBase getLocale(BuildContext context) {
@@ -166,7 +180,8 @@ class CommonUtils {
166180
if (url.startsWith("http")) {
167181
NavigatorUtils.goGSYWebView(context, url, title);
168182
} else {
169-
NavigatorUtils.goGSYWebView(context, new Uri.dataFromString(url, mimeType: 'text/html', encoding: Encoding.getByName("utf-8")).toString(), title);
183+
NavigatorUtils.goGSYWebView(
184+
context, new Uri.dataFromString(url, mimeType: 'text/html', encoding: Encoding.getByName("utf-8")).toString(), title);
170185
}
171186
}
172187

lib/main.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,37 @@ class FlutterReduxApp extends StatelessWidget {
5151
theme: store.state.themeData,
5252
routes: {
5353
WelcomePage.sName: (context) {
54+
store.state.platformLocale = Localizations.localeOf(context);
5455
return WelcomePage();
5556
},
5657
HomePage.sName: (context) {
57-
return new FreeLocalizations(
58+
return new GSYLocalizations(
5859
child: new HomePage(),
5960
);
6061
},
6162
LoginPage.sName: (context) {
62-
return LoginPage();
63+
return new GSYLocalizations(
64+
child: new LoginPage(),
65+
);
6366
},
6467
});
6568
}),
6669
);
6770
}
6871
}
6972

70-
class FreeLocalizations extends StatefulWidget {
73+
class GSYLocalizations extends StatefulWidget {
7174
final Widget child;
7275

73-
FreeLocalizations({Key key, this.child}) : super(key: key);
76+
GSYLocalizations({Key key, this.child}) : super(key: key);
7477

7578
@override
76-
State<FreeLocalizations> createState() {
77-
return new _FreeLocalizations();
79+
State<GSYLocalizations> createState() {
80+
return new _GSYLocalizations();
7881
}
7982
}
8083

81-
class _FreeLocalizations extends State<FreeLocalizations> {
84+
class _GSYLocalizations extends State<GSYLocalizations> {
8285
@override
8386
Widget build(BuildContext context) {
8487
return new StoreBuilder<GSYState>(builder: (context, store) {

lib/widget/HomeDrawer.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import 'package:gsy_github_app_flutter/common/local/LocalStorage.dart';
1111
import 'package:gsy_github_app_flutter/common/localization/DefaultLocalizations.dart';
1212
import 'package:gsy_github_app_flutter/common/model/User.dart';
1313
import 'package:gsy_github_app_flutter/common/redux/GSYState.dart';
14-
import 'package:gsy_github_app_flutter/common/redux/LocaleRedux.dart';
1514
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
1615
import 'package:gsy_github_app_flutter/common/utils/CommonUtils.dart';
1716
import 'package:gsy_github_app_flutter/common/utils/NavigatorUtils.dart';
@@ -52,6 +51,19 @@ class HomeDrawer extends StatelessWidget {
5251
}, colorList: CommonUtils.getThemeListColor());
5352
}
5453

54+
showLanguageDialog(BuildContext context, Store store) {
55+
List<String> list = [
56+
CommonUtils.getLocale(context).home_language_default,
57+
CommonUtils.getLocale(context).home_language_zh,
58+
CommonUtils.getLocale(context).home_language_en,
59+
];
60+
CommonUtils.showCommitOptionDialog(context, list, (index) {
61+
CommonUtils.changeLocale(store, index);
62+
LocalStorage.save(Config.LOCALE, index.toString());
63+
}, colorList: CommonUtils.getThemeListColor(), height: 150.0);
64+
}
65+
66+
5567
@override
5668
Widget build(BuildContext context) {
5769
return new StoreBuilder<GSYState>(
@@ -138,6 +150,14 @@ class HomeDrawer extends StatelessWidget {
138150
onTap: () {
139151
showThemeDialog(context, store);
140152
}),
153+
new ListTile(
154+
title: new Text(
155+
CommonUtils.getLocale(context).home_change_language,
156+
style: GSYConstant.normalText,
157+
),
158+
onTap: () {
159+
showLanguageDialog(context, store);
160+
}),
141161
new ListTile(
142162
title: new Text(
143163
CommonUtils.getLocale(context).home_check_update,
@@ -154,7 +174,6 @@ class HomeDrawer extends StatelessWidget {
154174
onTap: () {
155175
GetVersion.projectVersion.then((value) {
156176
showAboutDialog(context, value);
157-
store.dispatch(RefreshLocaleAction(Locale('en', 'US')));
158177
});
159178
}),
160179
new ListTile(

0 commit comments

Comments
 (0)