瀏覽代碼

Improve localeResolution logic

Neeraj Gupta 2 年之前
父節點
當前提交
a22e136e71
共有 1 個文件被更改,包括 14 次插入3 次删除
  1. 14 3
      lib/locale.dart

+ 14 - 3
lib/locale.dart

@@ -16,14 +16,25 @@ const List<Locale> appSupportedLocales = <Locale>[
 ];
 ];
 
 
 Locale localResolutionCallBack(locales, supportedLocales) {
 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) {
   for (Locale locale in locales) {
     if (appSupportedLocales.contains(locale)) {
     if (appSupportedLocales.contains(locale)) {
       return 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 {
 Future<Locale> getLocale() async {