Improve localeResolution logic

This commit is contained in:
Neeraj Gupta 2023-05-07 10:28:21 +05:30
parent 4a64aa6a41
commit a22e136e71
No known key found for this signature in database
GPG key ID: 3C5A1684DC1729E1

View file

@ -16,14 +16,25 @@ const List<Locale> appSupportedLocales = <Locale>[
];
Locale localResolutionCallBack(locales, supportedLocales) {
// print call stacktrace to identify caller
Locale? languageCodeMatch;
final Map<String, Locale> languageCodeToLocale = {
for (Locale supportedLocale in appSupportedLocales)
supportedLocale.languageCode: supportedLocale
};
for (Locale locale in locales) {
if (appSupportedLocales.contains(locale)) {
return locale;
}
if (languageCodeMatch == null &&
languageCodeToLocale.containsKey(locale.languageCode)) {
languageCodeMatch = languageCodeToLocale[locale.languageCode];
}
}
// if device language is not supported by the app, use en as default
return const Locale('en');
// Return the first language code match or default to 'en'
return languageCodeMatch ?? const Locale('en');
}
Future<Locale> getLocale() async {