mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 01:18:35 +01:00
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
This commit is contained in:
36
lib/helpers/locale.dart
Normal file
36
lib/helpers/locale.dart
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Locale>? locales, Iterable<Locale> supportedLocales) {
|
||||
if (locales == null || locales.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
final first = locales.first;
|
||||
if (supportedLocales.any((s) => s.languageCode == first.languageCode)) {
|
||||
return first;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -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,
|
||||
);
|
||||
|
||||
78
test/other/locale_resolution_test.dart
Normal file
78
test/other/locale_resolution_test.dart
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user