Skip to content

Commit 3064e79

Browse files
fix #902 language error
1 parent 46bed7c commit 3064e79

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

lib/app.dart

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ class _FlutterReduxAppState extends State<FlutterReduxApp>
4848

4949
NavigatorObserver navigatorObserver = NavigatorObserver();
5050

51+
// Helper method to check if the locale is supported
52+
Locale _checkSupportedLocale(Locale locale) {
53+
// Define the supported locales
54+
final supportedLocales = GSYLocalizations.localizedValues;
55+
56+
// Check if the requested locale is supported
57+
for (final supportedLocale in supportedLocales.keys) {
58+
if (supportedLocale == locale.languageCode) {
59+
return locale;
60+
}
61+
}
62+
63+
// Fall back to English if the locale is not supported
64+
return const Locale('en', 'US');
65+
}
66+
67+
68+
5169
@override
5270
void initState() {
5371
super.initState();
@@ -71,6 +89,11 @@ class _FlutterReduxAppState extends State<FlutterReduxApp>
7189
builder: (BuildContext context, WidgetRef ref, Widget? child) {
7290
final (greyApp, appLocale, themeData) = ref.watch(appStateProvider);
7391

92+
93+
94+
// Make sure the locale is supported or fall back to a default one
95+
final effectiveLocale = _checkSupportedLocale(appLocale);
96+
7497
/// 使用 flutter_redux 做部分状态共享
7598
/// 通过 StoreProvider 应用 store
7699
/// 这里是为了展示使用 flutter_redux 的能力所以使用了多种状态管理
@@ -87,8 +110,8 @@ class _FlutterReduxAppState extends State<FlutterReduxApp>
87110
GlobalWidgetsLocalizations.delegate,
88111
GSYLocalizationsDelegate.delegate,
89112
],
90-
supportedLocales: [appLocale],
91-
locale: appLocale,
113+
supportedLocales: [effectiveLocale],
114+
locale: effectiveLocale,
92115
theme: themeData,
93116
navigatorObservers: [navigatorObserver],
94117

lib/common/localization/default_localizations.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ class GSYLocalizations {
1313

1414
///根据不同 locale.languageCode 加载不同语言对应
1515
///GSYStringEn和GSYStringZh都继承了GSYStringBase
16-
static final Map<String, GSYStringBase> _localizedValues = {
16+
static final Map<String, GSYStringBase> localizedValues = {
1717
'en': GSYStringEn(),
1818
'zh': GSYStringZh(),
1919
'ja': GSYStringJa(),
2020
'ko': GSYStringKo(),
2121
};
2222

2323
GSYStringBase? get currentLocalized {
24-
return _localizedValues[locale.languageCode];
24+
return localizedValues[locale.languageCode];
2525
}
2626

2727
///通过 Localizations 加载当前的 GSYLocalizations

0 commit comments

Comments
 (0)