Files
flutter/lib/helpers/locale.dart
2026-05-18 09:40:12 +02:00

53 lines
2.0 KiB
Dart

/*
* 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;
}
/// Stable string encoding for a [Locale], matching the keys generated by
/// Flutter's AppLocalizations (e.g. `pt_BR`, `zh_Hant`, `pl`). [scriptCode]
/// takes precedence over [countryCode] so locales like `zh_Hant` round-trip
/// distinctly from plain `zh`.
String encodeLocale(Locale locale) {
final script = locale.scriptCode;
if (script != null && script.isNotEmpty) {
return '${locale.languageCode}_$script';
}
final country = locale.countryCode;
if (country != null && country.isNotEmpty) {
return '${locale.languageCode}_$country';
}
return locale.languageCode;
}