From 24052e9f9a3617ae068363775f4a2d9fcf2bc508 Mon Sep 17 00:00:00 2001 From: Edgard Messias Date: Mon, 11 May 2020 15:29:17 -0300 Subject: [PATCH] fix: Fixed browser language detect (close #5987) --- .../base/i18n/customNavigatorDetector.js | 49 +++++++++++++++++++ .../base/i18n/languageDetector.web.js | 11 +++-- 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 react/features/base/i18n/customNavigatorDetector.js diff --git a/react/features/base/i18n/customNavigatorDetector.js b/react/features/base/i18n/customNavigatorDetector.js new file mode 100644 index 000000000..768f2dfd0 --- /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(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 a45e701d4..d4c9c557a 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;