jiti-meet/react/features/base/i18n/customNavigatorDetector.ts

68 lines
1.6 KiB
TypeScript
Raw Normal View History

declare let navigator: any;
/**
* 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);
}
}
2021-02-26 12:25:34 +00:00
found = found.map<string>(normalizeLanguage);
return found.length > 0 ? found : undefined;
},
/**
* Name of the language detector.
*/
name: 'customNavigatorDetector'
};
2021-02-26 12:25:34 +00:00
/**
* Normalize language format.
*
* (en-US => enUS)
* (en-gb => enGB)
* (es-es => es).
*
* @param {string} language - Language.
* @returns {string} The normalized language.
*/
2022-08-01 07:14:54 +00:00
function normalizeLanguage(language: string) {
2021-02-26 12:25:34 +00:00
const [ lang, variant ] = language.replace('_', '-').split('-');
if (!variant || lang === variant) {
return lang;
}
return lang + variant.toUpperCase();
}