Skip to content

Commit 8979268

Browse files
committed
feat(dsh-idle-archive): i18n 试点——双语字典 + 官方 locale 双通道接入(#348)
- src/client/locales.ts:zh key 源 + en satisfies 锁平衡,双语文案覆盖弹窗/设置卡/toast - declare module 合并 LocaleNamespaceMap(官方 ui-jobs 同款模式);显式 import type 先把 ui-slots 拉进解析图绕开 TS2664(上游发布物 lib/types/*.d.ts 相对导入保留 .ts 后缀致 augmentation 模块名解析失败,microsoft/TypeScript#63960 同类,上游修复后可删) - 双通道接线:ctx.locale.register/bind(原生 DOM)+ slots.register locale: NS(React t seat); 语言切换 subscribe 重绑 t 并重绘 - tsconfig +rewriteRelativeImportExtensions(./locales.ts 后缀 emit 改写) - devDeps 仅留 ui-slots/store(纯类型面),未用 locale 类型包不引入 - smoke 补 i18n 接入哨兵断言(NS/register/bind/双语文案/locale 参数)
1 parent 5029176 commit 8979268

7 files changed

Lines changed: 445 additions & 237 deletions

File tree

‎packages/dsh-idle-archive/package.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@
8181
},
8282
"devDependencies": {
8383
"@deepseek-ai/cordis": "catalog:",
84+
"@deepseek-ai/dsh-client-store": "catalog:",
85+
"@deepseek-ai/dsh-client-ui-slots": "catalog:",
8486
"@deepseek-ai/dsh-host-webserver": "catalog:",
8587
"schemastery": "^3.18.0"
8688
}

‎packages/dsh-idle-archive/src/client/index.ts‎

Lines changed: 80 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,38 @@
2020
import * as React from "react";
2121
// 样式:独立 style.css(见同目录),build-client 的 .css text-loader 构建期内联为字符串
2222
import STYLE from "./style.css";
23+
// i18n(issue #348):复用官方 dsh-client-locale——zh/en 双语字典,LocaleNamespaceMap
24+
// 声明合并进官方 ui-slots 类型面;仅 import type(编译期擦除,无运行时依赖)。
25+
import { zh, en, type IdleArchiveLocaleKey } from "./locales.ts";
26+
// 显式类型导入,先把 @deepseek-ai/dsh-client-ui-slots 拉进模块解析图:上游发布物
27+
// lib/types/*.d.ts 相对导入保留 .ts 后缀(renderer.ts / store.ts,磁盘只有 .d.ts),
28+
// declare module 增强的模块名解析(不走过 import 的解析缓存)会判 TS2664 cannot be
29+
// found(microsoft/TypeScript#63960 顺序依赖同类问题;TS 5.9/6/7 实测一致)。待上游
30+
// 修复发布物后此行可删。
31+
import type { LocaleNamespaceMap } from "@deepseek-ai/dsh-client-ui-slots";
32+
33+
declare module "@deepseek-ai/dsh-client-ui-slots" {
34+
interface LocaleNamespaceMap {
35+
/** dsh-idle-archive 弹窗/设置卡文案。 */
36+
"settings.idleArchive": IdleArchiveLocaleKey;
37+
}
38+
}
39+
40+
/** 本插件字典命名空间(宿主 locale 服务注册用)。 */
41+
const NS = "settings.idleArchive";
2342

2443
var CHANNEL = "/dsh-idle-archive";
2544
var MODAL_ID = "dsh-idle-archive-modal";
2645
var STYLE_ID = "dsh-idle-archive-style";
2746
var CSS_VERSION = "1";
2847
var DEFAULTS = { enabled: true, idleHours: 72, snoozeHours: 24, scanMinutes: 60, maxRows: 50 };
2948

49+
/** i18n 翻译函数(apply 时由 ctx.locale.bind(NS) 装配;未装配回落 key 本体,行为零变化)。 */
50+
var t: any = function (key: string, params?: any) {
51+
if (params === undefined) return key;
52+
return String(key); // 未装配时占位插值忽略(正常路径早已装配)
53+
};
54+
3055
/** 全局运行态(apply 内部使用)。 */
3156
var state: Record<string, any> = { settings: null, snoozed: {}, candidates: [], modalOpen: false };
3257
var rpc: any = null;
@@ -56,13 +81,13 @@ import STYLE from "./style.css";
5681
/** 相对时间描述。 */
5782
function fmtRelative(ts: any) {
5883
var diff = Date.now() - ts;
59-
if (diff < 60 * 1000) return "刚刚";
84+
if (diff < 60 * 1000) return t("justNow");
6085
var min = Math.floor(diff / 60000);
61-
if (min < 60) return min + " 分钟前";
86+
if (min < 60) return t("minutesAgo", { n: min });
6287
var h = Math.floor(min / 60);
63-
if (h < 24) return h + " 小时前";
88+
if (h < 24) return t("hoursAgo", { n: h });
6489
var d = Math.floor(h / 24);
65-
if (d < 30) return d + " 天前";
90+
if (d < 30) return t("daysAgo", { n: d });
6691
return new Date(ts).toLocaleDateString();
6792
}
6893

