Match UI i18n configuration to browser locales

This commit is contained in:
smitten 2023-11-01 23:10:57 -04:00
parent c9dc8f00f9
commit 1b28ec3b72
Signed by: smitten
GPG key ID: 1DDD22F13552A07A

View file

@ -8,7 +8,30 @@ const specialLanguageCodes = {
'zh': 'zh-Hans' 'zh': 'zh-Hans'
} }
const internalToBrowserLocale = fallbackCode => specialLanguageCodes[fallbackCode] || window.navigator.language || fallbackCode // Find a browser language that matches the configured UI language.
// Browser language should match the configured generic short code prefix:
// eg 'en-GB' browser language matches 'en' UI language.
const findBrowserRegionMatch = genericLang => {
for (const blang of window.navigator.languages) {
if (genericLang === blang.split('-')[0])
return blang;
}
return null;
}
const internalToBrowserLocale = (() => {
const resolvedBrowserLocales = {}
return i18nLocale => {
if (resolvedBrowserLocales[i18nLocale]) {
return resolvedBrowserLocales[i18nLocale]
}
const lang = specialLanguageCodes[i18nLocale] || i18nLocale;
const resolved = findBrowserRegionMatch(lang) || lang;
resolvedBrowserLocales[i18nLocale] = resolved
return resolved
}
})()
const internalToBackendLocale = code => internalToBrowserLocale(code).replace('_', '-') const internalToBackendLocale = code => internalToBrowserLocale(code).replace('_', '-')