feat: 前端 UI 国际化,支持中/英/日三语切换

之前语言设置只影响后端 AI 行为,前端 UI 文字始终为中文。
新增轻量 i18n 模块(React Context + 翻译字典),切换语言后所有 UI 文字即时刷新。

- 新增 i18n 核心模块和 zh-CN/en-US/ja-JP 翻译文件(约 72 个 key)
- App.tsx 拆分为 App(Provider)+ AppContent(业务),确保 Hook 可访问 Context
- 所有组件通过 useI18n().t() 获取翻译文本
- errors.ts 改为接受翻译函数参数
- storage.ts saveConfig 时派发事件通知 locale 变化
This commit is contained in:
2026-06-14 14:34:30 +08:00
parent d8bbdb1744
commit e95b1603c1
14 changed files with 418 additions and 71 deletions

View File

@@ -5,20 +5,13 @@
import type { ErrorCode } from "../types";
const ERROR_MESSAGES: Record<ErrorCode, string> = {
INVALID_MESSAGE: "消息格式异常,请重试",
SESSION_NOT_FOUND: "会话已过期,请重新连接",
RATE_LIMITED: "请求太频繁,请稍后再试",
IMAGE_TOO_LARGE: "图像过大,请降低分辨率",
AUDIO_TOO_SHORT: "语音太短,请再说一句",
LLM_TIMEOUT: "AI 响应超时,请重试",
LLM_ERROR: "AI 服务异常,请稍后重试",
STT_ERROR: "语音识别失败,请重试",
TTS_ERROR: "语音合成失败",
INTERNAL_ERROR: "服务内部错误,请重试",
};
/** 将错误码转为用户友好文案 */
export function getErrorMessage(code: string): string {
return ERROR_MESSAGES[code as ErrorCode] ?? `未知错误: ${code}`;
/** 将错误码转为用户友好文案(需要传入翻译函数) */
export function getErrorMessage(code: string, translate: (key: string) => string): string {
const key = `error.${code}`;
const translated = translate(key);
// 如果翻译函数返回 key 本身(未找到翻译),用 fallback
if (translated === key) {
return translate("error.unknown") + `: ${code}`;
}
return translated;
}