feat: 默认跟随系统主题和浏览器语言

This commit is contained in:
Nixevol
2026-07-05 02:05:53 +08:00
parent 4dc9528397
commit 76bdf4fa3f
7 changed files with 53 additions and 9 deletions
+13 -1
View File
@@ -11,10 +11,20 @@ export const SUPPORTED_LOCALES = [
const STORAGE_KEY = 'artveris_locale';
const DEFAULT_LOCALE = 'zh-CN';
function matchBrowserLocale(): string {
const languages = navigator.languages?.length ? navigator.languages : [navigator.language];
for (const language of languages) {
const normalized = language.toLowerCase();
const matched = SUPPORTED_LOCALES.find((locale) => normalized.startsWith(locale.value.slice(0, 2).toLowerCase()));
if (matched) return matched.value;
}
return DEFAULT_LOCALE;
}
export function getInitialLocale(): string {
const saved = localStorage.getItem(STORAGE_KEY);
if (saved && SUPPORTED_LOCALES.some((l) => l.value === saved)) return saved;
return DEFAULT_LOCALE;
return matchBrowserLocale();
}
export function setLocale(locale: string): void {
@@ -32,3 +42,5 @@ export const i18n = createI18n({
'en-US': enUS,
},
});
document.querySelector('html')?.setAttribute('lang', i18n.global.locale.value);