feat(i18n): dev-mode warn on missing translation keys, default-locale fallback (P3-54)
Missing keys now log a console.warn in dev; at runtime the default-locale dict is consulted before falling back to the raw key so zh users are not shown literal keys when a zh translation is absent.
This commit is contained in:
1 parent
33a371eb50
commit
9e6aa61e09
1 file changed
+10
-1
@@ -10,7 +10,16 @@ export type TranslationFn = (key: string, fallback?: string) => string;
|
||||
|
||||
export function createTranslator(locale: AppLocale): TranslationFn {
|
||||
const dict = translations[locale] ?? translations[DEFAULT_LOCALE];
|
||||
return (key: string, fallback?: string) => dict[key] ?? fallback ?? key;
|
||||
const defaultDict = translations[DEFAULT_LOCALE];
|
||||
return (key: string, fallback?: string) => {
|
||||
const value = dict[key];
|
||||
if (value !== undefined) return value;
|
||||
if (import.meta.env.DEV && !defaultDict[key] && fallback === undefined) {
|
||||
|
||||
console.warn(`[i18n] missing translation key: ${key}`);
|
||||
}
|
||||
return defaultDict[key] ?? fallback ?? key;
|
||||
};
|
||||
}
|
||||
|
||||
export const LocaleContext = createContext<AppLocale>(DEFAULT_LOCALE);
|
||||
|
||||
Reference in new issue
Block a user