2021-04-06 21:15:08 +02:00
|
|
|
import SUPPORTED_BROWSER_LANGUAGES from 'constants/supported_browser_languages';
|
2020-11-20 14:21:31 +01:00
|
|
|
const DEFAULT_LANG = 'en';
|
|
|
|
|
2021-01-06 19:13:56 +01:00
|
|
|
export const getDefaultLanguage = () => {
|
2021-04-06 21:15:08 +02:00
|
|
|
const browserLanguage = window.navigator.language;
|
2022-01-12 15:46:57 +01:00
|
|
|
|
|
|
|
if (SUPPORTED_BROWSER_LANGUAGES[browserLanguage]) {
|
|
|
|
return SUPPORTED_BROWSER_LANGUAGES[browserLanguage];
|
|
|
|
}
|
|
|
|
|
|
|
|
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_BROWSER_LANGUAGES[mainLang]) {
|
|
|
|
return SUPPORTED_BROWSER_LANGUAGES[mainLang];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2022-03-04 03:05:11 +01:00
|
|
|
const homepages = window.homepages || {};
|
2021-10-27 19:25:08 +02:00
|
|
|
return (homepages[language] && language) || (homepages[language.slice(0, 2)] && language.slice(0, 2)) || DEFAULT_LANG;
|
2020-11-20 14:21:31 +01:00
|
|
|
};
|
2021-06-07 03:54:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sorts the language map by their native representation (not by language code).
|
|
|
|
*
|
|
|
|
* @param languages The language map to sort, e.g. "{ 'ja': '日本語', ... }"
|
|
|
|
* @returns {[string, string][]}
|
|
|
|
*/
|
|
|
|
export function sortLanguageMap(languages) {
|
|
|
|
return Object.entries(languages).sort((a, b) => {
|
|
|
|
const lhs = String(a[1]);
|
|
|
|
const rhs = String(b[1]);
|
|
|
|
if (lhs < rhs) return -1;
|
|
|
|
if (lhs > rhs) return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
2022-02-24 11:06:38 +01:00
|
|
|
|
|
|
|
/**
|
2022-04-22 08:32:56 +02:00
|
|
|
* Resolves the claim_search 'any_language' parameter based on various language settings.
|
2022-02-24 11:06:38 +01:00
|
|
|
*
|
2022-04-22 08:32:56 +02:00
|
|
|
* @param languageSetting The user (redux) language setting.
|
|
|
|
* @param searchInLanguageSetting By default, the user (redux) setting, but some components may override to false (e.g. 'ignoreSearchInLanguage' prop).
|
|
|
|
* @param searchLanguages Per-instance default language list (e.g. different Categories having different filters).
|
|
|
|
* @param languageUrlParams Language override via URLSearchParams.
|
2022-02-24 11:06:38 +01:00
|
|
|
* @returns {string|null} Comma-separated string of language codes, or null.
|
|
|
|
*/
|
2022-04-22 08:32:56 +02:00
|
|
|
export function resolveLangForClaimSearch(
|
|
|
|
languageSetting, // : string,
|
|
|
|
searchInLanguageSetting, // : boolean,
|
|
|
|
searchLanguages, // : ?Array<string>,
|
|
|
|
languageUrlParams = null // : ?string
|
|
|
|
) {
|
|
|
|
const langParam = languageUrlParams;
|
|
|
|
|
|
|
|
let lang;
|
|
|
|
if (searchInLanguageSetting) {
|
|
|
|
lang = languageSetting;
|
|
|
|
} else if (searchLanguages) {
|
|
|
|
lang = searchLanguages.join(',');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lang) {
|
|
|
|
if (langParam === null) {
|
|
|
|
return lang === 'en' ? 'en,none' : lang;
|
|
|
|
} else if (langParam === 'any') {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return langParam === 'en' ? 'en,none' : langParam;
|
|
|
|
}
|
|
|
|
} else if (langParam === null) {
|
|
|
|
return null;
|
|
|
|
} else if (langParam === 'any') {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return langParam === 'en' ? 'en,none' : langParam;
|
|
|
|
}
|
2022-02-24 11:06:38 +01:00
|
|
|
}
|