Skip to content

Commit

Permalink
fix: Language parsing not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Losses committed Dec 5, 2024
1 parent bd03c60 commit 397373c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
16 changes: 11 additions & 5 deletions lib/screens/settings_language/settings_language.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,17 @@ class _SettingsLanguageState extends State<SettingsLanguage> {
});

if (newLocale != null) {
final serializedLocale = [
newLocale.languageCode,
if (newLocale.scriptCode != null) newLocale.scriptCode,
if (newLocale.countryCode != null) newLocale.countryCode,
].join('_');
final List<String> 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 {
Expand Down
35 changes: 20 additions & 15 deletions lib/utils/locale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 397373c

Please sign in to comment.