Skip to content

Commit a761050

Browse files
authored
[COD-44] 修复侧栏导航与全局组件的中文残留 (#89)
* fix COD-44:侧栏与全局组件中文残留改走 i18n * fix COD-44:展示用文案表改为渲染时翻译,补英文无中文护栏
1 parent 31b19a1 commit a761050

45 files changed

Lines changed: 1476 additions & 827 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { afterEach, describe, expect, it } from "vitest";
2+
import { setTrLang } from "@/i18n";
3+
import {
4+
ALERT_RULE_LABEL,
5+
CREDIT_SOURCE_LABEL,
6+
METRIC_AXIS_UNIT,
7+
STATE_LABEL,
8+
TASK_REPORT_LABEL,
9+
UPSTREAM_LEVEL_LABEL,
10+
USAGE_ERROR_LABEL,
11+
alertRuleLabel,
12+
creditSourceLabel,
13+
credentialStateLabel,
14+
metricAxisUnit,
15+
taskReportLabel,
16+
upstreamLevelLabel,
17+
usageErrorLabel,
18+
} from "../display";
19+
import { TIMELINE_EVENT_LABEL, timelineEventLabel } from "../credentialTimeline";
20+
import type { CredentialState } from "../display";
21+
import type { UpstreamLevel } from "../types";
22+
23+
/**
24+
* 展示用常量表的英文护栏(COD-44 返工)。
25+
*
26+
* 表值必须是**中文源串(msgid)**,翻译留到渲染时由 `xxxLabel()` 过 `tr`:
27+
* 模块级 `tr(...)` 会把加载瞬间的语言冻进常量,切英文后表里仍是中文
28+
* (「尝试 3 · 失败 1」就是这么漏出来的)。这里对每张表逐个取值断言
29+
* 英文下无中日韩字符、中文下与表值完全一致。
30+
*/
31+
const CJK = /[\u4e00-\u9fff]/;
32+
33+
afterEach(() => setTrLang("zh"));
34+
35+
describe("展示表在英文下不留中文", () => {
36+
it("STATE_LABEL → credentialStateLabel", () => {
37+
const states = Object.keys(STATE_LABEL) as CredentialState[];
38+
setTrLang("en");
39+
const en = states.map((state) => credentialStateLabel(state));
40+
expect(en.filter((label) => CJK.test(label))).toEqual([]);
41+
setTrLang("zh");
42+
expect(states.map((state) => credentialStateLabel(state)))
43+
.toEqual(states.map((state) => STATE_LABEL[state]));
44+
});
45+
46+
it("TASK_REPORT_LABEL → taskReportLabel(含拼接后的整行)", () => {
47+
const keys = Object.keys(TASK_REPORT_LABEL);
48+
const report = Object.fromEntries(keys.map((key) => [key, 3]));
49+
setTrLang("en");
50+
expect(CJK.test(taskReportLabel(report))).toBe(false);
51+
expect(taskReportLabel({ unknown_field: 1 })).toBe("unknown_field 1");
52+
// 表里的每个 key 都出现在整行里(不是被 key 兜底吞掉)
53+
expect(taskReportLabel(report)).not.toContain("succeeded 3");
54+
setTrLang("zh");
55+
expect(taskReportLabel({ attempted: 3, failed: 1 })).toBe("尝试 3 · 失败 1");
56+
});
57+
58+
it("CREDIT_SOURCE_LABEL → creditSourceLabel", () => {
59+
const sources = Object.keys(CREDIT_SOURCE_LABEL);
60+
setTrLang("en");
61+
expect(sources.map(creditSourceLabel).filter((label) => CJK.test(label))).toEqual([]);
62+
expect(creditSourceLabel("nope")).toBe("nope");
63+
setTrLang("zh");
64+
expect(sources.map(creditSourceLabel)).toEqual(sources.map((s) => CREDIT_SOURCE_LABEL[s]));
65+
});
66+
67+
it("METRIC_AXIS_UNIT → metricAxisUnit(无单位指标返回 undefined)", () => {
68+
const metrics = Object.keys(METRIC_AXIS_UNIT);
69+
setTrLang("en");
70+
expect(metrics.map(metricAxisUnit).filter((unit) => CJK.test(String(unit)))).toEqual([]);
71+
expect(metricAxisUnit("latency")).toBeUndefined();
72+
expect(metricAxisUnit("ttfb")).toBeUndefined();
73+
setTrLang("zh");
74+
expect(metrics.map(metricAxisUnit)).toEqual(metrics.map((m) => METRIC_AXIS_UNIT[m]));
75+
});
76+
77+
it("USAGE_ERROR_LABEL → usageErrorLabel(未知类型兜底也不回中文)", () => {
78+
const types = Object.keys(USAGE_ERROR_LABEL);
79+
setTrLang("en");
80+
expect(types.map(usageErrorLabel).filter((label) => CJK.test(label))).toEqual([]);
81+
expect(CJK.test(usageErrorLabel(null))).toBe(false);
82+
expect(CJK.test(usageErrorLabel("brand_new"))).toBe(false);
83+
setTrLang("zh");
84+
expect(types.map(usageErrorLabel)).toEqual(types.map((t) => USAGE_ERROR_LABEL[t as keyof typeof USAGE_ERROR_LABEL]));
85+
});
86+
87+
it("UPSTREAM_LEVEL_LABEL → upstreamLevelLabel(未知档位兜底)", () => {
88+
const levels = Object.keys(UPSTREAM_LEVEL_LABEL) as UpstreamLevel[];
89+
setTrLang("en");
90+
expect(levels.map(upstreamLevelLabel).filter((label) => CJK.test(label))).toEqual([]);
91+
expect(CJK.test(upstreamLevelLabel("brand_new"))).toBe(false);
92+
setTrLang("zh");
93+
expect(levels.map(upstreamLevelLabel)).toEqual(levels.map((l) => UPSTREAM_LEVEL_LABEL[l]));
94+
});
95+
96+
it("TIMELINE_EVENT_LABEL → timelineEventLabel(未知事件原样透出)", () => {
97+
const events = Object.keys(TIMELINE_EVENT_LABEL);
98+
setTrLang("en");
99+
expect(events.map(timelineEventLabel).filter((label) => CJK.test(label))).toEqual([]);
100+
expect(timelineEventLabel("brand_new")).toBe("brand_new");
101+
setTrLang("zh");
102+
expect(events.map(timelineEventLabel)).toEqual(events.map((e) => TIMELINE_EVENT_LABEL[e]));
103+
});
104+
105+
it("ALERT_RULE_LABEL → alertRuleLabel", () => {
106+
const rules = Object.keys(ALERT_RULE_LABEL);
107+
setTrLang("en");
108+
expect(rules.map(alertRuleLabel).filter((label) => CJK.test(label))).toEqual([]);
109+
setTrLang("zh");
110+
expect(rules.map(alertRuleLabel)).toEqual(rules.map((r) => ALERT_RULE_LABEL[r]));
111+
});
112+
});

‎web/src/api/apiKeys.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,22 @@
1010
import { ApiError } from "./client";
1111
import type { ApiKey } from "./types";
1212
import { isSessionExpiry, notifyUnauthorized } from "./unauthorized";
13-
import { acceptLanguageHeader } from "../i18n";
13+
import { acceptLanguageHeader, tr } from "../i18n";
1414

1515
/** 列表用量窗口:今日 / 近 7 天 / 近 30 天(含今天在内的自然日)。 */
1616
export type ApiKeyWindow = "today" | "7d" | "30d";
1717

1818
/** 三档切换控件与表头的文案(单一来源,避免「近 7 天」两处写法不一致)。 */
1919
export const USAGE_WINDOWS: { value: ApiKeyWindow; label: string }[] = [
20-
{ value: "today", label: "今日" },
21-
{ value: "7d", label: "近 7 天" },
22-
{ value: "30d", label: "近 30 天" },
20+
{ value: "today", label: tr("今日") },
21+
{ value: "7d", label: tr("近 7 天") },
22+
{ value: "30d", label: tr("近 30 天") },
2323
];
2424

2525
export const USAGE_WINDOW_LABELS: Record<ApiKeyWindow, string> = {
26-
today: "今日",
27-
"7d": "近 7 天",
28-
"30d": "近 30 天",
26+
today: tr("今日"),
27+
"7d": tr("近 7 天"),
28+
"30d": tr("近 30 天"),
2929
};
3030

3131
export interface ApiKeyListResponse {

‎web/src/api/client.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ async function request<T>(path: string, init: RequestInit = {},
150150
// 超时归一为可展示的 ApiError;调用方主动取消(切页/翻页)原样上抛,
151151
// react-query 依赖它是 AbortError 来判定「取消」而非「失败」。
152152
if (guard.timedOut()) {
153-
throw new ApiError(0, "timeout", tr("请求超时({0} 秒)", [timeoutMs / 1000]));
153+
throw new ApiError(0, "timeout", tr(tr("请求超时({0} 秒)"), [timeoutMs / 1000]));
154154
}
155155
throw error;
156156
} finally {
@@ -173,7 +173,7 @@ async function request<T>(path: string, init: RequestInit = {},
173173
throw new ApiError(
174174
response.status,
175175
"invalid_response",
176-
tr("服务端返回了非 JSON 响应(可能是反向代理错误页),请检查服务是否正常。"),
176+
tr(tr("服务端返回了非 JSON 响应(可能是反向代理错误页),请检查服务是否正常。")),
177177
);
178178
}
179179
return body as T;
@@ -251,7 +251,7 @@ export const api = {
251251
`/api/billing${query({ month, format })}`,
252252
{ credentials: "same-origin", headers: { "Accept-Language": acceptLanguageHeader() } });
253253
if (!response.ok) {
254-
let message = tr("导出失败(HTTP {0})", [response.status]);
254+
let message = tr(tr("导出失败(HTTP {0})"), [response.status]);
255255
try {
256256
const body = await response.json();
257257
if (body?.error?.message) message = body.error.message;
@@ -273,7 +273,7 @@ export const api = {
273273
`/api/credentials/${encodeURIComponent(id)}/export`,
274274
{ credentials: "same-origin", headers: { "Accept-Language": acceptLanguageHeader() } });
275275
if (!response.ok) {
276-
let message = tr("导出失败(HTTP {0})", [response.status]);
276+
let message = tr(tr("导出失败(HTTP {0})"), [response.status]);
277277
try {
278278
const body = await response.json();
279279
if (body?.error?.message) message = body.error.message;
@@ -307,7 +307,7 @@ export const api = {
307307
},
308308
);
309309
if (!response.ok) {
310-
let message = tr("恢复失败(HTTP {0})", [response.status]);
310+
let message = tr(tr("恢复失败(HTTP {0})"), [response.status]);
311311
try {
312312
const body = await response.json();
313313
if (body?.error?.message) message = body.error.message;
@@ -326,7 +326,7 @@ export const api = {
326326
headers: { "Accept-Language": acceptLanguageHeader() },
327327
});
328328
if (!response.ok) {
329-
throw new ApiError(response.status, "download_failed", tr("下载备份失败"));
329+
throw new ApiError(response.status, "download_failed", tr(tr("下载备份失败")));
330330
}
331331
return response.blob();
332332
},
@@ -531,7 +531,7 @@ export const api = {
531531
if (!response.ok) {
532532
// 与 request() 同口径地还原错误体,页面才能弹一致的中文提示
533533
let code = "error";
534-
let message = tr("导出失败(HTTP {0})", [response.status]);
534+
let message = tr(tr("导出失败(HTTP {0})"), [response.status]);
535535
try {
536536
const body = await response.json();
537537
if (body?.error) {

‎web/src/api/credentialTimeline.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ export interface CredentialTimelinePage {
3434
has_more: boolean;
3535
}
3636

37-
/** 事件枚举 → 中文标签。未知取值原样显示(后端加新事件时不至于空白)。 */
37+
/**
38+
* 事件枚举 → 中文源串(msgid)。表值只放中文,渲染时由 `timelineEventLabel`
39+
* 过 `tr`:模块级 `tr(...)` 会冻住加载瞬间的语言,切英文后仍渲染中文。
40+
* 未知取值原样显示(后端加新事件时不至于空白)。
41+
*/
3842
export const TIMELINE_EVENT_LABEL: Record<string, string> = {
3943
cooldown_start: "进入冷却",
4044
cooldown_end: "冷却结束",
@@ -96,7 +100,7 @@ export async function fetchCredentialTimeline(
96100
throw new ApiError(
97101
response.status,
98102
"invalid_response",
99-
tr("服务端返回了非 JSON 响应(可能是反向代理错误页),请检查服务是否正常。"),
103+
tr(tr("服务端返回了非 JSON 响应(可能是反向代理错误页),请检查服务是否正常。")),
100104
);
101105
}
102106
if (!response.ok) {

0 commit comments

Comments
 (0)