From 94d829599255668c2e8d56a639e38c36d0c6df6b Mon Sep 17 00:00:00 2001 From: Imran Remtulla Date: Fri, 21 Feb 2025 12:51:44 -0500 Subject: [PATCH] Bugfix - app crashes when language set to zh_Hant_TW --- lib/main.dart | 15 ++++++--------- lib/pages/settings.dart | 4 ++-- lib/providers/settings_provider.dart | 22 +++++++++++----------- pubspec.yaml | 2 +- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index e34b1ea..4b20795 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -23,7 +23,7 @@ import 'package:easy_localization/src/localization.dart'; List> supportedLocales = const [ MapEntry(Locale('en'), 'English'), MapEntry(Locale('zh'), '简体中文'), - MapEntry(Locale('zh_Hant_TW'), '臺灣話'), + MapEntry(Locale('zh', 'Hant_TW'), '臺灣話'), MapEntry(Locale('it'), 'Italiano'), MapEntry(Locale('ja'), '日本語'), MapEntry(Locale('hu'), 'Magyar'), @@ -61,11 +61,11 @@ Future loadTranslations() async { var forceLocale = s.forcedLocale; final controller = EasyLocalizationController( saveLocale: true, - forceLocale: forceLocale != null ? Locale(forceLocale) : null, + forceLocale: forceLocale, fallbackLocale: fallbackLocale, supportedLocales: supportedLocales.map((e) => e.key).toList(), assetLoader: const RootBundleAssetLoader(), - useOnlyLangCode: true, + useOnlyLangCode: false, useFallbackTranslations: true, path: localeDir, onLoadError: (FlutterError e) { @@ -119,7 +119,7 @@ void main() async { supportedLocales: supportedLocales.map((e) => e.key).toList(), path: localeDir, fallbackLocale: fallbackLocale, - useOnlyLangCode: true, + useOnlyLangCode: false, child: const Obtainium()), )); BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask); @@ -203,12 +203,9 @@ class _ObtainiumState extends State { }); } } - if (!supportedLocales - .map((e) => e.key.languageCode) - .contains(context.locale.languageCode) || + if (!supportedLocales.map((e) => e.key).contains(context.locale) || (settingsProvider.forcedLocale == null && - context.deviceLocale.languageCode != - context.locale.languageCode)) { + context.deviceLocale != context.locale)) { settingsProvider.resetLocaleSafe(context); } } diff --git a/lib/pages/settings.dart b/lib/pages/settings.dart index 42ee8f1..65495e9 100644 --- a/lib/pages/settings.dart +++ b/lib/pages/settings.dart @@ -262,14 +262,14 @@ class _SettingsPageState extends State { child: Text(tr('followSystem')), ), ...supportedLocales.map((e) => DropdownMenuItem( - value: e.key.toLanguageTag(), + value: e.key, child: Text(e.value), )) ], onChanged: (value) { settingsProvider.forcedLocale = value; if (value != null) { - context.setLocale(Locale(value)); + context.setLocale(value); } else { settingsProvider.resetLocaleSafe(context); } diff --git a/lib/providers/settings_provider.dart b/lib/providers/settings_provider.dart index acc8b1e..7af479b 100644 --- a/lib/providers/settings_provider.dart +++ b/lib/providers/settings_provider.dart @@ -261,22 +261,24 @@ class SettingsProvider with ChangeNotifier { notifyListeners(); } - String? get forcedLocale { - var fl = prefs?.getString('forcedLocale'); - return supportedLocales - .where((element) => element.key.toLanguageTag() == fl) - .isNotEmpty + Locale? get forcedLocale { + var flSegs = prefs?.getString('forcedLocale')?.split('-'); + var fl = flSegs != null && flSegs.isNotEmpty + ? Locale(flSegs[0], flSegs.length > 1 ? flSegs[1] : null) + : null; + var set = supportedLocales.where((element) => element.key == fl).isNotEmpty ? fl : null; + return set; } - set forcedLocale(String? fl) { + set forcedLocale(Locale? fl) { if (fl == null) { prefs?.remove('forcedLocale'); } else if (supportedLocales - .where((element) => element.key.toLanguageTag() == fl) + .where((element) => element.key == fl) .isNotEmpty) { - prefs?.setString('forcedLocale', fl); + prefs?.setString('forcedLocale', fl.toLanguageTag()); } notifyListeners(); } @@ -285,9 +287,7 @@ class SettingsProvider with ChangeNotifier { a.length == b.length && a.union(b).length == a.length; void resetLocaleSafe(BuildContext context) { - if (context.supportedLocales - .map((e) => e.languageCode) - .contains(context.deviceLocale.languageCode)) { + if (context.supportedLocales.contains(context.deviceLocale)) { context.resetLocale(); } else { context.setLocale(context.fallbackLocale!); diff --git a/pubspec.yaml b/pubspec.yaml index 7d81647..e9730b2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.1.42+2299 +version: 1.1.43+2300 environment: sdk: ^3.6.0