Ver Fonte

Improve localeResolution logic

Neeraj Gupta há 2 anos atrás
pai
commit
a22e136e71
1 ficheiros alterados com 14 adições e 3 exclusões
  1. 14 3
      lib/locale.dart

+ 14 - 3
lib/locale.dart

@@ -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 {