Skip to content

Commit

Permalink
refactor: get language (ChatGPTNextWeb#4922)
Browse files Browse the repository at this point in the history
* refactor: get language
  • Loading branch information
lloydzhou authored Jul 4, 2024
1 parent 2803a91 commit 8cb204e
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions app/locales/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,17 @@ function setItem(key: string, value: string) {

function getLanguage() {
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 {
return DEFAULT_LANG;
}
Expand All @@ -110,15 +120,7 @@ export function getLang(): Lang {
return savedLang as Lang;
}

const lang = getLanguage();

for (const option of AllLangs) {
if (lang.includes(option)) {
return option;
}
}

return DEFAULT_LANG;
return getLanguage();
}

export function changeLang(lang: Lang) {
Expand Down

0 comments on commit 8cb204e

Please sign in to comment.