From 4e68f7332f42119abd145a768fc8a83999f17470 Mon Sep 17 00:00:00 2001 From: fylorn <249551762+fylorn@users.noreply.github.com> Date: Sun, 20 Sep 2026 10:47:14 +0800 Subject: [PATCH] feat(i18n): move the sessions page into dictionaries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The last file on the pending list. It waited for the subscription-session change (#65), which added the turns served by a subscription and the wording that goes with them; translating before that landed would have meant translating text that was about to be rewritten. The cost cell says the same things as the overview — estimated, unpriced, no usage, subscription — so it uses the same wording, differing only in the unit: the overview counts requests, this page counts turns. The pending list is now empty. It stays as a guard: a new file with Chinese in it fails the test. Co-Authored-By: Claude Opus 5 --- src/Sessions.i18n.ts | 119 ++++++++++++++++++++++++++++++++++++++++ src/Sessions.tsx | 81 +++++++++++++++------------ src/i18n.test.ts | 8 +-- src/i18n/terminology.md | 3 + 4 files changed, 171 insertions(+), 40 deletions(-) create mode 100644 src/Sessions.i18n.ts diff --git a/src/Sessions.i18n.ts b/src/Sessions.i18n.ts new file mode 100644 index 00000000..4d288a6d --- /dev/null +++ b/src/Sessions.i18n.ts @@ -0,0 +1,119 @@ +import { messages } from "@/i18n"; + +/** + * 会话页的文案。 + * + * 费用那一格的限定语和概览页说的是同一批事(估算、无法计价、无用量、 + * 订阅),措辞跟着 `Dashboard.i18n.tsx` 走 —— 同一件事在两页上不该有两种 + * 说法。区别只在量词:概览按请求数,这里按轮次。 + */ +export const sessionsText = messages( + { + loading: "读取中…", + emptyTitle: "暂无会话记录", + emptyBody: "同一对话中的请求将聚合为会话,并显示在此处。", + + // 表头 + started: "开始", + client: "客户端", + turns: "轮次", + duration: "时长", + peakContext: "上下文峰值", + cacheSavings: "缓存节省", + cost: "费用", + + /** 轮次一列里跟在总轮数后面的失败数 */ + failedTurns: (n: number) => `${n} 失败`, + + // 时长。会话短的按秒算,长的按小时算 + seconds: (n: number) => `${n} 秒`, + minutes: (n: number) => `${n} 分`, + hours: (n: string) => `${n} 小时`, + + // 费用那一格。**估算、无法计价、无用量、订阅各说各的** + estimatedTip: (amount: string) => + `其中 ${amount} 为估算值:请求在响应结束前断开或中断,输出用量计至断开时;或模型的单价取自其他平台。`, + unpriced: "无法计价", + noPricedTurnsTip: "此会话中没有可计价的轮次", + unpricedTurns: (n: number) => `+${n} 轮无法计价`, + unpricedTurnsTip: "这些轮次所用的模型未定价,费用未计入合计", + noUsageTurns: (n: number) => `+${n} 轮无用量`, + noUsageTurnsTip: + "这些轮次没有用量数据:上游未报告,或连接在报告之前已结束。费用无法计算,未计入合计", + subscriptionTurns: (n: number) => `订阅额度 ${n} 轮`, + subscriptionTip: "这些轮次由订阅制上游服务,计入订阅额度,不按用量产生费用", + + // 详情 + /** `at` 是开始时刻写出来的样子 */ + detailTitle: (at: string, turns: number, duration: string) => + `${at} 的会话 · ${turns} 轮 · ${duration}`, + /** 模型名之间的分隔符 */ + modelSep: "、", + detailUsage: (models: string, input: string, output: string, cacheRead: string) => + `${models} · 输入 ${input} / 输出 ${output} · 缓存读取 ${cacheRead}`, + + growthTitle: "上下文增长(每轮的输入 token)", + /** 柱子的悬浮文字。`cached` 是缓存命中的那一段,没有时为 null */ + growthBar: (shown: string, _n: number, cached: string | null) => + `${shown} token${cached ? `,其中 ${cached} 为缓存命中` : ""}`, + growthLegend: (peak: string) => `绿色为缓存命中部分。峰值 ${peak} token。`, + + waterfallTitle: "每轮费用", + /** 瀑布里单独一轮的标注 */ + turnSubscription: "订阅额度", + turnFailed: "失败", + turnCancelled: "已取消", + }, + { + loading: "Loading…", + emptyTitle: "No sessions yet", + emptyBody: "Requests from the same conversation are grouped into a session and appear here.", + + started: "Started", + client: "Client", + turns: "Turns", + duration: "Duration", + peakContext: "Peak context", + cacheSavings: "Cache savings", + cost: "Cost", + + failedTurns: (n: number) => `${n} failed`, + + // 和 format.i18n.ts 的单位写法一致:s、min、h + seconds: (n: number) => `${n} s`, + minutes: (n: number) => `${n} min`, + hours: (n: string) => `${n} h`, + + estimatedTip: (amount: string) => + `${amount} of this is estimated: requests disconnected or were interrupted before the response finished, and output usage is counted up to the disconnect; or the model's price was taken from another platform.`, + unpriced: "Unpriced", + noPricedTurnsTip: "No turn in this session could be priced.", + unpricedTurns: (n: number) => `+${n} unpriced`, + unpricedTurnsTip: + "The models used by these turns have no price, so their cost is not included in the total.", + // 费用那一格在默认窗口宽度下只有 ~340px,几个限定语要挤在一行里: + // 单位交给「轮次」那一列,这里只写数目 + noUsageTurns: (n: number) => `+${n} no usage`, + noUsageTurnsTip: + "These turns have no usage data: the upstream did not report it, or the connection ended before it was reported. Their cost cannot be calculated and is not included in the total.", + subscriptionTurns: (n: number) => `${n} on subscription`, + subscriptionTip: + "These turns were served by a subscription upstream. They count against the subscription quota and do not incur cost by usage.", + + detailTitle: (at: string, turns: number, duration: string) => + `Session at ${at} · ${turns === 1 ? "1 turn" : `${turns} turns`} · ${duration}`, + modelSep: ", ", + detailUsage: (models: string, input: string, output: string, cacheRead: string) => + `${models} · Input ${input} / Output ${output} · Cache reads ${cacheRead}`, + + growthTitle: "Context growth (input tokens per turn)", + growthBar: (shown: string, n: number, cached: string | null) => + `${shown} ${n === 1 ? "token" : "tokens"}${cached ? `, ${cached} from cache` : ""}`, + growthLegend: (peak: string) => `Green marks the part served from cache. Peak ${peak} tokens.`, + + waterfallTitle: "Cost per turn", + turnSubscription: "Subscription", + turnFailed: "Failed", + turnCancelled: "Canceled", + }, +); diff --git a/src/Sessions.tsx b/src/Sessions.tsx index 5bba2cf6..30e2c148 100644 --- a/src/Sessions.tsx +++ b/src/Sessions.tsx @@ -5,6 +5,8 @@ import { usd, type SessionDetail, type SessionView, type TurnView } from "./type import { Empty, EmptyDescription, EmptyHeader, EmptyTitle } from "@/ui/empty"; import { toast } from "sonner"; import { useCoreEvent } from "./useCoreEvent"; +import { textOf, useText } from "@/i18n"; +import { sessionsText } from "./Sessions.i18n"; import { Table, TableBody, @@ -31,6 +33,7 @@ import { * 总额**。一个会撒谎的成本面板不如没有。 */ export default function Sessions() { + const t = useText(sessionsText); const [rows, setRows] = useState(null); const [open, setOpen] = useState(null); const [error, setError] = useState(null); @@ -53,7 +56,7 @@ export default function Sessions() { // 原来每 10 秒重算一遍,空闲时每一遍都算出同一个答案。 useCoreEvent(["request_finished", "request_failed", "request_cancelled"], () => void load()); - if (!rows) return
{error ?? "读取中…"}
; + if (!rows) return
{error ?? t.loading}
; if (rows.length === 0) { // 空状态永远在回答「接下来该做什么」 @@ -61,8 +64,8 @@ export default function Sessions() {
- 暂无会话记录 - 同一对话中的请求将聚合为会话,并显示在此处。 + {t.emptyTitle} + {t.emptyBody}
@@ -74,13 +77,13 @@ export default function Sessions() { - 开始 - 客户端 - 轮次 - 时长 - 上下文峰值 - 缓存节省 - 费用 + {t.started} + {t.client} + {t.turns} + {t.duration} + {t.peakContext} + {t.cacheSavings} + {t.cost} @@ -101,7 +104,7 @@ export default function Sessions() { {/* 轮次和失败数之间要有间隔 —— 挨着写会读成「181 失败」 */} {s.turns} {s.errors > 0 && ( - {s.errors} 失败 + {t.failedTurns(s.errors)} )} {dur(s.ended_ms - s.started_ms)} @@ -132,9 +135,10 @@ export default function Sessions() { * N 次」,这里用同一个说法。 */ function Cost({ s }: { s: SessionView }) { + const t = useText(sessionsText); const subscription = s.subscription_turns > 0 && ( - - 订阅额度 {s.subscription_turns} 轮 + + {t.subscriptionTurns(s.subscription_turns)} ); if (s.priced_turns === 0) { @@ -142,7 +146,7 @@ function Cost({ s }: { s: SessionView }) { if (subscription && s.unpriced_turns === 0 && s.no_usage_turns === 0) return subscription; return ( <> - 无法计价 + {t.unpriced} {subscription && {subscription}} ); @@ -151,20 +155,20 @@ function Cost({ s }: { s: SessionView }) { <> {s.cost_micros_estimated > 0 ? ( // **估算不能冒充实测**:合计里有估算的部分,就要带着记号 - + ~{usd(s.cost_micros)} ) : ( usd(s.cost_micros) )} {s.unpriced_turns > 0 && ( - - +{s.unpriced_turns} 轮无法计价 + + {t.unpricedTurns(s.unpriced_turns)} )} {s.no_usage_turns > 0 && ( - - +{s.no_usage_turns} 轮无用量 + + {t.noUsageTurns(s.no_usage_turns)} )} {subscription && {subscription}} @@ -193,9 +197,11 @@ function when(ms: number) { } function dur(ms: number) { - if (ms < 60_000) return `${Math.round(ms / 1000)} 秒`; - if (ms < 3_600_000) return `${Math.round(ms / 60_000)} 分`; - return `${(ms / 3_600_000).toFixed(1)} 小时`; + // 取文案要在调用的那一刻,不能提到模块级 —— 换了语言它不会跟着换 + const t = textOf(sessionsText); + if (ms < 60_000) return t.seconds(Math.round(ms / 1000)); + if (ms < 3_600_000) return t.minutes(Math.round(ms / 60_000)); + return t.hours((ms / 3_600_000).toFixed(1)); } function tokens(n: number) { @@ -205,18 +211,23 @@ function tokens(n: number) { /** 一次会话的细节:上下文增长曲线 + 每轮的成本瀑布。 */ function Detail({ d, onClose }: { d: SessionDetail; onClose: () => void }) { + const t = useText(sessionsText); const { session: s, turns } = d; return ( !o && onClose()}> - {when(s.started_ms)} 的会话 · {s.turns} 轮 · {dur(s.ended_ms - s.started_ms)} + {t.detailTitle(when(s.started_ms), s.turns, dur(s.ended_ms - s.started_ms))}
- {s.models.join("、")} · 输入 {tokens(s.input_tokens)} / 输出 {tokens(s.output_tokens)} · - 缓存读取 {tokens(s.cache_read_tokens)} + {t.detailUsage( + s.models.join(t.modelSep), + tokens(s.input_tokens), + tokens(s.output_tokens), + tokens(s.cache_read_tokens), + )}
@@ -233,10 +244,11 @@ function Detail({ d, onClose }: { d: SessionDetail; onClose: () => void }) { * 那个东西 —— 折线会把那一跳平滑掉一部分。 */ function Growth({ turns }: { turns: TurnView[] }) { + const text = useText(sessionsText); const max = Math.max(1, ...turns.map((t) => t.input_tokens ?? 0)); return (
-
上下文增长(每轮的输入 token)
+
{text.growthTitle}
{turns.map((t) => { const v = t.input_tokens ?? 0; @@ -246,7 +258,7 @@ function Growth({ turns }: { turns: TurnView[] }) { key={t.id} className="flex-1 bg-neutral-200 dark:bg-neutral-700" style={{ height: `${Math.max(2, (v / max) * 100)}%` }} - title={`${tokens(v)} token${cached > 0 ? `,其中 ${tokens(cached)} 为缓存命中` : ""}`} + title={text.growthBar(tokens(v), v, cached > 0 ? tokens(cached) : null)} > {/* 缓存命中的那一段单独染色 —— 看出哪一轮打断了缓存 */}
-
- 绿色为缓存命中部分。峰值 {tokens(max)} token。 -
+
{text.growthLegend(tokens(max))}
); } /** 每轮的成本瀑布 —— 找出那个 8 万 token 的文件读取。 */ function Waterfall({ turns }: { turns: TurnView[] }) { + const text = useText(sessionsText); const max = Math.max(1, ...turns.map((t) => t.cost_micros ?? 0)); return (
-
每轮费用
+
{text.waterfallTitle}
    {turns.map((t, i) => (
  • @@ -284,16 +295,16 @@ function Waterfall({ turns }: { turns: TurnView[] }) { {/* **没有价格就说没有价格,不写 $0**;订阅那一轮也没有金额,但它不是没有价格 */} {t.billing === "subscription" ? ( - 订阅额度 + {text.turnSubscription} ) : t.cost_micros == null ? ( - 无法计价 + {text.unpriced} ) : ( // 估算的金额要带记号:取消、断在中间的那几轮输出只计到断开时 (t.cost_estimated ? "~" : "") + usd(t.cost_micros) )} - {t.error && 失败} - {t.cancelled && 已取消} + {t.error && {text.turnFailed}} + {t.cancelled && {text.turnCancelled}}
  • ))}
diff --git a/src/i18n.test.ts b/src/i18n.test.ts index 30c68f3f..c4dca15b 100644 --- a/src/i18n.test.ts +++ b/src/i18n.test.ts @@ -31,12 +31,10 @@ describe("界面语言", () => { const CJK = /[\u3400-\u9fff\u3000-\u303f\uff00-\uffef]/; /** - * 还没迁到词表里的文件。**只减不增**:迁完一个就从这里删掉,下面有一条 - * 检查会在「已经迁完却还留在清单上」时报错。新文件一律不许进这个清单。 + * 还没迁到词表里的文件。**现在是空的 —— 全都迁完了。**清单留着当守卫: + * 新写的文件一律不许进来,下面第一条检查就是那道门。 */ -const PENDING = new Set([ - "src/Sessions.tsx", -]); +const PENDING = new Set([]); function files(dir: string): string[] { const out: string[] = []; diff --git a/src/i18n/terminology.md b/src/i18n/terminology.md index 1420afbb..47934267 100644 --- a/src/i18n/terminology.md +++ b/src/i18n/terminology.md @@ -55,6 +55,9 @@ known colloquialisms. | 概览 | Overview | sidebar | | 流量 | Traffic | sidebar; the list of requests | | 会话 | Sessions / session | | +| 轮次 / 轮 | turns / turn | one request within a session | +| 上下文峰值 | peak context | | +| 缓存节省 | cache savings | | | 发现 | Findings | sidebar | | 防护 | Protection | sidebar | | 上游 | Upstreams / upstream | |