fix(i18n): normalize locale and prevent undefined translations (#914)

* fix(i18n): guard locale input and add safe translation fallback

* refactor(i18n): isolate locale utils and normalize server cookie decode

---------

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
This commit is contained in:
Xinmin Zeng
2026-02-27 08:10:38 +08:00
committed by GitHub
parent 902ff3b9f3
commit e9adaab7a6
5 changed files with 81 additions and 32 deletions
+20 -7
View File
@@ -7,7 +7,13 @@ import { getLocaleFromCookie, setLocaleInCookie } from "./cookies";
import { enUS } from "./locales/en-US";
import { zhCN } from "./locales/zh-CN";
import { detectLocale, type Locale, type Translations } from "./index";
import {
DEFAULT_LOCALE,
detectLocale,
normalizeLocale,
type Locale,
type Translations,
} from "./index";
const translations: Record<Locale, Translations> = {
"en-US": enUS,
@@ -17,7 +23,7 @@ const translations: Record<Locale, Translations> = {
export function useI18n() {
const { locale, setLocale } = useI18nContext();
const t = translations[locale];
const t = translations[locale] ?? translations[DEFAULT_LOCALE];
const changeLocale = (newLocale: Locale) => {
setLocale(newLocale);
@@ -26,12 +32,19 @@ export function useI18n() {
// Initialize locale on mount
useEffect(() => {
const saved = getLocaleFromCookie() as Locale | null;
if (!saved) {
const detected = detectLocale();
setLocale(detected);
setLocaleInCookie(detected);
const saved = getLocaleFromCookie();
if (saved) {
const normalizedSaved = normalizeLocale(saved);
setLocale(normalizedSaved);
if (saved !== normalizedSaved) {
setLocaleInCookie(normalizedSaved);
}
return;
}
const detected = detectLocale();
setLocale(detected);
setLocaleInCookie(detected);
}, [setLocale]);
return {