feat: auto-detect browser locale (zh-TW / zh / en)

Falls back to navigator.languages when no saved preference exists in
localStorage. Manual language selection still takes priority.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Dan Lee
2026-03-20 11:04:19 +08:00
co-authored by Claude Sonnet 4.6
parent 8dcbe98c6e
commit 239b357e57
+13
View File
@@ -883,6 +883,17 @@ const I18nContext = createContext<I18nContextType>({
t: (key) => key,
});
function detectBrowserLocale(): Locale {
const langs = navigator.languages?.length ? navigator.languages : [navigator.language];
for (const lang of langs) {
const l = lang.toLowerCase();
if (l.startsWith('zh-tw') || l.startsWith('zh-hant') || l.startsWith('zh-hk') || l.startsWith('zh-mo')) return 'zh-TW';
if (l.startsWith('zh')) return 'zh';
if (l.startsWith('en')) return 'en';
}
return 'zh';
}
export function I18nProvider({ children }: { children: ReactNode }) {
const [locale, setLocaleState] = useState<Locale>("zh");
@@ -890,6 +901,8 @@ export function I18nProvider({ children }: { children: ReactNode }) {
const saved = localStorage.getItem("locale") as Locale;
if (saved && (saved === "zh-TW" || saved === "zh" || saved === "en")) {
setLocaleState(saved);
} else {
setLocaleState(detectBrowserLocale());
}
}, []);