






















@@ -1,14 +1,27 @@
1-import { describe, expect, it } from "vitest";
2-import {
3-DEFAULT_LOCALE,
4-SUPPORTED_LOCALES,
5-loadLazyLocaleTranslation,
6-resolveNavigatorLocale,
7-} from "../../ui/src/i18n/lib/registry.ts";
8-import type { TranslationMap } from "../../ui/src/i18n/lib/types.ts";
1+import fs from "node:fs";
2+import { fileURLToPath } from "node:url";
3+import { beforeAll, describe, expect, it } from "vitest";
9410-function getNestedTranslation(map: TranslationMap | null, ...path: string[]): string | undefined {
11-let value: string | TranslationMap | undefined = map ?? undefined;
5+type TranslationTree = {
6+readonly [key: string]: string | TranslationTree | undefined;
7+};
8+9+type LocaleRegistry = {
10+DEFAULT_LOCALE: string;
11+SUPPORTED_LOCALES: readonly string[];
12+loadLazyLocaleTranslation(locale: string): Promise<TranslationTree | null>;
13+resolveNavigatorLocale(locale: string): string;
14+};
15+16+const registryModuleUrl = new URL("../../ui/src/i18n/lib/registry.ts", import.meta.url);
17+const describeWhenUiI18nPresent = fs.existsSync(fileURLToPath(registryModuleUrl))
18+ ? describe
19+ : describe.skip;
20+21+let registry: LocaleRegistry;
22+23+function getNestedTranslation(map: TranslationTree | null, ...path: string[]): string | undefined {
24+let value: string | TranslationTree | undefined = map ?? undefined;
1225for (const key of path) {
1326if (value === undefined || typeof value === "string") {
1427return undefined;
@@ -18,9 +31,13 @@ function getNestedTranslation(map: TranslationMap | null, ...path: string[]): st
1831return typeof value === "string" ? value : undefined;
1932}
203321-describe("ui i18n locale registry", () => {
34+describeWhenUiI18nPresent("ui i18n locale registry", () => {
35+beforeAll(async () => {
36+registry = (await import("../../ui/src/i18n/lib/registry.ts")) as LocaleRegistry;
37+});
38+2239it("lists supported locales", () => {
23-expect(SUPPORTED_LOCALES).toEqual([
40+expect(registry.SUPPORTED_LOCALES).toEqual([
2441"en",
2542"zh-CN",
2643"zh-TW",
@@ -36,39 +53,39 @@ describe("ui i18n locale registry", () => {
3653"pl",
3754"th",
3855]);
39-expect(DEFAULT_LOCALE).toBe("en");
56+expect(registry.DEFAULT_LOCALE).toBe("en");
4057});
41584259it("resolves browser locale fallbacks", () => {
43-expect(resolveNavigatorLocale("de-DE")).toBe("de");
44-expect(resolveNavigatorLocale("es-ES")).toBe("es");
45-expect(resolveNavigatorLocale("es-MX")).toBe("es");
46-expect(resolveNavigatorLocale("pt-PT")).toBe("pt-BR");
47-expect(resolveNavigatorLocale("zh-HK")).toBe("zh-TW");
48-expect(resolveNavigatorLocale("en-US")).toBe("en");
49-expect(resolveNavigatorLocale("ja-JP")).toBe("ja-JP");
50-expect(resolveNavigatorLocale("ko-KR")).toBe("ko");
51-expect(resolveNavigatorLocale("fr-CA")).toBe("fr");
52-expect(resolveNavigatorLocale("tr-TR")).toBe("tr");
53-expect(resolveNavigatorLocale("uk-UA")).toBe("uk");
54-expect(resolveNavigatorLocale("id-ID")).toBe("id");
55-expect(resolveNavigatorLocale("pl-PL")).toBe("pl");
56-expect(resolveNavigatorLocale("th-TH")).toBe("th");
60+expect(registry.resolveNavigatorLocale("de-DE")).toBe("de");
61+expect(registry.resolveNavigatorLocale("es-ES")).toBe("es");
62+expect(registry.resolveNavigatorLocale("es-MX")).toBe("es");
63+expect(registry.resolveNavigatorLocale("pt-PT")).toBe("pt-BR");
64+expect(registry.resolveNavigatorLocale("zh-HK")).toBe("zh-TW");
65+expect(registry.resolveNavigatorLocale("en-US")).toBe("en");
66+expect(registry.resolveNavigatorLocale("ja-JP")).toBe("ja-JP");
67+expect(registry.resolveNavigatorLocale("ko-KR")).toBe("ko");
68+expect(registry.resolveNavigatorLocale("fr-CA")).toBe("fr");
69+expect(registry.resolveNavigatorLocale("tr-TR")).toBe("tr");
70+expect(registry.resolveNavigatorLocale("uk-UA")).toBe("uk");
71+expect(registry.resolveNavigatorLocale("id-ID")).toBe("id");
72+expect(registry.resolveNavigatorLocale("pl-PL")).toBe("pl");
73+expect(registry.resolveNavigatorLocale("th-TH")).toBe("th");
5774});
58755976it("loads lazy locale translations from the registry", async () => {
60-const de = await loadLazyLocaleTranslation("de");
61-const es = await loadLazyLocaleTranslation("es");
62-const ptBR = await loadLazyLocaleTranslation("pt-BR");
63-const zhCN = await loadLazyLocaleTranslation("zh-CN");
64-const th = await loadLazyLocaleTranslation("th");
77+const de = await registry.loadLazyLocaleTranslation("de");
78+const es = await registry.loadLazyLocaleTranslation("es");
79+const ptBR = await registry.loadLazyLocaleTranslation("pt-BR");
80+const zhCN = await registry.loadLazyLocaleTranslation("zh-CN");
81+const th = await registry.loadLazyLocaleTranslation("th");
65826683expect(getNestedTranslation(de, "common", "health")).toBe("Status");
6784expect(getNestedTranslation(es, "common", "health")).toBe("Estado");
6885expect(getNestedTranslation(es, "languages", "de")).toBe("Deutsch (Alemán)");
6986expect(getNestedTranslation(ptBR, "languages", "es")).toBe("Español (Espanhol)");
7087expect(getNestedTranslation(zhCN, "common", "health")).toBe("\u5065\u5eb7\u72b6\u51b5");
7188expect(getNestedTranslation(th, "languages", "en")).toBeTruthy();
72-expect(await loadLazyLocaleTranslation("en")).toBeNull();
89+expect(await registry.loadLazyLocaleTranslation("en")).toBeNull();
7390});
7491});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。