Merge pull request #2130 from ImranR98/dev

Bugfix - app crashes when language set to zh_Hant_TW
This commit is contained in:
Imran
2025-02-21 11:52:02 -06:00
committed by GitHub
4 changed files with 20 additions and 23 deletions

View File

@ -23,7 +23,7 @@ import 'package:easy_localization/src/localization.dart';
List<MapEntry<Locale, String>> supportedLocales = const [ List<MapEntry<Locale, String>> supportedLocales = const [
MapEntry(Locale('en'), 'English'), MapEntry(Locale('en'), 'English'),
MapEntry(Locale('zh'), '简体中文'), MapEntry(Locale('zh'), '简体中文'),
MapEntry(Locale('zh_Hant_TW'), '臺灣話'), MapEntry(Locale('zh', 'Hant_TW'), '臺灣話'),
MapEntry(Locale('it'), 'Italiano'), MapEntry(Locale('it'), 'Italiano'),
MapEntry(Locale('ja'), '日本語'), MapEntry(Locale('ja'), '日本語'),
MapEntry(Locale('hu'), 'Magyar'), MapEntry(Locale('hu'), 'Magyar'),
@ -61,11 +61,11 @@ Future<void> loadTranslations() async {
var forceLocale = s.forcedLocale; var forceLocale = s.forcedLocale;
final controller = EasyLocalizationController( final controller = EasyLocalizationController(
saveLocale: true, saveLocale: true,
forceLocale: forceLocale != null ? Locale(forceLocale) : null, forceLocale: forceLocale,
fallbackLocale: fallbackLocale, fallbackLocale: fallbackLocale,
supportedLocales: supportedLocales.map((e) => e.key).toList(), supportedLocales: supportedLocales.map((e) => e.key).toList(),
assetLoader: const RootBundleAssetLoader(), assetLoader: const RootBundleAssetLoader(),
useOnlyLangCode: true, useOnlyLangCode: false,
useFallbackTranslations: true, useFallbackTranslations: true,
path: localeDir, path: localeDir,
onLoadError: (FlutterError e) { onLoadError: (FlutterError e) {
@ -119,7 +119,7 @@ void main() async {
supportedLocales: supportedLocales.map((e) => e.key).toList(), supportedLocales: supportedLocales.map((e) => e.key).toList(),
path: localeDir, path: localeDir,
fallbackLocale: fallbackLocale, fallbackLocale: fallbackLocale,
useOnlyLangCode: true, useOnlyLangCode: false,
child: const Obtainium()), child: const Obtainium()),
)); ));
BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask); BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
@ -203,12 +203,9 @@ class _ObtainiumState extends State<Obtainium> {
}); });
} }
} }
if (!supportedLocales if (!supportedLocales.map((e) => e.key).contains(context.locale) ||
.map((e) => e.key.languageCode)
.contains(context.locale.languageCode) ||
(settingsProvider.forcedLocale == null && (settingsProvider.forcedLocale == null &&
context.deviceLocale.languageCode != context.deviceLocale != context.locale)) {
context.locale.languageCode)) {
settingsProvider.resetLocaleSafe(context); settingsProvider.resetLocaleSafe(context);
} }
} }

View File

@ -262,14 +262,14 @@ class _SettingsPageState extends State<SettingsPage> {
child: Text(tr('followSystem')), child: Text(tr('followSystem')),
), ),
...supportedLocales.map((e) => DropdownMenuItem( ...supportedLocales.map((e) => DropdownMenuItem(
value: e.key.toLanguageTag(), value: e.key,
child: Text(e.value), child: Text(e.value),
)) ))
], ],
onChanged: (value) { onChanged: (value) {
settingsProvider.forcedLocale = value; settingsProvider.forcedLocale = value;
if (value != null) { if (value != null) {
context.setLocale(Locale(value)); context.setLocale(value);
} else { } else {
settingsProvider.resetLocaleSafe(context); settingsProvider.resetLocaleSafe(context);
} }

View File

@ -261,22 +261,24 @@ class SettingsProvider with ChangeNotifier {
notifyListeners(); notifyListeners();
} }
String? get forcedLocale { Locale? get forcedLocale {
var fl = prefs?.getString('forcedLocale'); var flSegs = prefs?.getString('forcedLocale')?.split('-');
return supportedLocales var fl = flSegs != null && flSegs.isNotEmpty
.where((element) => element.key.toLanguageTag() == fl) ? Locale(flSegs[0], flSegs.length > 1 ? flSegs[1] : null)
.isNotEmpty : null;
var set = supportedLocales.where((element) => element.key == fl).isNotEmpty
? fl ? fl
: null; : null;
return set;
} }
set forcedLocale(String? fl) { set forcedLocale(Locale? fl) {
if (fl == null) { if (fl == null) {
prefs?.remove('forcedLocale'); prefs?.remove('forcedLocale');
} else if (supportedLocales } else if (supportedLocales
.where((element) => element.key.toLanguageTag() == fl) .where((element) => element.key == fl)
.isNotEmpty) { .isNotEmpty) {
prefs?.setString('forcedLocale', fl); prefs?.setString('forcedLocale', fl.toLanguageTag());
} }
notifyListeners(); notifyListeners();
} }
@ -285,9 +287,7 @@ class SettingsProvider with ChangeNotifier {
a.length == b.length && a.union(b).length == a.length; a.length == b.length && a.union(b).length == a.length;
void resetLocaleSafe(BuildContext context) { void resetLocaleSafe(BuildContext context) {
if (context.supportedLocales if (context.supportedLocales.contains(context.deviceLocale)) {
.map((e) => e.languageCode)
.contains(context.deviceLocale.languageCode)) {
context.resetLocale(); context.resetLocale();
} else { } else {
context.setLocale(context.fallbackLocale!); context.setLocale(context.fallbackLocale!);

View File

@ -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 # 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 # 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. # 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: environment:
sdk: ^3.6.0 sdk: ^3.6.0