Files
TubeCards/lib/utils/locale_utils.dart
friebetill 80f218097d Initial commit
Add Space version 2.0.1
2022-03-28 14:56:00 +02:00

29 lines
969 B
Dart

import 'dart:ui';
/// The default locale to be used in case we could not find a matching locale
/// that is supported.
const defaultLocale = Locale('en', '');
/// Returns the best matching supported locale.
///
/// Currently, the most fitting locale is only determined on the basis of the
/// LanguageCodes. See basicLocaleListResolution in app.dart for a better
/// algorithm. If no match is found, fallback to English locale.
Locale getBestMatchingSupportedLocale(
Iterable<Locale>? preferredLocales,
Iterable<Locale> supportedLocales,
) {
if (preferredLocales == null) {
return defaultLocale;
}
final preferredLanguageCodes = preferredLocales.map((p) => p.languageCode);
final supportedLanguageCodes = supportedLocales.map((s) => s.languageCode);
final resolvedLanguageCode = preferredLanguageCodes.firstWhere(
supportedLanguageCodes.contains,
orElse: () => defaultLocale.languageCode,
);
return Locale(resolvedLanguageCode, '');
}