2020-11-20 14:21:31 +01:00
|
|
|
import homepages from 'homepages';
|
2021-03-16 11:42:39 +01:00
|
|
|
import SUPPORTED_LANGUAGES from 'constants/supported_languages';
|
2020-11-20 14:21:31 +01:00
|
|
|
const DEFAULT_LANG = 'en';
|
|
|
|
|
2021-03-16 11:42:39 +01:00
|
|
|
/**
|
|
|
|
* Checks if the given language is an alias of a language that we support, and
|
|
|
|
* returns the version that the rest of the application (lbry-sdk, Transifex)
|
|
|
|
* expects. If the given language is not an expected alias, the original value
|
|
|
|
* is returned.
|
|
|
|
*
|
|
|
|
* @param lang
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
export function resolveLanguageAlias(lang) {
|
|
|
|
const KNOWN_LANG_ALIASES = {
|
|
|
|
'zh-CN': 'zh-Hans',
|
|
|
|
'zh-TW': 'zh-Hant',
|
|
|
|
};
|
|
|
|
return KNOWN_LANG_ALIASES[lang] ? KNOWN_LANG_ALIASES[lang] : lang;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the native language of the system, filtered/resolved
|
|
|
|
* to the language code format that the application expects.
|
|
|
|
*
|
|
|
|
* @returns {string|*}
|
|
|
|
*/
|
2021-01-06 19:13:56 +01:00
|
|
|
export const getDefaultLanguage = () => {
|
2021-03-16 11:42:39 +01:00
|
|
|
const browserLanguage = resolveLanguageAlias(window.navigator.language);
|
|
|
|
|
|
|
|
if (SUPPORTED_LANGUAGES[browserLanguage]) {
|
|
|
|
return browserLanguage;
|
|
|
|
} else {
|
|
|
|
if (browserLanguage.includes('-')) {
|
|
|
|
// Perhaps it is a sub-lang that we are currently not supporting.
|
|
|
|
// See if we support the main one.
|
|
|
|
const mainLang = browserLanguage.substring(0, browserLanguage.indexOf('-'));
|
|
|
|
if (SUPPORTED_LANGUAGES[mainLang]) {
|
|
|
|
return mainLang;
|
|
|
|
} else {
|
|
|
|
return DEFAULT_LANG;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return DEFAULT_LANG;
|
|
|
|
}
|
|
|
|
}
|
2020-11-20 14:21:31 +01:00
|
|
|
};
|
|
|
|
|
2021-01-06 19:13:56 +01:00
|
|
|
// If homepages has a key "zh-Hant" return that, otherwise return "zh", otherwise "en"
|
|
|
|
export const getDefaultHomepageKey = () => {
|
|
|
|
const language = getDefaultLanguage();
|
|
|
|
return (homepages[language] && language) || (homepages[language.slice(0, 2)] && language.slice(0, 2)) || DEFAULT_LANG;
|
2020-11-20 14:21:31 +01:00
|
|
|
};
|