mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-05-03 02:30:44 +08:00
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:
36
frontend/src/core/i18n/locale.ts
Normal file
36
frontend/src/core/i18n/locale.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
export const SUPPORTED_LOCALES = ["en-US", "zh-CN"] as const;
|
||||
export type Locale = (typeof SUPPORTED_LOCALES)[number];
|
||||
export const DEFAULT_LOCALE: Locale = "en-US";
|
||||
|
||||
export function isLocale(value: string): value is Locale {
|
||||
return (SUPPORTED_LOCALES as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
export function normalizeLocale(locale: string | null | undefined): Locale {
|
||||
if (!locale) {
|
||||
return DEFAULT_LOCALE;
|
||||
}
|
||||
|
||||
if (isLocale(locale)) {
|
||||
return locale;
|
||||
}
|
||||
|
||||
if (locale.toLowerCase().startsWith("zh")) {
|
||||
return "zh-CN";
|
||||
}
|
||||
|
||||
return DEFAULT_LOCALE;
|
||||
}
|
||||
|
||||
// Helper function to detect browser locale
|
||||
export function detectLocale(): Locale {
|
||||
if (typeof window === "undefined") {
|
||||
return DEFAULT_LOCALE;
|
||||
}
|
||||
|
||||
const browserLang =
|
||||
navigator.language ||
|
||||
(navigator as unknown as { userLanguage: string }).userLanguage;
|
||||
|
||||
return normalizeLocale(browserLang);
|
||||
}
|
||||
Reference in New Issue
Block a user