refactor: get language (#4922)

* refactor: get language
This commit is contained in:
Lloyd Zhou 2024-07-04 17:18:42 +08:00 committed by GitHub
parent 2803a91673
commit 8cb204e22e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 10 deletions

View File

@ -97,7 +97,17 @@ function setItem(key: string, value: string) {
function getLanguage() { function getLanguage() {
try { try {
return navigator.language.toLowerCase(); const locale = new Intl.Locale(navigator.language).maximize();
const region = locale?.region?.toLowerCase();
// 1. check region code in ALL_LANGS
if (AllLangs.includes(region as Lang)) {
return region as Lang;
}
// 2. check language code in ALL_LANGS
if (AllLangs.includes(locale.language as Lang)) {
return locale.language as Lang;
}
return DEFAULT_LANG;
} catch { } catch {
return DEFAULT_LANG; return DEFAULT_LANG;
} }
@ -110,15 +120,7 @@ export function getLang(): Lang {
return savedLang as Lang; return savedLang as Lang;
} }
const lang = getLanguage(); return getLanguage();
for (const option of AllLangs) {
if (lang.includes(option)) {
return option;
}
}
return DEFAULT_LANG;
} }
export function changeLang(lang: Lang) { export function changeLang(lang: Lang) {