Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

refactor(turnrewind): move the undo card's hardcoded strings into the locale dictionaries

yingtianlan236197f40cfa
diff --git a/packages/dsh-tauri-turnrewind-ts/src/client/components/command-view.ts b/packages/dsh-tauri-turnrewind-ts/src/client/components/command-view.ts
index 0bf38d40..43b2b35e 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/client/components/command-view.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/client/components/command-view.ts
@@ -6,9 +6,10 @@
* 从 persisted plan status 重建,而不是内存态。
*/
-import type { CommandViewProps, ParsedUndoFile, ParsedUndoOutput } from '../types'
+import type { CommandViewProps, LocaleKey, ParsedUndoFile, ParsedUndoOutput, Translate } from '../types'
import React, { useEffect, useRef, useState } from 'react'
import { TURNREWIND_CLASS_PREFIX, TURNREWIND_HTTP_BASE, TURNREWIND_POLL_INTERVAL_MS } from '../constants'
+import { LOCALES } from '../locales'
import { parseUndoOutput, resolvePlanStatus } from '../utils/parse'
import { resolveOwnerSessionId } from '../utils/session'
@@ -97,6 +98,22 @@ export function setSubmitLine(next: SubmitLine | null): void {
submitLine = next
}
+// ------------------------------------------------------------------
+// locale 通道:与提交通道同一注入模式。组件不直接依赖 locale 服务
+// (slot props 不携带它),由 apply() 按当前活跃语言注入取词函数;
+// 未注入时回退到 zh 字典——字典即文案的唯一来源,不另存一份硬编码。
+// ------------------------------------------------------------------
+let translator: Translate | null = null
+
+/** 供 apply() 安装/卸载 locale 取词通道。 */
+export function setCardTranslator(next: Translate | null): void {
+ translator = next
+}
+
+function tr(key: LocaleKey): string {
+ return translator ? translator(key) : LOCALES.zh[key]
+}
+
// ------------------------------------------------------------------
// 命令卡片组件。props 类型集中在 client/types(P2-10)。
// ------------------------------------------------------------------
@@ -112,7 +129,7 @@ export function UndoCommandView(props: CommandViewProps): React.ReactElement {
const withDiff = parsed.files.filter(file => file.diff.length > 0)
const totals = parsed.files.reduce((sum, file) => ({ additions: sum.additions + file.additions, deletions: sum.deletions + file.deletions }), { additions: 0, deletions: 0 })
const hasDiff = withDiff.length > 0
- const summary = parsed.summary || (state === 'error' ? '失败' : state === 'running' ? '运行中' : '完成')
+ const summary = parsed.summary || (state === 'error' ? tr('cardFailed') : state === 'running' ? tr('cardRunning') : tr('cardDone'))
// 展开状态按命令持久化:用户折叠后刷新不重新展开。
// 持久化 key 只用稳定且长度受限的节点标识(node.id / planId)——不回退到
@@ -182,7 +199,7 @@ export function UndoCommandView(props: CommandViewProps): React.ReactElement {
else if (next.status === 'pending')
setPlanStatus('pending')
if (next.status === 'applied')
- setResultText(next.resultText ?? '已执行')
+ setResultText(next.resultText ?? tr('cardAppliedFallback'))
if (next.stop) {
haltPolling()
return
@@ -247,18 +264,18 @@ export function UndoCommandView(props: CommandViewProps): React.ReactElement {
}
const confirmLabel = submitting
- ? '执行中…'
- : submitted === 'confirm' ? '已提交执行确认' : '✓ 执行'
+ ? tr('confirmExecuting')
+ : submitted === 'confirm' ? tr('confirmSubmitted') : tr('confirmExecute')
const cancelLabel = submitting
- ? '取消中…'
- : submitted === 'cancel' ? '已取消' : '✕ 取消'
+ ? tr('cancelCancelling')
+ : submitted === 'cancel' ? tr('cancelled') : tr('cancelAction')
// plan 提交即置 applying:提示行不等轮询返回就切到等待态。
const pendingWait = submitting || submitted === 'confirm' || planStatus === 'applying'
const hint = submitError
- ? `执行确认失败:${submitError}`
+ ? `${tr('confirmFailed')}${submitError}`
: resultText || (planStatus === 'applied' || pendingWait
- ? '已提交,等待执行结果…'
- : planStatus === 'expired' || planStatus === 'gone' ? '该计划已过期,重新执行 /undo 可生成新预览' : planStatus === 'cancelled' || submitted === 'cancel' ? '已取消' : '执行将恢复下方文件到本轮改动前')
+ ? tr('cardWaitingResult')
+ : planStatus === 'expired' || planStatus === 'gone' ? tr('planExpiredHint') : planStatus === 'cancelled' || submitted === 'cancel' ? tr('cancelled') : tr('previewHint'))
// 提交后的结果(成功/失败/等待中)靠左展示;预览/过期/取消提示贴 footer 右缘。
const hintLeft = Boolean(resultText || submitError || submitted !== null || planStatus === 'applied')
const hintCls = `${TURNREWIND_CLASS_PREFIX}-card-hint${submitError
diff --git a/packages/dsh-tauri-turnrewind-ts/src/client/index.ts b/packages/dsh-tauri-turnrewind-ts/src/client/index.ts
index 2fabb67d..f45a168a 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/client/index.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/client/index.ts
@@ -9,7 +9,7 @@
import type { ClientContext } from 'dsh-tauri/client'
import type { LocaleKey } from './locales'
import { compat } from 'dsh-tauri/client'
-import { setSubmitLine } from './components/command-view'
+import { setCardTranslator, setSubmitLine } from './components/command-view'
import { TURNREWIND_HTTP_BASE, TURNREWIND_LOCALE_NS, TURNREWIND_POLL_INTERVAL_MS, TURNREWIND_POLL_STOP_MS } from './constants'
import { LOCALES } from './locales'
import { registerCommandView } from './register/command-view'
@@ -60,6 +60,16 @@ export function apply(ctx: ClientContext): void {
return disposer
}, 'turnrewind locale en')
+ // ————————————————— 卡片 locale 通道 —————————————————
+ // 组件经 slot props 拿不到 locale 服务:由 apply 层把取词函数注入组件模块
+ // (与 setSubmitLine 同一生命周期模式),stop/HMR 时置空。
+ ctx.effect(() => {
+ setCardTranslator(t)
+ return () => {
+ setCardTranslator(null)
+ }
+ }, 'turnrewind card translator')
+
// ————————————————— ✓/✗ 提交通道 —————————————————
// 直接 POST 到插件的同源 HTTP 路由(宿主页面本身由同一 Host 服务,无需额外
// auth wiring)。返回错误字符串让卡片可以显示真实失败原因。
@@ -67,7 +77,7 @@ export function apply(ctx: ClientContext): void {
setSubmitLine(async (line, ownerSessionId) => {
try {
if (typeof ownerSessionId !== 'string' || ownerSessionId.length === 0)
- return '无法确定该卡片所属的会话,请刷新页面后重试'
+ return t('sessionMissing')
const kind = line.includes('--confirm') ? 'confirm' : 'cancel'
const planId = line.split(' ').at(-1)!
const res = await fetch(`${TURNREWIND_HTTP_BASE}/${kind}`, {
diff --git a/packages/dsh-tauri-turnrewind-ts/src/client/locales/index.ts b/packages/dsh-tauri-turnrewind-ts/src/client/locales/index.ts
index 3c979eba..5184732c 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/client/locales/index.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/client/locales/index.ts
@@ -2,7 +2,27 @@
* client/locales/index.ts — 双语字典(undo 卡片 / 不可用弹窗)。
*/
-export type LocaleKey = 'dialogTitle' | 'dialogIntro' | 'dialogReason' | 'dialogConfirm' | 'cardExecuting' | 'cardFailed' | 'cardDone' | 'cardWaitingResult' | 'confirmFailed' | 'cancelled'
+export type LocaleKey
+ = | 'dialogTitle'
+ | 'dialogIntro'
+ | 'dialogReason'
+ | 'dialogConfirm'
+ | 'cardExecuting'
+ | 'cardFailed'
+ | 'cardDone'
+ | 'cardRunning'
+ | 'cardAppliedFallback'
+ | 'cardWaitingResult'
+ | 'confirmFailed'
+ | 'confirmExecute'
+ | 'confirmExecuting'
+ | 'confirmSubmitted'
+ | 'cancelAction'
+ | 'cancelCancelling'
+ | 'cancelled'
+ | 'planExpiredHint'
+ | 'previewHint'
+ | 'sessionMissing'
const DICT_ZH: Record<LocaleKey, string> = {
dialogTitle: 'Turn 撤销不可用',
@@ -12,9 +32,19 @@ const DICT_ZH: Record<LocaleKey, string> = {
cardExecuting: '执行中',
cardFailed: '失败',
cardDone: '完成',
+ cardRunning: '运行中',
+ cardAppliedFallback: '已执行',
cardWaitingResult: '已提交,等待执行结果…',
confirmFailed: '执行确认失败:',
+ confirmExecute: '✓ 执行',
+ confirmExecuting: '执行中…',
+ confirmSubmitted: '已提交执行确认',
+ cancelAction: '✕ 取消',
+ cancelCancelling: '取消中…',
cancelled: '已取消',
+ planExpiredHint: '该计划已过期,重新执行 /undo 可生成新预览',
+ previewHint: '执行将恢复下方文件到本轮改动前',
+ sessionMissing: '无法确定该卡片所属的会话,请刷新页面后重试',
}
const DICT_EN: Record<LocaleKey, string> = {
@@ -25,9 +55,19 @@ const DICT_EN: Record<LocaleKey, string> = {
cardExecuting: 'Executing',
cardFailed: 'Failed',
cardDone: 'Done',
+ cardRunning: 'Running',
+ cardAppliedFallback: 'Applied',
cardWaitingResult: 'Submitted, waiting for the result…',
confirmFailed: 'Confirm failed: ',
+ confirmExecute: '✓ Apply',
+ confirmExecuting: 'Applying…',
+ confirmSubmitted: 'Apply confirmed',
+ cancelAction: '✕ Cancel',
+ cancelCancelling: 'Cancelling…',
cancelled: 'Cancelled',
+ planExpiredHint: 'This plan has expired; run /undo again for a fresh preview',
+ previewHint: 'Applying restores the files below to their state before this turn',
+ sessionMissing: 'Cannot determine the session this card belongs to; refresh the page and try again',
}
export const LOCALES = { zh: DICT_ZH, en: DICT_EN }
diff --git a/packages/dsh-tauri-turnrewind-ts/src/client/types/index.ts b/packages/dsh-tauri-turnrewind-ts/src/client/types/index.ts
index 683c2e9e..3d9b2b3d 100644
--- a/packages/dsh-tauri-turnrewind-ts/src/client/types/index.ts
+++ b/packages/dsh-tauri-turnrewind-ts/src/client/types/index.ts
@@ -2,6 +2,8 @@
* client/types/index.ts — 客户端共享类型。
*/
+import type { LocaleKey } from '../locales'
+
export type { LocaleKey } from '../locales'
/** 卡片里单个文件条目(解析 /undo 预览输出得到)。 */
@@ -34,3 +36,6 @@ export interface CommandViewProps {
node?: { id?: string, name?: string, sessionId?: string, outcome?: { kind?: string, text?: string } }
sessionId?: string
}
+
+/** locale 取词函数:apply 装配层按当前活跃语言注入,组件层消费。 */
+export type Translate = (key: LocaleKey) => string