itizawa 4 лет назад
Родитель
Сommit
74f456d7dc

+ 2 - 1
resource/locales/en_US/meta.json

@@ -1,4 +1,5 @@
 {
   "id": "en_US",
-  "displayName": "English"
+  "displayName": "English",
+  "browserLanguageId": "en"
 }

+ 2 - 1
resource/locales/ja_JP/meta.json

@@ -1,4 +1,5 @@
 {
   "id": "ja_JP",
-  "displayName": "日本語"
+  "displayName": "日本語",
+  "browserLanguageId": "ja"
 }

+ 2 - 1
resource/locales/zh_CN/meta.json

@@ -1,4 +1,5 @@
 {
 	"id": "zh_CN",
-	"displayName": "简体中文"
+	"displayName": "简体中文",
+  "userLanguage":"cn"
 }

+ 33 - 2
src/client/js/util/i18n.js

@@ -15,7 +15,38 @@ export const i18nFactory = (userLocaleId) => {
     lookup(options) {
       return userLocaleId;
     },
-    cacheUserlanguage(lng, options) {
+  });
+  // Defined detection from the browser to convert id
+  // See Reference: https://github.com/i18next/i18next-browser-languageDetector/blob/master/src/browserLookups/navigator.js
+  langDetector.addDetector({
+    name: 'userBrowserDetector',
+    lookup(options) {
+      const found = [];
+
+      if (typeof navigator !== 'undefined') {
+        if (navigator.languages) { // chrome only; not an array, so can't use .push.apply instead of iterating
+          for (let i = 0; i < navigator.languages.length; i++) {
+            found.push(navigator.languages[i]);
+          }
+        }
+        if (navigator.userLanguage) {
+          found.push(navigator.userLanguage);
+        }
+        if (navigator.language) {
+          found.push(navigator.language);
+        }
+      }
+
+      if (found.length === 0) {
+        return undefined;
+      }
+
+      // detect id from browserLanguageId
+      const foundedLanguage = localeMetadatas.find(v => v.browserLanguageId === found[0]);
+      if (foundedLanguage == null) {
+        return undefined;
+      }
+      return foundedLanguage.id;
     },
   });
 
@@ -29,7 +60,7 @@ export const i18nFactory = (userLocaleId) => {
 
       fallbackLng: 'en_US',
       detection: {
-        order: ['userSettingDetector', 'querystring', 'localStorage'],
+        order: ['userSettingDetector', 'userBrowserDetector'],
       },
 
       interpolation: {