Files
beaver_project/app-instance/frontend/lib/i18n/core.ts
steven_li 3b0af173cc refactor(beaver): 移除Hermes相关引用和迁移代码,完善Beaver后端主线实现
移除了所有Hermes相关的命名引用,包括:
- 从.gitignore中清理相关构建缓存文件
- 将README中的beaver-home路径配置更新
- 完善backend/README.md文档说明Beaver后端主线实现
- 移除Hermes风格的相关注释和兼容性代码
- 清理nanobot环境变量兼容性处理
- 删除技能迁移和服务迁移相关功能代码
- 更新测试用例中相关命名和函数名

BREAKING CHANGE: 移除了Hermes迁移相关API和CLI命令,不再支持nanobot环境变量兼容性
2026-05-14 17:20:32 +08:00

77 lines
2.0 KiB
TypeScript

export const APP_LOCALE_COOKIE = 'beaver_locale';
export const APP_LOCALE_STORAGE_KEY = 'beaver_locale';
export const APP_LOCALES = ['zh-CN', 'en-US'] as const;
export type AppLocale = (typeof APP_LOCALES)[number];
export function isAppLocale(value: string | null | undefined): value is AppLocale {
return value === 'zh-CN' || value === 'en-US';
}
export function normalizeAppLocale(value?: string | null): AppLocale {
const probe = value?.trim().toLowerCase() || '';
if (probe.startsWith('en')) {
return 'en-US';
}
return 'zh-CN';
}
function readCookieLocale(): string | null {
if (typeof document === 'undefined') {
return null;
}
const match = document.cookie
.split('; ')
.find((item) => item.startsWith(`${APP_LOCALE_COOKIE}=`));
if (!match) {
return null;
}
return decodeURIComponent(match.slice(APP_LOCALE_COOKIE.length + 1));
}
export function readBrowserAppLocale(): AppLocale {
if (typeof window === 'undefined') {
return 'zh-CN';
}
const fromDocument = document.documentElement.lang;
if (fromDocument) {
return normalizeAppLocale(fromDocument);
}
const fromStorage = window.localStorage.getItem(APP_LOCALE_STORAGE_KEY);
if (fromStorage) {
return normalizeAppLocale(fromStorage);
}
const fromCookie = readCookieLocale();
if (fromCookie) {
return normalizeAppLocale(fromCookie);
}
return normalizeAppLocale(window.navigator.language);
}
export function persistAppLocale(locale: AppLocale): void {
if (typeof window === 'undefined') {
return;
}
document.documentElement.lang = locale;
window.localStorage.setItem(APP_LOCALE_STORAGE_KEY, locale);
document.cookie = `${APP_LOCALE_COOKIE}=${encodeURIComponent(locale)}; path=/; max-age=31536000; samesite=lax`;
}
export function getCurrentAppLocale(): AppLocale {
if (typeof window === 'undefined') {
return 'zh-CN';
}
return readBrowserAppLocale();
}
export function pickAppText<T>(locale: AppLocale, zhValue: T, enValue: T): T {
return locale === 'en-US' ? enValue : zhValue;
}