From e22f7ebbe31ec41facbba297dd6de47cb36214dd Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Thu, 7 May 2026 11:23:48 +0200 Subject: [PATCH] Fix locale resolution logic The previous logic didn't consult the app's supportedLocales. For the Android 13 per-app picker this was fine (the user picked a locale that's also in `locales_config.xml`), but for users whose primary locale wasn't in the supportedLocales list but whose secondary one *was*, the app would fall through to English instead of honoring the secondary --- lib/helpers/locale.dart | 36 ++++++++++++ lib/main.dart | 8 +-- test/other/locale_resolution_test.dart | 78 ++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 lib/helpers/locale.dart create mode 100644 test/other/locale_resolution_test.dart diff --git a/lib/helpers/locale.dart b/lib/helpers/locale.dart new file mode 100644 index 000000000..c9e1073cf --- /dev/null +++ b/lib/helpers/locale.dart @@ -0,0 +1,36 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 - 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/widgets.dart'; + +/// Resolves the active locale for [WidgetsApp.localeListResolutionCallback]. +/// +/// If the first device locale shares a language with any [supportedLocales], +/// it is returned directly. Otherwise `null` is returned so Flutter's default +/// `basicLocaleListResolution` can walk the rest of the preference list and +/// find the best supported match. +Locale? resolveLocale(List? locales, Iterable supportedLocales) { + if (locales == null || locales.isEmpty) { + return null; + } + final first = locales.first; + if (supportedLocales.any((s) => s.languageCode == first.languageCode)) { + return first; + } + return null; +} diff --git a/lib/main.dart b/lib/main.dart index 7d1079641..0812357ee 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -24,6 +24,7 @@ import 'package:provider/provider.dart'; import 'package:wger/core/exceptions/http_exception.dart'; import 'package:wger/core/locator.dart'; import 'package:wger/helpers/errors.dart'; +import 'package:wger/helpers/locale.dart'; import 'package:wger/helpers/shared_preferences.dart'; import 'package:wger/l10n/generated/app_localizations.dart'; import 'package:wger/providers/add_exercise.dart'; @@ -279,12 +280,7 @@ class MainApp extends StatelessWidget { const ConfigureDashboardWidgetsScreen(), TrophyScreen.routeName: (ctx) => const TrophyScreen(), }, - localeListResolutionCallback: (locales, supportedLocales) { - if (locales != null && locales.isNotEmpty) { - return locales.first; - } - return null; - }, + localeListResolutionCallback: resolveLocale, localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, ); diff --git a/test/other/locale_resolution_test.dart b/test/other/locale_resolution_test.dart new file mode 100644 index 000000000..faf88e279 --- /dev/null +++ b/test/other/locale_resolution_test.dart @@ -0,0 +1,78 @@ +/* + * This file is part of wger Workout Manager . + * Copyright (c) 2026 - 2026 wger Team + * + * wger Workout Manager is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:wger/helpers/locale.dart'; +import 'package:wger/l10n/generated/app_localizations.dart'; + +void main() { + const supported = AppLocalizations.supportedLocales; + + group('resolveLocale', () { + test('returns null when device locale list is null', () { + expect(resolveLocale(null, supported), isNull); + }); + + test('returns null when device locale list is empty', () { + expect(resolveLocale([], supported), isNull); + }); + + test('returns the first locale when its language is supported', () { + // Simulates Android 13 per-app language picker: user picked Spanish. + final result = resolveLocale([const Locale('es')], supported); + expect(result, const Locale('es')); + }); + + test('returns the first locale even with country code (e.g. pt-BR)', () { + final result = resolveLocale([const Locale('pt', 'BR')], supported); + expect(result, const Locale('pt', 'BR')); + }); + + test('returns the first locale when only language matches (e.g. es-MX → es)', () { + // Device locale es-MX is not in supportedLocales explicitly, but Spanish is. + // We return the device locale and let AppLocalizations' delegate + // do the language-code lookup. + final result = resolveLocale([const Locale('es', 'MX')], supported); + expect(result, const Locale('es', 'MX')); + }); + + test('returns null when first locale is unsupported, falling through to Flutter default', () { + // Vietnamese is not in supportedLocales. Returning null lets Flutter's + // basicLocaleListResolution walk the remaining preferences. + final result = resolveLocale([const Locale('vi')], supported); + expect(result, isNull); + }); + + test('returns null when primary is unsupported even if a later preference is supported', () { + // Returning null here lets Flutter's basicLocaleListResolution walk the + // rest of the preference list and pick the supported `de`. + final result = resolveLocale( + [const Locale('vi'), const Locale('de')], + supported, + ); + expect(result, isNull); + }); + + test('honours supported locales passed in (decoupled from AppLocalizations)', () { + const tiny = [Locale('en'), Locale('de')]; + expect(resolveLocale([const Locale('de')], tiny), const Locale('de')); + expect(resolveLocale([const Locale('fr')], tiny), isNull); + }); + }); +}