From 397373c4c8e5b4dcb1ab8f38474444e09aad7b9e Mon Sep 17 00:00:00 2001 From: LOSSES Don <1384036+Losses@users.noreply.github.com> Date: Thu, 5 Dec 2024 16:16:29 +0800 Subject: [PATCH] fix: Language parsing not working --- .../settings_language/settings_language.dart | 16 ++++++--- lib/utils/locale.dart | 35 +++++++++++-------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/lib/screens/settings_language/settings_language.dart b/lib/screens/settings_language/settings_language.dart index 5844c59ec..39d508923 100644 --- a/lib/screens/settings_language/settings_language.dart +++ b/lib/screens/settings_language/settings_language.dart @@ -49,11 +49,17 @@ class _SettingsLanguageState extends State { }); if (newLocale != null) { - final serializedLocale = [ - newLocale.languageCode, - if (newLocale.scriptCode != null) newLocale.scriptCode, - if (newLocale.countryCode != null) newLocale.countryCode, - ].join('_'); + final List parts = [newLocale.languageCode]; + + if (newLocale.scriptCode != null) { + parts.add('s_${newLocale.scriptCode}'); + } + + if (newLocale.countryCode != null) { + parts.add('c_${newLocale.countryCode}'); + } + + final serializedLocale = parts.join('|'); await _settingsManager.setValue(localeKey, serializedLocale); } else { diff --git a/lib/utils/locale.dart b/lib/utils/locale.dart index 3ec9d5573..1b5f7fbb6 100644 --- a/lib/utils/locale.dart +++ b/lib/utils/locale.dart @@ -3,23 +3,28 @@ import 'dart:ui'; Locale? localeFromString(String? x) { if (x == null) return null; - final parts = x.split('_'); - if (parts.isNotEmpty) { - if (parts.length == 3) { - return Locale.fromSubtags( - languageCode: parts[0], - scriptCode: parts[1], - countryCode: parts[2], - ); - } else if (parts.length == 2) { - return Locale.fromSubtags( - languageCode: parts[0], - countryCode: parts[1], - ); - } else if (parts.length == 1) { - return Locale(parts[0]); + final parts = x.split('|'); + String? languageCode; + String? scriptCode; + String? countryCode; + + for (var part in parts) { + if (part.startsWith('s_')) { + scriptCode = part.substring(2); + } else if (part.startsWith('c_')) { + countryCode = part.substring(2); + } else { + languageCode ??= part; } } + if (languageCode != null) { + return Locale.fromSubtags( + languageCode: languageCode, + scriptCode: scriptCode, + countryCode: countryCode, + ); + } + return null; }