@@ -93,13 +118,13 @@ import STYLE from "./style.css";
93118
function doArchive(id: any) {
94119
if (!workspaces) return;
95120
(workspaces as any).archiveSession(id).then(function () {
96-
toast("已归档会话");
121+
toast(t("archivedOk"));
97122
state.candidates = state.candidates.filter(function (c: any) { return c.id !== id; });
98123
// 归档后清掉该会话的静默标记(下次扫描不会再出现,已归档者天然跳过)。
99124
rpc("clearSnooze", { sessionId: id }).catch(function () {});
100125
renderModal();
101126
}).catch(function (e: any) {
102-
toast("归档失败:" + (e && e.message || e), true);
127+
toast(t("archivedFail", { msg: (e && e.message || e) }), true);
103128
});
104129
}
105130

@@ -137,20 +162,20 @@ import STYLE from "./style.css";
137162
title.title = row.title;
138163
var time = document.createElement("div");
139164
time.className = "dia-row-time";
140-
time.textContent = "最后对话 " + fmtRelative(row.updatedAt) + "(" + new Date(row.updatedAt).toLocaleString() + ")";
165+
time.textContent = t("rowLastSeen", { rel: fmtRelative(row.updatedAt), abs: new Date(row.updatedAt).toLocaleString() });
141166
main.appendChild(title);
142167
main.appendChild(time);
143168

144169
var archiveBtn = document.createElement("button");
145170
archiveBtn.type = "button";
146171
archiveBtn.className = "dia-btn dia-btn-archive";
147-
archiveBtn.textContent = "归档";
172+
archiveBtn.textContent = t("archive");
148173
archiveBtn.addEventListener("click", function () { doArchive(row.id); });
149174

150175
var snoozeBtn = document.createElement("button");
151176
snoozeBtn.type = "button";
152177
snoozeBtn.className = "dia-btn";
153-
snoozeBtn.textContent = "暂不归档";
178+
snoozeBtn.textContent = t("snoozeRow");
154179
snoozeBtn.addEventListener("click", function () { doSnooze([row.id], (state.settings && state.settings.snoozeHours) || DEFAULTS.snoozeHours); });
155180

156181
el.appendChild(main);
@@ -178,21 +203,24 @@ import STYLE from "./style.css";
178203
var head = document.createElement("div");
179204
head.className = "dia-head";
180205
var headText = document.createElement("span");
181-
headText.textContent = "会话闲置提醒";
206+
headText.textContent = t("modalTitle");
182207
var closeBtn = document.createElement("button");
183208
closeBtn.type = "button";
184209
closeBtn.className = "dia-close";
185210
closeBtn.textContent = "×";
186-
closeBtn.title = "全部暂不归档(Y 小时内不再提醒)";
211+
closeBtn.title = t("closeTitle");
187212
closeBtn.addEventListener("click", function () { doSnooze(state.candidates.map(function (c: any) { return c.id; }), 1); });
188213
head.appendChild(headText);
189214
head.appendChild(closeBtn);
190215

191216
var hours = (state.settings && state.settings.idleHours) || DEFAULTS.idleHours;
192217
var desc = document.createElement("div");
193218
desc.className = "dia-desc";
194-
desc.textContent = "以下 " + candidates.length + " 个会话超过 " + hours + " 小时未对话。归档后从会话列表隐藏,会话记录仍保留;" +
195-
"「暂不归档」后 " + ((state.settings && state.settings.snoozeHours) || DEFAULTS.snoozeHours) + " 小时内不再提醒该会话。";
219+
desc.textContent = t("desc", {
220+
count: candidates.length,
221+
hours: hours,
222+
snooze: (state.settings && state.settings.snoozeHours) || DEFAULTS.snoozeHours,
223+
});
196224

