/* * 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; } /// Stable string encoding for a [Locale], matching the keys generated by /// Flutter's AppLocalizations (e.g. `pt_BR`, `zh_Hant`, `pl`). /// [Locale.scriptCode] takes precedence over [Locale.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; }