Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

fix: Fixed browser language detect (close #5987)

Edgard Messias24052e9f9a36
diff --git a/react/features/base/i18n/customNavigatorDetector.js b/react/features/base/i18n/customNavigatorDetector.js
new file mode 100644
index 0000000000..768f2dfd00
--- /dev/null
+++ b/react/features/base/i18n/customNavigatorDetector.js
@@ -0,0 +1,49 @@
+/* @flow */
+
+declare var navigator: Object;
+
+/**
+ * Custom language detection, just returns the config property if any.
+ */
+export default {
+ /**
+ * Does not support caching.
+ *
+ * @returns {void}
+ */
+ cacheUserLanguage: Function.prototype,
+
+ /**
+ * Looks the language up in the config.
+ *
+ * @returns {string} The default language if any.
+ */
+ lookup() {
+ let 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);
+ }
+ }
+
+ // Fix language format (en-US => enUS)
+ found = found.map<string>(f => f.replace(/[-_]+/g, ''));
+
+ return found.length > 0 ? found : undefined;
+ },
+
+ /**
+ * Name of the language detector.
+ */
+ name: 'customNavigatorDetector'
+};
diff --git a/react/features/base/i18n/languageDetector.web.js b/react/features/base/i18n/languageDetector.web.js
index a45e701d41..d4c9c557a1 100644
--- a/react/features/base/i18n/languageDetector.web.js
+++ b/react/features/base/i18n/languageDetector.web.js
@@ -3,6 +3,8 @@
import BrowserLanguageDetector from 'i18next-browser-languagedetector';
import configLanguageDetector from './configLanguageDetector';
+import customNavigatorDetector from './customNavigatorDetector';
+
declare var interfaceConfig: Object;
@@ -14,13 +16,15 @@ declare var interfaceConfig: Object;
*/
const order = [
'querystring',
- 'localStorage',
- configLanguageDetector.name
+ 'localStorage'
];
// Allow i18next to detect the system language reported by the Web browser
// itself.
-interfaceConfig.LANG_DETECTION && order.push('navigator');
+interfaceConfig.LANG_DETECTION && order.push(customNavigatorDetector.name);
+
+// Default use configured language
+order.push(configLanguageDetector.name);
/**
* The singleton language detector for Web.
@@ -37,6 +41,7 @@ const languageDetector
// Add the language detector which looks the language up in the config. Its
// order has already been established above.
+languageDetector.addDetector(customNavigatorDetector);
languageDetector.addDetector(configLanguageDetector);
export default languageDetector;