197225
var list = document.createElement("div");
198226
list.className = "dia-list";
@@ -201,11 +229,11 @@ import STYLE from "./style.css";
201229
foot.className = "dia-foot";
202230
var hint = document.createElement("span");
203231
hint.className = "dia-hint";
204-
hint.textContent = "× / Esc:仅关闭(1 小时内不重复提醒);「全部暂不归档」:按静默时长不提醒";
232+
hint.textContent = t("hint");
205233
var allBtn = document.createElement("button");
206234
allBtn.type = "button";
207235
allBtn.className = "dia-all";
208-
allBtn.textContent = "全部暂不归档";
236+
allBtn.textContent = t("allSnooze");
209237
allBtn.addEventListener("click", function () {
210238
var hours = (state.settings && state.settings.snoozeHours) || DEFAULTS.snoozeHours;
211239
doSnooze(state.candidates.map(function (c: any) { return c.id; }), hours);
@@ -353,7 +381,7 @@ import STYLE from "./style.css";
353381
}, []);
354382

355383
if (!settings) {
356-
return React.createElement("li", { className: "dia-set-card" }, "会话闲置提醒:加载中…");
384+
return React.createElement("li", { className: "dia-set-card" }, t("settingsLoading"));
357385
}
358386

359387
function patch(p: any) {
@@ -365,11 +393,11 @@ import STYLE from "./style.css";
365393
rpc("config", { settings: settings }).then(function (v: any) {
366394
state.settings = v.settings;
367395
setSettings(v.settings);
368-
setSaved("已保存");
396+
setSaved(t("savedOk"));
369397
scheduleSoon();
370398
setTimeout(function () { setSaved(""); }, 1800);
371399
}).catch(function (e: any) {
372-
setSaved("保存失败:" + (e && e.message || e));
400+
setSaved(t("savedFail", { msg: (e && e.message || e) }));
373401
});
374402
}
375403

