Skip to content

Commit d32befb

Browse files
authored
Merge pull request #2130 from ImranR98/dev
Bugfix - app crashes when language set to zh_Hant_TW
2 parents dcf9f57 + 94d8295 commit d32befb

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

lib/main.dart

+6-9
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import 'package:easy_localization/src/localization.dart';
2323
List<MapEntry<Locale, String>> supportedLocales = const [
2424
MapEntry(Locale('en'), 'English'),
2525
MapEntry(Locale('zh'), '简体中文'),
26-
MapEntry(Locale('zh_Hant_TW'), '臺灣話'),
26+
MapEntry(Locale('zh', 'Hant_TW'), '臺灣話'),
2727
MapEntry(Locale('it'), 'Italiano'),
2828
MapEntry(Locale('ja'), '日本語'),
2929
MapEntry(Locale('hu'), 'Magyar'),
@@ -61,11 +61,11 @@ Future<void> loadTranslations() async {
6161
var forceLocale = s.forcedLocale;
6262
final controller = EasyLocalizationController(
6363
saveLocale: true,
64-
forceLocale: forceLocale != null ? Locale(forceLocale) : null,
64+
forceLocale: forceLocale,
6565
fallbackLocale: fallbackLocale,
6666
supportedLocales: supportedLocales.map((e) => e.key).toList(),
6767
assetLoader: const RootBundleAssetLoader(),
68-
useOnlyLangCode: true,
68+
useOnlyLangCode: false,
6969
useFallbackTranslations: true,
7070
path: localeDir,
7171
onLoadError: (FlutterError e) {
@@ -119,7 +119,7 @@ void main() async {
119119
supportedLocales: supportedLocales.map((e) => e.key).toList(),
120120
path: localeDir,
121121
fallbackLocale: fallbackLocale,
122-
useOnlyLangCode: true,
122+
useOnlyLangCode: false,
123123
child: const Obtainium()),
124124
));
125125
BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
@@ -203,12 +203,9 @@ class _ObtainiumState extends State<Obtainium> {
203203
});
204204
}
205205
}
206-
if (!supportedLocales
207-
.map((e) => e.key.languageCode)
208-
.contains(context.locale.languageCode) ||
206+
if (!supportedLocales.map((e) => e.key).contains(context.locale) ||
209207
(settingsProvider.forcedLocale == null &&
210-
context.deviceLocale.languageCode !=
211-
context.locale.languageCode)) {
208+
context.deviceLocale != context.locale)) {
212209
settingsProvider.resetLocaleSafe(context);
213210
}
214211
}

lib/pages/settings.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,14 @@ class _SettingsPageState extends State<SettingsPage> {
262262
child: Text(tr('followSystem')),
263263
),
264264
...supportedLocales.map((e) => DropdownMenuItem(
265-
value: e.key.toLanguageTag(),
265+
value: e.key,
266266
child: Text(e.value),
267267
))
268268
],
269269
onChanged: (value) {
270270
settingsProvider.forcedLocale = value;
271271
if (value != null) {
272-
context.setLocale(Locale(value));
272+
context.setLocale(value);
273273
} else {
274274
settingsProvider.resetLocaleSafe(context);
275275
}

lib/providers/settings_provider.dart

+11-11
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,24 @@ class SettingsProvider with ChangeNotifier {
261261
notifyListeners();
262262
}
263263

264-
String? get forcedLocale {
265-
var fl = prefs?.getString('forcedLocale');
266-
return supportedLocales
267-
.where((element) => element.key.toLanguageTag() == fl)
268-
.isNotEmpty
264+
Locale? get forcedLocale {
265+
var flSegs = prefs?.getString('forcedLocale')?.split('-');
266+
var fl = flSegs != null && flSegs.isNotEmpty
267+
? Locale(flSegs[0], flSegs.length > 1 ? flSegs[1] : null)
268+
: null;
269+
var set = supportedLocales.where((element) => element.key == fl).isNotEmpty
269270
? fl
270271
: null;
272+
return set;
271273
}
272274

273-
set forcedLocale(String? fl) {
275+
set forcedLocale(Locale? fl) {
274276
if (fl == null) {
275277
prefs?.remove('forcedLocale');
276278
} else if (supportedLocales
277-
.where((element) => element.key.toLanguageTag() == fl)
279+
.where((element) => element.key == fl)
278280
.isNotEmpty) {
279-
prefs?.setString('forcedLocale', fl);
281+
prefs?.setString('forcedLocale', fl.toLanguageTag());
280282
}
281283
notifyListeners();
282284
}
@@ -285,9 +287,7 @@ class SettingsProvider with ChangeNotifier {
285287
a.length == b.length && a.union(b).length == a.length;
286288

287289
void resetLocaleSafe(BuildContext context) {
288-
if (context.supportedLocales
289-
.map((e) => e.languageCode)
290-
.contains(context.deviceLocale.languageCode)) {
290+
if (context.supportedLocales.contains(context.deviceLocale)) {
291291
context.resetLocale();
292292
} else {
293293
context.setLocale(context.fallbackLocale!);

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
1616
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
1717
# In Windows, build-name is used as the major, minor, and patch parts
1818
# of the product and file versions while build-number is used as the build suffix.
19-
version: 1.1.42+2299
19+
version: 1.1.43+2300
2020

2121
environment:
2222
sdk: ^3.6.0

0 commit comments

Comments
 (0)