fix: Locale selection issue with country-specific locales (#383)

This commit is contained in:
Tio 2025-06-11 15:50:25 -03:00 committed by GitHub
parent 3d7a654096
commit f3e920ac79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 19 additions and 9 deletions

View file

@ -288,13 +288,15 @@ class _MainState extends ConsumerState<Main> with WindowListener, WidgetsBinding
),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
builder: (context, child) => Localizations.override(
context: context,
locale: AppLocalizations.supportedLocales.firstWhere(
(element) => element.languageCode == language.languageCode,
orElse: () => const Locale('en', "GB"),
),
child: LocalizationContextWrapper(child: ScaffoldMessenger(child: child ?? Container())),
locale: language,
localeResolutionCallback: (locale, supportedLocales) {
if (locale == null || !supportedLocales.contains(locale)) {
return const Locale('en');
}
return locale;
},
builder: (context, child) => LocalizationContextWrapper(
child: ScaffoldMessenger(child: child ?? Container()),
),
debugShowCheckedModeBanner: false,
darkTheme: darkTheme.copyWith(

View file

@ -31,7 +31,7 @@ List<Widget> buildClientSettingsVisual(
context: context,
locale: ref.watch(clientSettingsProvider.select((value) => (value.selectedLocale ?? currentLocale))),
child: Builder(builder: (context) {
String language = "Unknown";
String language = "English";
try {
language = context.localized.nativeName;
} catch (_) {}
@ -46,7 +46,7 @@ List<Widget> buildClientSettingsVisual(
context: context,
locale: entry,
child: Builder(builder: (context) {
return Text("${context.localized.nativeName} (${entry.languageCode.toUpperCase()})");
return Text("${context.localized.nativeName} (${entry.toDisplayCode()})");
}),
),
onTap: () => ref

View file

@ -31,3 +31,11 @@ class _LocalizationContextWrapperState extends ConsumerState<LocalizationContext
@override
Widget build(BuildContext context) => widget.child;
}
extension LocaleDisplayCodeExtension on Locale {
String toDisplayCode() {
return countryCode != null && countryCode!.isNotEmpty
? "${languageCode.toUpperCase()}-${countryCode!.toUpperCase()}"
: languageCode.toUpperCase();
}
}