Browse Source

fix: Fixed browser language detect (close #5987)

master
Edgard Messias 5 years ago
parent
commit
24052e9f9a

+ 49
- 0
react/features/base/i18n/customNavigatorDetector.js View File

@@ -0,0 +1,49 @@
1
+/* @flow */
2
+
3
+declare var navigator: Object;
4
+
5
+/**
6
+ * Custom language detection, just returns the config property if any.
7
+ */
8
+export default {
9
+    /**
10
+     * Does not support caching.
11
+     *
12
+     * @returns {void}
13
+     */
14
+    cacheUserLanguage: Function.prototype,
15
+
16
+    /**
17
+     * Looks the language up in the config.
18
+     *
19
+     * @returns {string} The default language if any.
20
+     */
21
+    lookup() {
22
+        let found = [];
23
+
24
+        if (typeof navigator !== 'undefined') {
25
+            if (navigator.languages) {
26
+                // chrome only; not an array, so can't use .push.apply instead of iterating
27
+                for (let i = 0; i < navigator.languages.length; i++) {
28
+                    found.push(navigator.languages[i]);
29
+                }
30
+            }
31
+            if (navigator.userLanguage) {
32
+                found.push(navigator.userLanguage);
33
+            }
34
+            if (navigator.language) {
35
+                found.push(navigator.language);
36
+            }
37
+        }
38
+
39
+        // Fix language format (en-US => enUS)
40
+        found = found.map<string>(f => f.replace(/[-_]+/g, ''));
41
+
42
+        return found.length > 0 ? found : undefined;
43
+    },
44
+
45
+    /**
46
+     * Name of the language detector.
47
+     */
48
+    name: 'customNavigatorDetector'
49
+};

+ 8
- 3
react/features/base/i18n/languageDetector.web.js View File

@@ -3,6 +3,8 @@
3 3
 import BrowserLanguageDetector from 'i18next-browser-languagedetector';
4 4
 
5 5
 import configLanguageDetector from './configLanguageDetector';
6
+import customNavigatorDetector from './customNavigatorDetector';
7
+
6 8
 
7 9
 declare var interfaceConfig: Object;
8 10
 
@@ -14,13 +16,15 @@ declare var interfaceConfig: Object;
14 16
  */
15 17
 const order = [
16 18
     'querystring',
17
-    'localStorage',
18
-    configLanguageDetector.name
19
+    'localStorage'
19 20
 ];
20 21
 
21 22
 // Allow i18next to detect the system language reported by the Web browser
22 23
 // itself.
23
-interfaceConfig.LANG_DETECTION && order.push('navigator');
24
+interfaceConfig.LANG_DETECTION && order.push(customNavigatorDetector.name);
25
+
26
+// Default use configured language
27
+order.push(configLanguageDetector.name);
24 28
 
25 29
 /**
26 30
  * The singleton language detector for Web.
@@ -37,6 +41,7 @@ const languageDetector
37 41
 
38 42
 // Add the language detector which looks the language up in the config. Its
39 43
 // order has already been established above.
44
+languageDetector.addDetector(customNavigatorDetector);
40 45
 languageDetector.addDetector(configLanguageDetector);
41 46
 
42 47
 export default languageDetector;

Loading…
Cancel
Save