164 lines
9.8 KiB
TypeScript
164 lines
9.8 KiB
TypeScript
/**
|
||
* Main-process Locale — 主进程侧文案双语(v0.8.1 P0-4 i18n 收口第二期)
|
||
*
|
||
* 背景:v0.7.2 建立了渲染层 i18next 集中文案字典,但主进程直接 broadcast 的
|
||
* toast / 系统通知(压缩、死循环、故障转移、记忆固化、sendMessage 错误路径等)
|
||
* 全部硬编码中文 —— en-US 用户看到的系统提示仍是中文,i18n 收口未完成。
|
||
*
|
||
* 契约:
|
||
* - 语言偏好唯一来源是设置面板 `ui.locale`(zh-CN / en-US);
|
||
* - main.ts 启动时注入一次,config:set/setBatch 变更 ui.locale 时经
|
||
* applyConfigSideEffects 热切换(无需重启);
|
||
* - mt(key, params) 为唯一取词入口,缺 key 时回退 zh 表,再缺失时回退 key 本身
|
||
* (与渲染层 t() 的回退语义一致);
|
||
* - 文案表只覆盖"主进程主动产生"的文案;渲染层文案仍归 i18n-strings.ts。
|
||
*/
|
||
|
||
export type MainLocale = 'zh-CN' | 'en-US';
|
||
|
||
let currentLocale: MainLocale = 'zh-CN';
|
||
|
||
/** 设置主进程语言(非法值保持不变;null/undefined 视为未配置 → zh-CN 默认) */
|
||
export function setMainLocale(locale: string | null | undefined): void {
|
||
if (locale === 'en-US' || locale === 'zh-CN') {
|
||
currentLocale = locale;
|
||
}
|
||
}
|
||
|
||
export function getMainLocale(): MainLocale {
|
||
return currentLocale;
|
||
}
|
||
|
||
const zh: Record<string, string> = {
|
||
// ===== sendMessage 错误路径 =====
|
||
'agent.error.invalidSessionId': '无效的会话 ID',
|
||
'agent.error.invalidMessage': '无效的消息格式',
|
||
'agent.error.sessionBusy': '该会话正在执行任务,请等待完成或先中断后再发送',
|
||
'agent.error.sessionMissing': '会话不存在或已被删除,请刷新后重试',
|
||
'agent.error.adapterLoadFailed':
|
||
'Adapter 加载失败,请检查 LLM 配置(Provider、API Key、Base URL、Model 是否完整)',
|
||
// ===== SOUL.md 降级提示 =====
|
||
'agent.soul.fallbackToast':
|
||
'未找到 SOUL.md 或内容为空,已使用默认 Metona 身份。可在工作空间根目录创建 SOUL.md 自定义 Agent 人格',
|
||
// ===== 引擎事件 toast =====
|
||
'agent.toast.compressed': '上下文压缩: {{original}} → {{compressed}} tokens(节省 {{saved}})',
|
||
'agent.toast.deadLoop':
|
||
'检测到死循环(第 {{iteration}} 轮):连续3轮重复相同工具调用,已自动终止',
|
||
'agent.toast.providerSwitched': 'Provider 故障转移: {{from}} → {{to}}(主 Provider 请求失败)',
|
||
'agent.toast.consolidated': 'AI 已将 {{count}} 条重要记忆写入 MEMORY.md',
|
||
'agent.toast.memoryOversize': 'MEMORY.md 已超过 50KB,建议在「记忆」面板整理记忆',
|
||
// ===== LLM 运行时查询 =====
|
||
'llm.balance.queryFailed': '余额查询失败(API Key 无效或网络错误)',
|
||
'llm.listModels.notConfigured': 'LLM 未配置(Provider/Model 为空),无法获取模型列表',
|
||
'llm.listModels.noApiKey': 'API Key 未配置,无法获取模型列表',
|
||
'llm.listModels.configInvalid': 'LLM 配置校验失败,请先在设置中修正配置',
|
||
'llm.listModels.unsupported': '当前 Provider 不支持模型列表查询',
|
||
'llm.pull.ollamaOnly': '仅 Ollama Provider 支持模型下载',
|
||
'llm.pull.inProgress': '已有模型下载任务进行中,请先取消',
|
||
'llm.pull.none': '没有进行中的下载任务',
|
||
// ===== main.ts 适配器/Provider =====
|
||
'config.toast.configIncomplete':
|
||
'LLM 配置不完整,请在设置中补全 Provider、API Key、Base URL 和 Model',
|
||
'config.toast.providerSwitched': 'Provider 已切换: {{from}} → {{to}}',
|
||
'config.toast.providerSwitchFailed': 'Provider 切换失败: {{message}}',
|
||
// ===== 系统通知 =====
|
||
'notify.updateAvailable.title': 'MetonaAI 更新可用',
|
||
'notify.updateAvailable.body': '新版本 {{version}} 已发布,可在 设置 → 日志与数据 中下载安装',
|
||
'notify.taskCompleted.title': 'MetonaAI — 任务完成',
|
||
'notify.taskCompleted.body': 'Agent 已完成任务 ({{seconds}}s)',
|
||
// ===== shared.ts 配置副作用 =====
|
||
'config.error.configIncomplete':
|
||
'LLM 配置不完整,请检查 Provider、API Key、Base URL 和 Model 是否都已填写',
|
||
'config.error.workspaceSaveFailed': '工作空间路径保存失败:{{message}}',
|
||
// ===== 记忆维护(v0.8.1 P1-2) =====
|
||
// ===== v0.8.2 P2-5 i18n 收口第三期:托盘 / 系统对话框 / 更新 / 工作空间校验 =====
|
||
'tray.menu.showWindow': '显示窗口',
|
||
'tray.menu.newSession': '新建会话',
|
||
'tray.menu.quit': '退出',
|
||
'tray.menu.status': '状态: {{status}}',
|
||
'tray.status.idle': '空闲',
|
||
'tray.status.thinking': '思考中',
|
||
'tray.status.executing': '执行中',
|
||
'tray.status.error': '异常',
|
||
'app.update.disabled': '自动更新未启用(未配置更新源或开发模式)',
|
||
'app.dialog.selectWorkspace.title': '选择工作空间目录',
|
||
'app.dialog.startupFailed.title': 'MetonaAI Desktop 启动失败',
|
||
'app.dialog.startupFailed.body':
|
||
'初始化过程中发生错误,应用即将退出。\n\n{{message}}\n\n可能原因:工作空间目录不可写、数据库文件损坏。\n可尝试在设置中切换工作空间路径后重新启动。',
|
||
'workspace.reason.empty': '路径不能为空',
|
||
'workspace.reason.notExists': '目录不存在,将在切换后自动创建',
|
||
'workspace.reason.notDirectory': '路径不是目录',
|
||
'workspace.reason.inaccessible': '无法访问路径',
|
||
'workspace.reason.inheritFailed': '继承文件失败:{{message}}',
|
||
'workspace.reason.restartRequired': '工作空间已切换,重启应用后生效',
|
||
};
|
||
|
||
const en: Record<string, string> = {
|
||
'agent.error.invalidSessionId': 'Invalid session ID',
|
||
'agent.error.invalidMessage': 'Invalid message format',
|
||
'agent.error.sessionBusy':
|
||
'This session is already running. Please wait for it to finish or abort it first.',
|
||
'agent.error.sessionMissing': 'Session does not exist or has been deleted. Please refresh.',
|
||
'agent.error.adapterLoadFailed':
|
||
'Adapter load failed. Check your LLM config (Provider, API Key, Base URL, Model).',
|
||
'agent.soul.fallbackToast':
|
||
'SOUL.md not found or empty — using the default Metona identity. Create SOUL.md in the workspace root to customize the agent persona.',
|
||
'agent.toast.compressed':
|
||
'Context compressed: {{original}} → {{compressed}} tokens (saved {{saved}})',
|
||
'agent.toast.deadLoop':
|
||
'Dead loop detected (iteration {{iteration}}): the same tool call repeated 3 times — aborted automatically',
|
||
'agent.toast.providerSwitched': 'Provider failover: {{from}} → {{to}} (primary provider failed)',
|
||
'agent.toast.consolidated': '{{count}} important memories were written to MEMORY.md',
|
||
'agent.toast.memoryOversize':
|
||
'MEMORY.md exceeds 50KB — consider tidying it up in the Memory panel',
|
||
'llm.balance.queryFailed': 'Balance query failed (invalid API key or network error)',
|
||
'llm.listModels.notConfigured': 'LLM not configured (Provider/Model empty) — cannot list models',
|
||
'llm.listModels.noApiKey': 'API Key not configured — cannot list models',
|
||
'llm.listModels.configInvalid': 'LLM config validation failed — fix it in Settings first',
|
||
'llm.listModels.unsupported': 'The current provider does not support model listing',
|
||
'llm.pull.ollamaOnly': 'Only the Ollama provider supports model download',
|
||
'llm.pull.inProgress': 'A model download is already in progress — cancel it first',
|
||
'llm.pull.none': 'No download in progress',
|
||
'config.toast.configIncomplete':
|
||
'LLM config incomplete. Please set Provider, API Key, Base URL and Model in Settings.',
|
||
'config.toast.providerSwitched': 'Provider switched: {{from}} → {{to}}',
|
||
'config.toast.providerSwitchFailed': 'Provider switch failed: {{message}}',
|
||
'notify.updateAvailable.title': 'MetonaAI update available',
|
||
'notify.updateAvailable.body':
|
||
'Version {{version}} has been released. Install it in Settings → Logs & Data.',
|
||
'notify.taskCompleted.title': 'MetonaAI — task completed',
|
||
'notify.taskCompleted.body': 'The agent finished the task ({{seconds}}s)',
|
||
'config.error.configIncomplete':
|
||
'LLM config incomplete. Check that Provider, API Key, Base URL and Model are all filled in.',
|
||
'config.error.workspaceSaveFailed': 'Failed to save workspace path: {{message}}',
|
||
// ===== v0.8.2 P2-5: tray / system dialogs / update / workspace validation =====
|
||
'tray.menu.showWindow': 'Show Window',
|
||
'tray.menu.newSession': 'New Session',
|
||
'tray.menu.quit': 'Quit',
|
||
'tray.menu.status': 'Status: {{status}}',
|
||
'tray.status.idle': 'Idle',
|
||
'tray.status.thinking': 'Thinking',
|
||
'tray.status.executing': 'Executing',
|
||
'tray.status.error': 'Error',
|
||
'app.update.disabled': 'Auto-update is not enabled (no feed URL configured, or dev mode)',
|
||
'app.dialog.selectWorkspace.title': 'Select workspace directory',
|
||
'app.dialog.startupFailed.title': 'MetonaAI Desktop failed to start',
|
||
'app.dialog.startupFailed.body':
|
||
'An error occurred during initialization and the app will exit.\n\n{{message}}\n\nPossible causes: the workspace directory is not writable, or the database file is corrupted.\nTry switching the workspace path in Settings and restart.',
|
||
'workspace.reason.empty': 'Path must not be empty',
|
||
'workspace.reason.notExists': 'Directory does not exist — it will be created after switching',
|
||
'workspace.reason.notDirectory': 'Path is not a directory',
|
||
'workspace.reason.inaccessible': 'Path is not accessible',
|
||
'workspace.reason.inheritFailed': 'Failed to inherit files: {{message}}',
|
||
'workspace.reason.restartRequired': 'Workspace switched — restart the app to apply',
|
||
};
|
||
|
||
const DICTS: Record<MainLocale, Record<string, string>> = { 'zh-CN': zh, 'en-US': en };
|
||
|
||
/** 主进程文案取词:缺 key 回退 zh 表;参数以 {{name}} 占位替换 */
|
||
export function mt(key: string, params?: Record<string, string | number>): string {
|
||
const raw = DICTS[currentLocale][key] ?? zh[key] ?? key;
|
||
if (!params) return raw;
|
||
return raw.replace(/\{\{(\w+)\}\}/g, (_, name: string) => String(params[name] ?? ''));
|
||
}
|