Repository navigation
Expand file tree
/
Copy pathi18n.test.ts
More file actions
102 lines (95 loc) · 4.05 KB
/
Copy pathi18n.test.ts
File metadata and controls
102 lines (95 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { expect, it } from "vitest";
import {
catalogs,
en,
htmlLang,
interpolate,
locales,
lookupTemplate,
matchLocale,
navigationTitle,
translate,
type MessageKey,
} from "@doca/i18n";
import { normalizePageStateValue } from "@core/modules/page-state.js";
import { builtinNavigation } from "../packages/web-plugin-registry/src/index.js";
it.each(locales)("keeps the %s catalog aligned with the English keys and placeholders", (locale) => {
const catalog = catalogs[locale];
const keys = Object.keys(en).sort() as Array<keyof typeof en>;
expect(Object.keys(catalog).sort()).toEqual(keys);
const tokens = (value: string) => (value.match(/\{\w+\}/g) ?? []).sort();
for (const key of keys) {
expect(catalog[key].trim(), key).not.toBe("");
expect(tokens(catalog[key]), key).toEqual(tokens(en[key]));
}
});
it("translates with English keys, placeholders, and an English fallback", () => {
expect(translate("zh", "nav.documents")).toBe("在线文档");
expect(translate("en", "account.expires", { date: "Jan 1" })).toBe("Expires Jan 1");
expect(interpolate("Hello {name}", {})).toBe("Hello {name}");
expect(lookupTemplate({ "files.count.one": "{count} file" }, {}, "files.count", { count: 1 })).toBe(
"{count} file",
);
expect(lookupTemplate({}, { "files.count.other": "{count} files" }, "files.count", { count: 2 })).toBe(
"{count} files",
);
expect(translate("zh", "common.failed")).toBe("操作失败");
});
it("accepts only supported interface locales", () => {
for (const locale of locales) {
expect(normalizePageStateValue("ui.locale", locale)).toBe(locale);
expect(matchLocale(htmlLang(locale))).toBe(locale);
}
for (const value of ["ar", "zh-TW", "fr-FR", "constructor", null, {}]) {
expect(() => normalizePageStateValue("ui.locale", value)).toThrow(/invalid_locale/);
}
});
it("recognizes regional interface tags without changing canonical write values", () => {
expect(["zh-TW", "zh-HK", "zh_mo", "zh-Hant-HK"].map(matchLocale))
.toEqual(["zh-Hant", "zh-Hant", "zh-Hant", "zh-Hant"]);
expect(["zh-CN", "zh-Hans-TW", "en-GB", "ja-JP", "fr-CA", "ko-KR", "es-MX", "ru-RU"].map(matchLocale))
.toEqual(["zh", "zh", "en", "ja", "fr", "ko", "es", "ru"]);
expect(matchLocale("ar-SA")).toBeNull();
});
it.each([
["zh-Hant", "搜尋"],
["ja", "検索"],
["fr", "Rechercher"],
["ko", "검색"],
["es", "Buscar"],
["ru", "Поиск"],
] as const)("translates a common control in %s", (locale, label) => {
expect(translate(locale, "common.search")).toBe(label);
});
it.each(locales)("localizes the built-in admin navigation in %s", (locale) => {
const keys: Record<string, MessageKey> = {
"doca.admin.overview": "admin.overview",
"doca.admin.login": "admin.login",
"doca.admin.users": "admin.users",
"doca.admin.registration": "admin.registration",
"doca.admin.access": "admin.access",
"doca.admin.ai": "admin.ai",
"doca.admin.mcp": "mcp.adminTitle",
"doca.admin.file-recognition": "admin.recognition",
"doca.admin.platform": "admin.platform",
"doca.admin.uploads": "admin.uploads",
"doca.admin.plugins": "plugins.title",
"doca.admin.navigation": "navigation.title",
"doca.admin.hooks": "admin.hooks",
};
const entries = builtinNavigation.filter((entry) => entry.defaults.includes("web.admin"));
expect(entries.map((entry) => entry.id).sort()).toEqual(Object.keys(keys).sort());
for (const entry of entries) {
const expected = locale === "zh" || locale === "en"
? entry.title[locale]
: translate(locale, keys[entry.id]!);
expect(navigationTitle(locale, entry), entry.id).toBe(expected);
}
});
it("respects explicit titles and bilingual plugin contributions in admin navigation", () => {
const title = { zh: "管理目录", en: "Administration directory" };
const users = builtinNavigation.find((entry) => entry.id === "doca.admin.users")!;
expect(navigationTitle("ko", users, title)).toBe(title.en);
expect(navigationTitle("zh", users, title)).toBe(title.zh);
expect(navigationTitle("ko", { id: "example.admin", title })).toBe(title.en);
});