@@ -378,12 +406,12 @@ import STYLE from "./style.css";
378406
setSaved("");
379407
if (disposed || state.modalOpen) return;
380408
if (!state.settings || !state.settings.enabled) {
381-
toast("会话闲置提醒已禁用");
409+
toast(t("disabledHint"));
382410
return;
383411
}
384412
var list = collectCandidates(true);
385413
if (!list.length) {
386-
toast("暂无需要归档的会话");
414+
toast(t("noCandidates"));
387415
return;
388416
}
389417
rpc("titles", { sessionIds: list.map(function (c: any) { return c.id; }) }).then(function (v: any) {
@@ -407,8 +435,8 @@ import STYLE from "./style.css";
407435
onClick: function () { setOpen(!open); },
408436
},
409437
React.createElement("span", { className: "dia-set-headText" },
410-
React.createElement("span", { className: "dia-set-name" }, "会话闲置提醒"),
411-
React.createElement("span", { className: "dia-set-description" }, "闲置阈值 / 静默时长 / 扫描间隔 / 立即检查"),
438+
React.createElement("span", { className: "dia-set-name" }, t("settingsName")),
439+
React.createElement("span", { className: "dia-set-description" }, t("settingsDescription")),
412440
),
413441
React.createElement("svg", {
414442
className: "dia-set-chevron" + (open ? " dia-set-chevronOpen" : ""),
@@ -427,15 +455,15 @@ import STYLE from "./style.css";
427455
),
428456
open ? React.createElement("div", { className: "dia-set-body" },
429457
React.createElement("div", { className: "dia-set-row" },
430-
React.createElement("label", null, "启用"),
458+
React.createElement("label", null, t("enable")),
431459
React.createElement("input", {
432460
type: "checkbox",
433461
checked: settings.enabled,
434462
onChange: function (e: any) { patch({ enabled: e.target.checked }); },
435463
}),
436464
),
437465
React.createElement("div", { className: "dia-set-row" },
438-
React.createElement("label", null, "闲置阈值(小时)"),
466+
React.createElement("label", null, t("idleHours")),
439467
React.createElement("input", {
440468
className: "dia-set-input",
441469
type: "number",
@@ -446,7 +474,7 @@ import STYLE from "./style.css";
446474
}),
447475
),
448476
React.createElement("div", { className: "dia-set-row" },
449-
React.createElement("label", null, "拒绝后静默(小时)"),
477+
React.createElement("label", null, t("snoozeHours")),
450478
React.createElement("input", {
451479
className: "dia-set-input",
452480
type: "number",
@@ -457,7 +485,7 @@ import STYLE from "./style.css";
457485
}),
458486
),
459487
React.createElement("div", { className: "dia-set-row" },
460-
React.createElement("label", null, "扫描间隔(分钟)"),
488+
React.createElement("label", null, t("scanMinutes")),
461489
React.createElement("select", {
462490
className: "dia-set-input",
463491
value: settings.scanMinutes,
@@ -472,8 +500,8 @@ import STYLE from "./style.css";
472500
React.createElement("div", { className: "dia-set-foot" },
473501
React.createElement("span", { className: "dia-set-saved" }, saved),
474502
React.createElement("span", { style: { display: "flex", gap: 8 } },
475-
React.createElement("button", { type: "button", className: "dia-set-check", onClick: checkNow }, "立即检查"),
476-
React.createElement("button", { type: "button", className: "dia-set-save", onClick: save }, "保存"),
503+
React.createElement("button", { type: "button", className: "dia-set-check", onClick: checkNow }, t("checkNow")),
504+
React.createElement("button", { type: "button", className: "dia-set-save", onClick: save }, t("save")),
477505
),
478506
),
479507
) : null,
@@ -502,6 +530,17 @@ import STYLE from "./style.css";
502530

503531
injectStyle();
504532

533+
// i18n(issue #348):注册本插件字典;t 绑定官方 locale 服务(未装配回落 key 本体)。
534+
var locale: any = ctx.get("locale");
535+
if (locale && typeof locale.register === "function") {
536+
try {
537+
locale.register(NS, { zh: zh, en: en });
538+
t = locale.bind(NS);
539+
} catch (e) {
540+
console.warn("[dsh-idle-archive] locale 注册失败:", e);
541+
}
542+
}
543+
505544
// 先按默认配置可用,宿主状态加载后覆盖(配置/snooze 持久化在宿主)。
506545
state.settings = DEFAULTS;
507546
rpc("state", {}).then(function (st: any) {
@@ -533,14 +572,24 @@ import STYLE from "./style.css";
533572
if (slots) {
534573
slots.inject("settings.plugin.item", function () {
535574
return slots.register(
536-
{ name: "settings.plugin.item", id: "dsh-idle-archive", key: "dsh-idle-archive", order: 45 },
575+
{ name: "settings.plugin.item", id: "dsh-idle-archive", key: "dsh-idle-archive", order: 45, locale: NS },
537576
function () {
538577
return React.createElement(SettingsCard, null);
539578
}
540579
);
541580
});
542581
}
543582

583+
// i18n 切语言重绘:locale 快照变化 → 重绑 t 并重建弹窗(原生 DOM 通道按 revision 刷新)。
584+
var unsubLocale: any = null;
585+
if (locale && typeof locale.subscribe === "function" && typeof locale.getSnapshot === "function") {
586+
unsubLocale = locale.subscribe(function () {
587+
try { t = locale.bind(NS); } catch (e) { /* 忽略 */ }
588+
if (state.modalOpen) { closeModal(); state.candidates = state.candidates.slice(); }
589+
scheduleSoon();
590+
});
591+
}
592+
544593
// ⚠️ 清理必须写在 ctx.effect 返回的 disposer 里。
545594
ctx.effect(function () {
546595
return function () {
@@ -550,6 +599,7 @@ import STYLE from "./style.css";
550599
document.removeEventListener("visibilitychange", onVisibility);
551600
if (unsubSessions) unsubSessions();
552601
if (unsubWorkspaces) unsubWorkspaces();
602+
if (unsubLocale) unsubLocale();
553603
if (modalEl) modalEl.remove();
554604
var style = document.getElementById(STYLE_ID);
555605
if (style) style.remove();
@@ -561,4 +611,4 @@ import STYLE from "./style.css";
561611
}
562612

563613
// ---- 浏览器半区契约:apply/inject 由 build-client 经 factory 装配(干净模块)----
564-
export const inject: string[] = ["slots"];
614+
export const inject: string[] = ["slots", "locale"];
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* dsh-idle-archive — 客户端文案字典(issue #348:复用官方 dsh-client-locale)。
3+
*
4+
* 双语平衡:`zh` 为 key 源;`en` 必须覆盖全部 key(`satisfies keyof typeof zh`
5+
* 编译期锁平衡)。动态数据用 `{name}` 占位模板,渲染期由 t 插值。
6+
* 宿主端稳定标识/事件名/配置键不翻译(官方原则:数据不翻译)。
7+
*/
8+
9+
/** 简体中文字典(key 源)。 */
10+
export const zh = {
11+
// 相对时间(fmtRelative)
12+
justNow: "刚刚",
13+
minutesAgo: "{n} 分钟前",
14+
hoursAgo: "{n} 小时前",
15+
daysAgo: "{n} 天前",
16+
// 弹窗
17+
modalTitle: "会话闲置提醒",
18+
closeTitle: "全部暂不归档(Y 小时内不再提醒)",
19+
desc: "以下 {count} 个会话超过 {hours} 小时未对话。归档后从会话列表隐藏,会话记录仍保留;「暂不归档」后 {snooze} 小时内不再提醒该会话。",
20+
hint: "× / Esc:仅关闭(1 小时内不重复提醒);「全部暂不归档」:按静默时长不提醒",
21+
allSnooze: "全部暂不归档",
22+
rowLastSeen: "最后对话 {rel}({abs})",
23+
archive: "归档",
24+
snoozeRow: "暂不归档",
25+
// toast
26+
archivedOk: "已归档会话",
27+
archivedFail: "归档失败:{msg}",
28+
// 设置卡
29+
settingsLoading: "会话闲置提醒:加载中…",
30+
savedOk: "已保存",
31+
savedFail: "保存失败:{msg}",
32+
disabledHint: "会话闲置提醒已禁用",
33+
noCandidates: "暂无需要归档的会话",
34+
settingsName: "会话闲置提醒",
35+
settingsDescription: "闲置阈值 / 静默时长 / 扫描间隔 / 立即检查",
36+
enable: "启用",
37+
idleHours: "闲置阈值(小时)",
38+
snoozeHours: "拒绝后静默(小时)",
39+
scanMinutes: "扫描间隔(分钟)",
40+
checkNow: "立即检查",
41+
save: "保存",
42+
} as const;
43+
44+
/** 字典 key 并集(LocaleNamespaceMap 声明合并用)。 */
45+
export type IdleArchiveLocaleKey = keyof typeof zh;
46+
47+
/** 英文词典:必须与 zh key 完整对齐。 */
48+
export const en: Record<IdleArchiveLocaleKey, string> = {
49+
justNow: "just now",
50+
minutesAgo: "{n} minutes ago",
51+
hoursAgo: "{n} hours ago",
52+
daysAgo: "{n} days ago",
53+
modalTitle: "Idle session reminder",
54+
closeTitle: "Snooze all (no reminder for Y hours)",
55+
desc: "{count} session(s) idle for over {hours} hours. Archiving hides them from the session list but keeps their history; snoozing silences the reminder for {snooze} hours.",
56+
hint: "× / Esc: close only (won't remind again for 1 hour); snooze-all uses the configured duration",
57+
allSnooze: "Snooze all",
58+
rowLastSeen: "Last seen {rel} ({abs})",
59+
archive: "Archive",
60+
snoozeRow: "Snooze",
61+
archivedOk: "Session archived",
62+
archivedFail: "Archive failed: {msg}",
63+
settingsLoading: "Idle archive: loading…",
64+
savedOk: "Saved",
65+
savedFail: "Save failed: {msg}",
66+
disabledHint: "Idle session reminder is disabled",
67+
noCandidates: "No sessions to archive",
68+
settingsName: "Idle archive",
69+
settingsDescription: "Idle threshold / snooze duration / scan interval / check now",
70+
enable: "Enabled",
71+
idleHours: "Idle threshold (hours)",
72+
snoozeHours: "Snooze after decline (hours)",
73+
scanMinutes: "Scan interval (minutes)",
74+
checkNow: "Check now",
75+
save: "Save",
76+
};

‎packages/dsh-idle-archive/test/smoke.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,12 @@ assertClientProductContract(pkgDir);
282282
// CHANNEL 一致性(宿主与客户端单一来源)
283283
assert.ok(client.includes('"/dsh-idle-archive"'), "客户端 CHANNEL 与宿主一致");
284284

285+
// i18n 接入哨兵(issue #348):双语字典 + 官方 locale 双通道接线进产物
286+
assert.ok(client.includes('"settings.idleArchive"'), "i18n 命名空间 NS 进产物");
287+
assert.ok(client.includes("locale.register"), "locale.register(字典注册)进产物");
288+
assert.ok(client.includes("locale.bind"), "locale.bind(原生 DOM 通道 t 绑定)进产物");
289+
assert.ok(client.includes("just now") && client.includes("justNow"), "en/zh 双语字典进产物");
290+
assert.ok(client.includes("locale: NS"), "slots.register locale 参数(React 通道)进产物");
291+
285292
console.log("PASS: dsh-idle-archive smoke(handler / apply / 围栏 / 契约)");
286293
cleanup();

0 commit comments

Comments
 (0)