From f3e920ac79b18132f2d1944f3f29743959cdbb70 Mon Sep 17 00:00:00 2001 From: Tio <70031990+Uncle-Tio@users.noreply.github.com> Date: Wed, 11 Jun 2025 15:50:25 -0300 Subject: [PATCH] fix: Locale selection issue with country-specific locales (#383) --- lib/main.dart | 16 +++++++++------- .../client_sections/client_settings_visual.dart | 4 ++-- lib/util/localization_helper.dart | 8 ++++++++ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 86f3aeb..e21ea46 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -288,13 +288,15 @@ class _MainState extends ConsumerState
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( diff --git a/lib/screens/settings/client_sections/client_settings_visual.dart b/lib/screens/settings/client_sections/client_settings_visual.dart index a9a7ef2..5666573 100644 --- a/lib/screens/settings/client_sections/client_settings_visual.dart +++ b/lib/screens/settings/client_sections/client_settings_visual.dart @@ -31,7 +31,7 @@ List 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 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 diff --git a/lib/util/localization_helper.dart b/lib/util/localization_helper.dart index 78cea31..2d2dc6e 100644 --- a/lib/util/localization_helper.dart +++ b/lib/util/localization_helper.dart @@ -31,3 +31,11 @@ class _LocalizationContextWrapperState extends ConsumerState widget.child; } + +extension LocaleDisplayCodeExtension on Locale { + String toDisplayCode() { + return countryCode != null && countryCode!.isNotEmpty + ? "${languageCode.toUpperCase()}-${countryCode!.toUpperCase()}" + : languageCode.toUpperCase(); + } +}