@hua-labs/i18n-core 2.2.0 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,149 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+ import { Translator } from "../core/translator";
3
+ import { I18nConfig } from "../types";
4
+
5
+ function createConfig(overrides?: Partial<I18nConfig>): I18nConfig {
6
+ return {
7
+ defaultLanguage: "ko",
8
+ fallbackLanguage: "en",
9
+ supportedLanguages: [
10
+ { code: "ko", name: "Korean", nativeName: "한국어" },
11
+ { code: "en", name: "English", nativeName: "English" },
12
+ ],
13
+ namespaces: ["common"],
14
+ loadTranslations: vi
15
+ .fn()
16
+ .mockImplementation(async (lang: string, ns: string) => {
17
+ if (lang === "ko" && ns === "common") {
18
+ return { greeting: "안녕하세요", welcome: "{name}님 환영합니다" };
19
+ }
20
+ if (lang === "en" && ns === "common") {
21
+ return { greeting: "Hello", welcome: "Welcome {name}" };
22
+ }
23
+ return {};
24
+ }),
25
+ ...overrides,
26
+ };
27
+ }
28
+
29
+ describe("defaultValue support", () => {
30
+ let translator: Translator;
31
+
32
+ beforeEach(() => {
33
+ vi.useFakeTimers();
34
+ });
35
+
36
+ afterEach(() => {
37
+ vi.useRealTimers();
38
+ });
39
+
40
+ describe("Translator.translate()", () => {
41
+ beforeEach(async () => {
42
+ translator = new Translator(createConfig());
43
+ await translator.initialize();
44
+ vi.advanceTimersByTime(100);
45
+ });
46
+
47
+ it("returns translation when key exists (ignores defaultValue)", () => {
48
+ const result = translator.translate("common:greeting", {
49
+ defaultValue: "Fallback",
50
+ });
51
+ expect(result).toBe("안녕하세요");
52
+ });
53
+
54
+ it("returns defaultValue when key is missing", () => {
55
+ const result = translator.translate("common:nonexistent", {
56
+ defaultValue: "Fallback text",
57
+ });
58
+ expect(result).toBe("Fallback text");
59
+ });
60
+
61
+ it("interpolates variables in defaultValue", () => {
62
+ const result = translator.translate("common:missing", {
63
+ defaultValue: "Hello {{name}}",
64
+ name: "World",
65
+ });
66
+ expect(result).toBe("Hello World");
67
+ });
68
+
69
+ it("returns empty string when key is missing and no defaultValue (production)", () => {
70
+ const result = translator.translate("common:nonexistent");
71
+ expect(result).toBe("");
72
+ });
73
+
74
+ it("returns defaultValue before initialization", () => {
75
+ const fresh = new Translator(createConfig());
76
+ // Not initialized — should still return defaultValue
77
+ const result = fresh.translate("common:missing", {
78
+ defaultValue: "Hello {{name}}",
79
+ name: "World",
80
+ });
81
+ expect(result).toBe("Hello World");
82
+ });
83
+
84
+ it("returns key when missing and debug mode (no defaultValue)", () => {
85
+ const debugTranslator = new Translator(createConfig({ debug: true }));
86
+ // Use initialTranslations to bypass async init
87
+ const config = createConfig({
88
+ debug: true,
89
+ initialTranslations: { ko: { common: {} }, en: { common: {} } },
90
+ });
91
+ const t = new Translator(config);
92
+ const result = t.translate("common:nonexistent");
93
+ expect(result).toBe("common:nonexistent");
94
+ });
95
+ });
96
+
97
+ describe("Translator.translateAsync()", () => {
98
+ beforeEach(async () => {
99
+ translator = new Translator(createConfig());
100
+ await translator.initialize();
101
+ vi.advanceTimersByTime(100);
102
+ });
103
+
104
+ it("returns defaultValue when key is missing", async () => {
105
+ const result = await translator.translateAsync("common:missing", {
106
+ defaultValue: "Async fallback",
107
+ });
108
+ expect(result).toBe("Async fallback");
109
+ });
110
+
111
+ it("returns translation when key exists", async () => {
112
+ const result = await translator.translateAsync("common:greeting", {
113
+ defaultValue: "Fallback",
114
+ });
115
+ expect(result).toBe("안녕하세요");
116
+ });
117
+ });
118
+
119
+ describe("Translator.translateSync()", () => {
120
+ beforeEach(async () => {
121
+ translator = new Translator(createConfig());
122
+ await translator.initialize();
123
+ vi.advanceTimersByTime(100);
124
+ });
125
+
126
+ it("returns defaultValue when key is missing", () => {
127
+ const result = translator.translateSync("common:missing", {
128
+ defaultValue: "Sync fallback",
129
+ });
130
+ expect(result).toBe("Sync fallback");
131
+ });
132
+
133
+ it("returns translation when key exists", () => {
134
+ const result = translator.translateSync("common:greeting", {
135
+ defaultValue: "Fallback",
136
+ });
137
+ expect(result).toBe("안녕하세요");
138
+ });
139
+
140
+ it("returns defaultValue even before initialization", () => {
141
+ const fresh = new Translator(createConfig());
142
+ // Not initialized — should still return defaultValue
143
+ const result = fresh.translateSync("common:missing", {
144
+ defaultValue: "Before init",
145
+ });
146
+ expect(result).toBe("Before init");
147
+ });
148
+ });
149
+ });