Skip to content

Commit f92d50f

Browse files
committed
🐛 修复i18n前缀语言脚本名和描述展示 #1123
1 parent 6b5ed41 commit f92d50f

File tree

2 files changed

+151
-3
lines changed

2 files changed

+151
-3
lines changed

src/locales/locales.test.ts

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { describe, it, expect } from "vitest";
2+
import { i18nName, i18nDescription } from "./locales";
3+
import type { SCMetadata } from "@App/app/repo/scripts";
4+
import i18n from "i18next";
5+
6+
describe.concurrent("i18nName", () => {
7+
it("完全匹配语言时返回对应的国际化名称", () => {
8+
// 模拟当前语言为 zh-cn
9+
i18n.language = "zh-CN";
10+
11+
const script = {
12+
name: "Default Script Name",
13+
metadata: {
14+
"name:zh-cn": ["中文脚本名"],
15+
"name:en-us": ["English Script Name"],
16+
} as SCMetadata,
17+
};
18+
19+
const result = i18nName(script);
20+
expect(result).toBe("中文脚本名");
21+
});
22+
23+
it("前缀匹配语言时返回对应的国际化名称", () => {
24+
// 模拟当前语言为 zh-tw,但只有 zh 前缀的名称
25+
i18n.language = "zh-TW";
26+
27+
const script = {
28+
name: "Default Script Name",
29+
metadata: {
30+
"name:zh": ["中文脚本名"],
31+
"name:en": ["English Script Name"],
32+
} as SCMetadata,
33+
};
34+
35+
const result = i18nName(script);
36+
expect(result).toBe("中文脚本名");
37+
});
38+
39+
it("没有匹配的国际化名称时返回默认名称", () => {
40+
i18n.language = "ja-JP";
41+
42+
const script = {
43+
name: "Default Script Name",
44+
metadata: {
45+
"name:zh-cn": ["中文脚本名"],
46+
"name:en-us": ["English Script Name"],
47+
} as SCMetadata,
48+
};
49+
50+
const result = i18nName(script);
51+
expect(result).toBe("Default Script Name");
52+
});
53+
54+
it("metadata 为空时返回默认名称", () => {
55+
i18n.language = "en-US";
56+
57+
const script = {
58+
name: "Default Script Name",
59+
metadata: {} as SCMetadata,
60+
};
61+
62+
const result = i18nName(script);
63+
expect(result).toBe("Default Script Name");
64+
});
65+
});
66+
67+
describe.concurrent("i18nDescription", () => {
68+
it("完全匹配语言时返回对应的国际化描述", () => {
69+
i18n.language = "zh-CN";
70+
71+
const script = {
72+
metadata: {
73+
description: ["Default description"],
74+
"description:zh-cn": ["中文描述"],
75+
"description:en-us": ["English description"],
76+
} as SCMetadata,
77+
};
78+
79+
const result = i18nDescription(script);
80+
expect(result).toBe("中文描述");
81+
});
82+
83+
it("前缀匹配语言时返回对应的国际化描述", () => {
84+
i18n.language = "zh-TW";
85+
86+
const script = {
87+
metadata: {
88+
description: ["Default description"],
89+
"description:zh": ["中文描述"],
90+
"description:en": ["English description"],
91+
} as SCMetadata,
92+
};
93+
94+
const result = i18nDescription(script);
95+
expect(result).toBe("中文描述");
96+
});
97+
98+
it("没有匹配的国际化描述时返回默认描述", () => {
99+
i18n.language = "ja-JP";
100+
101+
const script = {
102+
metadata: {
103+
description: ["Default description"],
104+
"description:zh-cn": ["中文描述"],
105+
"description:en-us": ["English description"],
106+
} as SCMetadata,
107+
};
108+
109+
const result = i18nDescription(script);
110+
expect(result).toBe("Default description");
111+
});
112+
113+
it("没有 description 字段时返回 undefined", () => {
114+
i18n.language = "en-US";
115+
116+
const script = {
117+
metadata: {} as SCMetadata,
118+
};
119+
120+
const result = i18nDescription(script);
121+
expect(result).toBeUndefined();
122+
});
123+
124+
it("description 字段为空数组时返回 undefined", () => {
125+
i18n.language = "en-US";
126+
127+
const script = {
128+
metadata: {
129+
description: [],
130+
} as SCMetadata,
131+
};
132+
133+
const result = i18nDescription(script);
134+
expect(result).toBeUndefined();
135+
});
136+
});

src/locales/locales.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,25 @@ export function watchLanguageChange(callback: (lng: string) => void) {
101101
export const i18nLang = (): string => `${i18n?.language?.toLowerCase()}`;
102102

103103
export function i18nName(script: { name: string; metadata: SCMetadata }) {
104-
const m = script.metadata[`name:${i18nLang()}`];
104+
const lang = i18nLang();
105+
let m = script.metadata[`name:${lang}`];
106+
if (!m) {
107+
// 尝试只用前缀匹配
108+
const langPrefix = lang.split("-")[0];
109+
m = script.metadata[`name:${langPrefix}`];
110+
}
105111
return m ? m[0] : script.name;
106112
}
107113

108114
export function i18nDescription(script: { metadata: SCMetadata }) {
109-
const m = script.metadata[`description:${i18nLang()}`];
110-
return m ? m[0] : script.metadata.description;
115+
const lang = i18nLang();
116+
let m = script.metadata[`description:${lang}`];
117+
if (!m) {
118+
// 尝试只用前缀匹配
119+
const langPrefix = lang.split("-")[0];
120+
m = script.metadata[`description:${langPrefix}`];
121+
}
122+
return m ? m[0] : script.metadata.description?.[0];
111123
}
112124

113125
// 判断是否是中文用户

0 commit comments

Comments
 (0)