auto-translate-json-library 2.0.2 → 2.1.0
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.
- package/README.MD +305 -3
- package/build/package.json +31 -16
- package/build/src/bin/cli.d.ts +1 -1
- package/build/src/bin/cli.js +253 -58
- package/build/src/config.d.ts +12 -1
- package/build/src/files.js +2 -2
- package/build/src/format-detector.js +1 -1
- package/build/src/huggingface-local.d.ts +11 -0
- package/build/src/huggingface-local.js +71 -0
- package/build/src/huggingface.d.ts +12 -0
- package/build/src/huggingface.js +76 -0
- package/build/src/index.js +16 -0
- package/build/src/lib.js +10 -26
- package/build/src/openai.js +1 -0
- package/build/src/provider-factory.d.ts +3 -0
- package/build/src/provider-factory.js +38 -0
- package/package.json +31 -16
- package/build/tests/arb-handler.test.d.ts +0 -1
- package/build/tests/arb-handler.test.js +0 -694
- package/build/tests/cli-integration.test.d.ts +0 -1
- package/build/tests/cli-integration.test.js +0 -352
- package/build/tests/csv-handler.test.d.ts +0 -1
- package/build/tests/csv-handler.test.js +0 -507
- package/build/tests/error-handling.test.d.ts +0 -1
- package/build/tests/error-handling.test.js +0 -456
- package/build/tests/files-integration.test.d.ts +0 -1
- package/build/tests/files-integration.test.js +0 -421
- package/build/tests/json-handler.test.d.ts +0 -1
- package/build/tests/json-handler.test.js +0 -526
- package/build/tests/po-handler.test.d.ts +0 -1
- package/build/tests/po-handler.test.js +0 -768
- package/build/tests/properties-handler.test.d.ts +0 -1
- package/build/tests/properties-handler.test.js +0 -316
- package/build/tests/setup.d.ts +0 -1
- package/build/tests/setup.js +0 -5
- package/build/tests/xliff-handler.test.d.ts +0 -1
- package/build/tests/xliff-handler.test.js +0 -459
- package/build/tests/xmb-xtb-handler.test.d.ts +0 -1
- package/build/tests/xmb-xtb-handler.test.js +0 -758
- package/build/tests/xml-handler.test.d.ts +0 -1
- package/build/tests/xml-handler.test.js +0 -570
- package/build/tests/yaml-handler.test.d.ts +0 -1
- package/build/tests/yaml-handler.test.js +0 -156
|
@@ -1,758 +0,0 @@
|
|
|
1
|
-
import { XmbHandler } from "../src/format/xmb-handler.js";
|
|
2
|
-
import { fileURLToPath } from 'url';
|
|
3
|
-
import { dirname } from 'path';
|
|
4
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
5
|
-
const __dirname = dirname(__filename);
|
|
6
|
-
import { XtbHandler } from "../src/format/xtb-handler.js";
|
|
7
|
-
import { XmbXtbUtils } from "../src/format/xmb-xtb-utils.js";
|
|
8
|
-
import * as fs from "node:fs";
|
|
9
|
-
import * as path from "node:path";
|
|
10
|
-
describe("XMB/XTB Handler Tests", () => {
|
|
11
|
-
let xmbHandler;
|
|
12
|
-
let xtbHandler;
|
|
13
|
-
let utils;
|
|
14
|
-
beforeEach(() => {
|
|
15
|
-
xmbHandler = new XmbHandler();
|
|
16
|
-
xtbHandler = new XtbHandler();
|
|
17
|
-
utils = new XmbXtbUtils();
|
|
18
|
-
});
|
|
19
|
-
describe("XMB Handler", () => {
|
|
20
|
-
const sampleXmbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
21
|
-
<messagebundle locale="en">
|
|
22
|
-
<msg id="HELLO_WORLD" desc="Greeting message">Hello, World!</msg>
|
|
23
|
-
<msg id="USER_GREETING" desc="Personalized greeting" meaning="greeting">Hello, {$username}!</msg>
|
|
24
|
-
<msg id="PLACEHOLDER_EXAMPLE" desc="Example with placeholders">
|
|
25
|
-
Welcome <ph name="USER_NAME">John</ph>, you have <ph name="COUNT">5</ph> messages.
|
|
26
|
-
</msg>
|
|
27
|
-
</messagebundle>`;
|
|
28
|
-
test("should handle XMB files correctly", () => {
|
|
29
|
-
expect(xmbHandler.canHandle("test.xmb")).toBe(true);
|
|
30
|
-
expect(xmbHandler.canHandle("test.xml")).toBe(false);
|
|
31
|
-
expect(xmbHandler.canHandle("test.xmb", sampleXmbContent)).toBe(true);
|
|
32
|
-
});
|
|
33
|
-
test("should parse XMB content correctly", () => {
|
|
34
|
-
const result = xmbHandler.parse(sampleXmbContent);
|
|
35
|
-
expect(result.HELLO_WORLD).toBe("Hello, World!");
|
|
36
|
-
expect(result.USER_GREETING).toBe("Hello, {$username}!");
|
|
37
|
-
expect(result.PLACEHOLDER_EXAMPLE).toContain("Welcome");
|
|
38
|
-
expect(result.PLACEHOLDER_EXAMPLE).toContain("John");
|
|
39
|
-
expect(result.PLACEHOLDER_EXAMPLE).toContain("5");
|
|
40
|
-
expect(result._metadata?.format).toBe("xmb");
|
|
41
|
-
expect(result._metadata?.xmbMetadata?.locale).toBe("en");
|
|
42
|
-
});
|
|
43
|
-
test("should serialize XMB content correctly", () => {
|
|
44
|
-
const parsed = xmbHandler.parse(sampleXmbContent);
|
|
45
|
-
const serialized = xmbHandler.serialize(parsed);
|
|
46
|
-
expect(serialized).toContain("<messagebundle");
|
|
47
|
-
expect(serialized).toContain("HELLO_WORLD");
|
|
48
|
-
expect(serialized).toContain("Hello, World!");
|
|
49
|
-
});
|
|
50
|
-
test("should validate XMB structure", () => {
|
|
51
|
-
const parsed = xmbHandler.parse(sampleXmbContent);
|
|
52
|
-
const validation = xmbHandler.validateStructure(parsed);
|
|
53
|
-
expect(validation.isValid).toBe(true);
|
|
54
|
-
expect(validation.errors).toHaveLength(0);
|
|
55
|
-
});
|
|
56
|
-
test("should generate XTB from XMB", () => {
|
|
57
|
-
const translations = {
|
|
58
|
-
HELLO_WORLD: "¡Hola, Mundo!",
|
|
59
|
-
USER_GREETING: "¡Hola, {$username}!",
|
|
60
|
-
PLACEHOLDER_EXAMPLE: "Bienvenido <ph name=\"USER_NAME\">Juan</ph>, tienes <ph name=\"COUNT\">5</ph> mensajes."
|
|
61
|
-
};
|
|
62
|
-
const xtbContent = xmbHandler.generateXtbFromXmb(sampleXmbContent, "es", translations);
|
|
63
|
-
expect(xtbContent).toContain("<translationbundle");
|
|
64
|
-
expect(xtbContent).toContain("lang=\"es\"");
|
|
65
|
-
expect(xtbContent).toContain("¡Hola, Mundo!");
|
|
66
|
-
expect(xtbContent).toContain("{$username}");
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
describe("XTB Handler", () => {
|
|
70
|
-
const sampleXtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
71
|
-
<translationbundle lang="es">
|
|
72
|
-
<translation id="HELLO_WORLD">¡Hola, Mundo!</translation>
|
|
73
|
-
<translation id="USER_GREETING">¡Hola, {$username}!</translation>
|
|
74
|
-
<translation id="PLACEHOLDER_EXAMPLE">
|
|
75
|
-
Bienvenido <ph name="USER_NAME">Juan</ph>, tienes <ph name="COUNT">5</ph> mensajes.
|
|
76
|
-
</translation>
|
|
77
|
-
</translationbundle>`;
|
|
78
|
-
test("should handle XTB files correctly", () => {
|
|
79
|
-
expect(xtbHandler.canHandle("test.xtb")).toBe(true);
|
|
80
|
-
expect(xtbHandler.canHandle("test.xml")).toBe(false);
|
|
81
|
-
expect(xtbHandler.canHandle("test.xtb", sampleXtbContent)).toBe(true);
|
|
82
|
-
});
|
|
83
|
-
test("should parse XTB content correctly", () => {
|
|
84
|
-
const result = xtbHandler.parse(sampleXtbContent);
|
|
85
|
-
expect(result.HELLO_WORLD).toBe("¡Hola, Mundo!");
|
|
86
|
-
expect(result.USER_GREETING).toBe("¡Hola, {$username}!");
|
|
87
|
-
expect(result.PLACEHOLDER_EXAMPLE).toContain("Bienvenido");
|
|
88
|
-
expect(result._metadata?.format).toBe("xtb");
|
|
89
|
-
expect(result._metadata?.xtbMetadata?.language).toBe("es");
|
|
90
|
-
});
|
|
91
|
-
test("should serialize XTB content correctly", () => {
|
|
92
|
-
const parsed = xtbHandler.parse(sampleXtbContent);
|
|
93
|
-
const serialized = xtbHandler.serialize(parsed);
|
|
94
|
-
expect(serialized).toContain("<translationbundle");
|
|
95
|
-
expect(serialized).toContain("lang=\"es\"");
|
|
96
|
-
expect(serialized).toContain("¡Hola, Mundo!");
|
|
97
|
-
});
|
|
98
|
-
test("should validate XTB structure", () => {
|
|
99
|
-
const parsed = xtbHandler.parse(sampleXtbContent);
|
|
100
|
-
const validation = xtbHandler.validateStructure(parsed);
|
|
101
|
-
expect(validation.isValid).toBe(true);
|
|
102
|
-
expect(validation.errors).toHaveLength(0);
|
|
103
|
-
});
|
|
104
|
-
test("should update XTB translations", () => {
|
|
105
|
-
const newTranslations = {
|
|
106
|
-
HELLO_WORLD: "¡Hola, Universo!",
|
|
107
|
-
NEW_MESSAGE: "Nuevo mensaje"
|
|
108
|
-
};
|
|
109
|
-
const updatedContent = xtbHandler.updateXtbTranslations(sampleXtbContent, newTranslations);
|
|
110
|
-
expect(updatedContent).toContain("¡Hola, Universo!");
|
|
111
|
-
expect(updatedContent).toContain("NEW_MESSAGE");
|
|
112
|
-
expect(updatedContent).toContain("Nuevo mensaje");
|
|
113
|
-
});
|
|
114
|
-
});
|
|
115
|
-
describe("XMB/XTB Utils", () => {
|
|
116
|
-
const sampleXmbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
117
|
-
<messagebundle locale="en">
|
|
118
|
-
<msg id="HELLO_WORLD" desc="Greeting message">Hello, World!</msg>
|
|
119
|
-
<msg id="USER_GREETING" desc="Personalized greeting">Hello, {$username}!</msg>
|
|
120
|
-
</messagebundle>`;
|
|
121
|
-
const sampleXtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
122
|
-
<translationbundle lang="es">
|
|
123
|
-
<translation id="HELLO_WORLD">¡Hola, Mundo!</translation>
|
|
124
|
-
<translation id="USER_GREETING">¡Hola, {$username}!</translation>
|
|
125
|
-
</translationbundle>`;
|
|
126
|
-
test("should validate bundle integrity", () => {
|
|
127
|
-
const validation = utils.validateBundleIntegrity(sampleXmbContent, sampleXtbContent);
|
|
128
|
-
expect(validation.isValid).toBe(true);
|
|
129
|
-
expect(validation.errors).toHaveLength(0);
|
|
130
|
-
});
|
|
131
|
-
test("should extract translator context", () => {
|
|
132
|
-
const context = utils.extractTranslatorContext(sampleXmbContent);
|
|
133
|
-
expect(context.HELLO_WORLD).toBeDefined();
|
|
134
|
-
expect(context.HELLO_WORLD.description).toBe("Greeting message");
|
|
135
|
-
expect(context.HELLO_WORLD.originalText).toBe("Hello, World!");
|
|
136
|
-
expect(context.USER_GREETING).toBeDefined();
|
|
137
|
-
expect(context.USER_GREETING.description).toBe("Personalized greeting");
|
|
138
|
-
expect(context.USER_GREETING.placeholders.variables).toContain("{$username}");
|
|
139
|
-
});
|
|
140
|
-
test("should validate message references", () => {
|
|
141
|
-
const validation = utils.validateMessageReferences(sampleXmbContent, sampleXtbContent);
|
|
142
|
-
expect(validation.isValid).toBe(true);
|
|
143
|
-
expect(validation.errors).toHaveLength(0);
|
|
144
|
-
});
|
|
145
|
-
test("should generate XTB from XMB using utils", () => {
|
|
146
|
-
const translations = {
|
|
147
|
-
HELLO_WORLD: "Bonjour le monde!",
|
|
148
|
-
USER_GREETING: "Bonjour, {$username}!"
|
|
149
|
-
};
|
|
150
|
-
const xtbContent = utils.generateXtbFromXmb(sampleXmbContent, "fr", translations);
|
|
151
|
-
expect(xtbContent).toContain("<translationbundle");
|
|
152
|
-
expect(xtbContent).toContain("lang=\"fr\"");
|
|
153
|
-
expect(xtbContent).toContain("Bonjour le monde!");
|
|
154
|
-
expect(xtbContent).toContain("{$username}");
|
|
155
|
-
});
|
|
156
|
-
test("should update XTB file using utils", () => {
|
|
157
|
-
const newTranslations = {
|
|
158
|
-
HELLO_WORLD: "¡Hola, Universo!",
|
|
159
|
-
NEW_MESSAGE: "Nuevo mensaje"
|
|
160
|
-
};
|
|
161
|
-
const updatedContent = utils.updateXtbFile(sampleXtbContent, newTranslations);
|
|
162
|
-
expect(updatedContent).toContain("¡Hola, Universo!");
|
|
163
|
-
expect(updatedContent).toContain("NEW_MESSAGE");
|
|
164
|
-
expect(updatedContent).toContain("Nuevo mensaje");
|
|
165
|
-
});
|
|
166
|
-
});
|
|
167
|
-
describe("Placeholder Validation", () => {
|
|
168
|
-
test("should validate placeholder integrity", () => {
|
|
169
|
-
const originalText = "Hello, {$username}! You have <ph name=\"COUNT\">5</ph> messages.";
|
|
170
|
-
const translatedText = "¡Hola, {$username}! Tienes <ph name=\"COUNT\">5</ph> mensajes.";
|
|
171
|
-
const validation = xmbHandler.validateMessageIntegrity(originalText, translatedText);
|
|
172
|
-
expect(validation.isValid).toBe(true);
|
|
173
|
-
expect(validation.errors).toHaveLength(0);
|
|
174
|
-
});
|
|
175
|
-
test("should detect missing placeholders", () => {
|
|
176
|
-
const originalText = "Hello, {$username}! You have <ph name=\"COUNT\">5</ph> messages.";
|
|
177
|
-
const translatedText = "¡Hola! Tienes mensajes."; // Missing placeholders
|
|
178
|
-
const validation = xmbHandler.validateMessageIntegrity(originalText, translatedText);
|
|
179
|
-
expect(validation.isValid).toBe(false);
|
|
180
|
-
expect(validation.errors.length).toBeGreaterThan(0);
|
|
181
|
-
expect(validation.errors.some(e => e.code === "MISSING_VARIABLE_PLACEHOLDER")).toBe(true);
|
|
182
|
-
expect(validation.errors.some(e => e.code === "MISSING_PH_PLACEHOLDER")).toBe(true);
|
|
183
|
-
});
|
|
184
|
-
test("should detect unmatched braces", () => {
|
|
185
|
-
const textWithUnmatchedBraces = "Hello, {$username! Missing closing brace.";
|
|
186
|
-
const validation = xmbHandler.validateStructure({
|
|
187
|
-
TEST_MESSAGE: textWithUnmatchedBraces,
|
|
188
|
-
_metadata: {
|
|
189
|
-
format: "xmb",
|
|
190
|
-
originalStructure: {
|
|
191
|
-
messagebundle: {
|
|
192
|
-
msg: {
|
|
193
|
-
"@_id": "TEST_MESSAGE",
|
|
194
|
-
"#text": textWithUnmatchedBraces
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
});
|
|
200
|
-
expect(validation.isValid).toBe(false);
|
|
201
|
-
expect(validation.errors.some(e => e.code === "UNMATCHED_PLACEHOLDER_BRACES")).toBe(true);
|
|
202
|
-
});
|
|
203
|
-
test("should detect unmatched ph tags", () => {
|
|
204
|
-
const textWithUnmatchedPh = "Welcome <ph name=\"USER\">John, you have messages.";
|
|
205
|
-
const validation = xmbHandler.validateStructure({
|
|
206
|
-
TEST_MESSAGE: textWithUnmatchedPh,
|
|
207
|
-
_metadata: {
|
|
208
|
-
format: "xmb",
|
|
209
|
-
originalStructure: {
|
|
210
|
-
messagebundle: {
|
|
211
|
-
msg: {
|
|
212
|
-
"@_id": "TEST_MESSAGE",
|
|
213
|
-
"#text": textWithUnmatchedPh
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
expect(validation.isValid).toBe(false);
|
|
220
|
-
expect(validation.errors.some(e => e.code === "UNMATCHED_PH_TAGS")).toBe(true);
|
|
221
|
-
});
|
|
222
|
-
test("should detect empty placeholder names", () => {
|
|
223
|
-
const textWithEmptyPhName = "Hello <ph name=\"\">World</ph>!";
|
|
224
|
-
const validation = xmbHandler.validateStructure({
|
|
225
|
-
TEST_MESSAGE: textWithEmptyPhName,
|
|
226
|
-
_metadata: {
|
|
227
|
-
format: "xmb",
|
|
228
|
-
originalStructure: {
|
|
229
|
-
messagebundle: {
|
|
230
|
-
msg: {
|
|
231
|
-
"@_id": "TEST_MESSAGE",
|
|
232
|
-
"#text": textWithEmptyPhName
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
});
|
|
238
|
-
expect(validation.isValid).toBe(false);
|
|
239
|
-
expect(validation.errors.some(e => e.code === "EMPTY_PLACEHOLDER_NAME")).toBe(true);
|
|
240
|
-
});
|
|
241
|
-
test("should validate complex nested placeholders", () => {
|
|
242
|
-
const originalText = "Status: <ph name=\"STATUS\"><ex>Active</ex></ph> for user {$userId} in <ph name=\"REGION\">US</ph>.";
|
|
243
|
-
const translatedText = "Estado: <ph name=\"STATUS\"><ex>Activo</ex></ph> para usuario {$userId} en <ph name=\"REGION\">US</ph>.";
|
|
244
|
-
const validation = xmbHandler.validateMessageIntegrity(originalText, translatedText);
|
|
245
|
-
expect(validation.isValid).toBe(true);
|
|
246
|
-
expect(validation.errors).toHaveLength(0);
|
|
247
|
-
});
|
|
248
|
-
test("should detect unmatched example tags", () => {
|
|
249
|
-
const textWithUnmatchedEx = "Your order will arrive on <ex>Monday.";
|
|
250
|
-
const validation = xmbHandler.validateStructure({
|
|
251
|
-
TEST_MESSAGE: textWithUnmatchedEx,
|
|
252
|
-
_metadata: {
|
|
253
|
-
format: "xmb",
|
|
254
|
-
originalStructure: {
|
|
255
|
-
messagebundle: {
|
|
256
|
-
msg: {
|
|
257
|
-
"@_id": "TEST_MESSAGE",
|
|
258
|
-
"#text": textWithUnmatchedEx
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
expect(validation.isValid).toBe(false);
|
|
265
|
-
expect(validation.errors.some(e => e.code === "UNMATCHED_EX_TAGS")).toBe(true);
|
|
266
|
-
});
|
|
267
|
-
test("should warn about extra placeholders in translation", () => {
|
|
268
|
-
const originalText = "Hello, {$username}!";
|
|
269
|
-
const translatedText = "¡Hola, {$username}! Extra: {$extraVar}";
|
|
270
|
-
const validation = xmbHandler.validateMessageIntegrity(originalText, translatedText);
|
|
271
|
-
expect(validation.isValid).toBe(true);
|
|
272
|
-
expect(validation.warnings.some(w => w.code === "EXTRA_VARIABLE_PLACEHOLDER")).toBe(true);
|
|
273
|
-
});
|
|
274
|
-
test("should validate XTB placeholder integrity", () => {
|
|
275
|
-
const originalText = "Hello, {$username}! You have <ph name=\"COUNT\">5</ph> messages.";
|
|
276
|
-
const translatedText = "¡Hola, {$username}! Tienes <ph name=\"COUNT\">5</ph> mensajes.";
|
|
277
|
-
const validation = xtbHandler.validateTranslationIntegrity(originalText, translatedText);
|
|
278
|
-
expect(validation.isValid).toBe(true);
|
|
279
|
-
expect(validation.errors).toHaveLength(0);
|
|
280
|
-
});
|
|
281
|
-
test("should detect missing placeholders in XTB", () => {
|
|
282
|
-
const originalText = "Hello, {$username}! You have <ph name=\"COUNT\">5</ph> messages.";
|
|
283
|
-
const translatedText = "¡Hola! Tienes mensajes."; // Missing placeholders
|
|
284
|
-
const validation = xtbHandler.validateTranslationIntegrity(originalText, translatedText);
|
|
285
|
-
expect(validation.isValid).toBe(false);
|
|
286
|
-
expect(validation.errors.some(e => e.code === "MISSING_VARIABLE_PLACEHOLDER")).toBe(true);
|
|
287
|
-
expect(validation.errors.some(e => e.code === "MISSING_PH_PLACEHOLDER")).toBe(true);
|
|
288
|
-
});
|
|
289
|
-
test("should validate XTB structure with placeholder errors", () => {
|
|
290
|
-
const textWithUnmatchedBraces = "Hello, {$username! Missing closing brace.";
|
|
291
|
-
const validation = xtbHandler.validateStructure({
|
|
292
|
-
TEST_MESSAGE: textWithUnmatchedBraces,
|
|
293
|
-
_metadata: {
|
|
294
|
-
format: "xtb",
|
|
295
|
-
originalStructure: {
|
|
296
|
-
translationbundle: {
|
|
297
|
-
translation: {
|
|
298
|
-
"@_id": "TEST_MESSAGE",
|
|
299
|
-
"#text": textWithUnmatchedBraces
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
});
|
|
305
|
-
expect(validation.isValid).toBe(false);
|
|
306
|
-
expect(validation.errors.some(e => e.code === "UNMATCHED_PLACEHOLDER_BRACES")).toBe(true);
|
|
307
|
-
});
|
|
308
|
-
});
|
|
309
|
-
describe("Mixed Content and Advanced Features", () => {
|
|
310
|
-
test("should handle XMB messages with mixed content (text + elements)", () => {
|
|
311
|
-
const mixedContentXmb = `<?xml version="1.0" encoding="UTF-8"?>
|
|
312
|
-
<messagebundle locale="en">
|
|
313
|
-
<msg id="MIXED_CONTENT">
|
|
314
|
-
Welcome <ph name="USER_NAME">John</ph>, you have <ph name="COUNT">5</ph> messages.
|
|
315
|
-
</msg>
|
|
316
|
-
</messagebundle>`;
|
|
317
|
-
const result = xmbHandler.parse(mixedContentXmb);
|
|
318
|
-
expect(result.MIXED_CONTENT).toContain("Welcome");
|
|
319
|
-
expect(result.MIXED_CONTENT).toContain('<ph name="USER_NAME">John</ph>');
|
|
320
|
-
expect(result.MIXED_CONTENT).toContain('<ph name="COUNT">5</ph>');
|
|
321
|
-
expect(result.MIXED_CONTENT).toContain("messages");
|
|
322
|
-
});
|
|
323
|
-
test("should handle XTB translations with mixed content", () => {
|
|
324
|
-
const mixedContentXtb = `<?xml version="1.0" encoding="UTF-8"?>
|
|
325
|
-
<translationbundle lang="es">
|
|
326
|
-
<translation id="MIXED_CONTENT">
|
|
327
|
-
Bienvenido <ph name="USER_NAME">Juan</ph>, tienes <ph name="COUNT">5</ph> mensajes.
|
|
328
|
-
</translation>
|
|
329
|
-
</translationbundle>`;
|
|
330
|
-
const result = xtbHandler.parse(mixedContentXtb);
|
|
331
|
-
expect(result.MIXED_CONTENT).toContain("Bienvenido");
|
|
332
|
-
expect(result.MIXED_CONTENT).toContain('<ph name="USER_NAME">Juan</ph>');
|
|
333
|
-
expect(result.MIXED_CONTENT).toContain('<ph name="COUNT">5</ph>');
|
|
334
|
-
expect(result.MIXED_CONTENT).toContain("mensajes");
|
|
335
|
-
});
|
|
336
|
-
test("should handle XMB messages with example elements", () => {
|
|
337
|
-
const xmbWithExamples = `<?xml version="1.0" encoding="UTF-8"?>
|
|
338
|
-
<messagebundle locale="en">
|
|
339
|
-
<msg id="WITH_EXAMPLES">
|
|
340
|
-
Your order will arrive on <ex>Monday</ex>.
|
|
341
|
-
</msg>
|
|
342
|
-
</messagebundle>`;
|
|
343
|
-
const result = xmbHandler.parse(xmbWithExamples);
|
|
344
|
-
expect(result.WITH_EXAMPLES).toContain("Your order will arrive on");
|
|
345
|
-
expect(result.WITH_EXAMPLES).toContain("<ex>Monday</ex>");
|
|
346
|
-
});
|
|
347
|
-
test("should handle XTB translations with example elements", () => {
|
|
348
|
-
const xtbWithExamples = `<?xml version="1.0" encoding="UTF-8"?>
|
|
349
|
-
<translationbundle lang="es">
|
|
350
|
-
<translation id="WITH_EXAMPLES">
|
|
351
|
-
Tu pedido llegará el <ex>lunes</ex>.
|
|
352
|
-
</translation>
|
|
353
|
-
</translationbundle>`;
|
|
354
|
-
const result = xtbHandler.parse(xtbWithExamples);
|
|
355
|
-
expect(result.WITH_EXAMPLES).toContain("Tu pedido llegará el");
|
|
356
|
-
expect(result.WITH_EXAMPLES).toContain("<ex>lunes</ex>");
|
|
357
|
-
});
|
|
358
|
-
test("should handle XMB messages with nested placeholder structures", () => {
|
|
359
|
-
const nestedPlaceholderXmb = `<?xml version="1.0" encoding="UTF-8"?>
|
|
360
|
-
<messagebundle locale="en">
|
|
361
|
-
<msg id="NESTED_PH">
|
|
362
|
-
Status: <ph name="STATUS"><ex>Active</ex></ph> for user {$userId}.
|
|
363
|
-
</msg>
|
|
364
|
-
</messagebundle>`;
|
|
365
|
-
const result = xmbHandler.parse(nestedPlaceholderXmb);
|
|
366
|
-
expect(result.NESTED_PH).toContain("Status:");
|
|
367
|
-
expect(result.NESTED_PH).toContain("STATUS");
|
|
368
|
-
expect(result.NESTED_PH).toContain("{$userId}");
|
|
369
|
-
// The exact structure may vary based on how mixed content is parsed
|
|
370
|
-
expect(result.NESTED_PH).toContain("for user");
|
|
371
|
-
});
|
|
372
|
-
test("should handle string-type messages in XMB", () => {
|
|
373
|
-
// Test the case where a message is parsed as a string rather than an object
|
|
374
|
-
const result = xmbHandler.parse(`<?xml version="1.0" encoding="UTF-8"?>
|
|
375
|
-
<messagebundle locale="en">
|
|
376
|
-
<msg id="STRING_MESSAGE">Simple string message</msg>
|
|
377
|
-
</messagebundle>`);
|
|
378
|
-
expect(result.STRING_MESSAGE).toBe("Simple string message");
|
|
379
|
-
});
|
|
380
|
-
test("should handle string-type translations in XTB", () => {
|
|
381
|
-
// Test the case where a translation is parsed as a string rather than an object
|
|
382
|
-
const result = xtbHandler.parse(`<?xml version="1.0" encoding="UTF-8"?>
|
|
383
|
-
<translationbundle lang="es">
|
|
384
|
-
<translation id="STRING_TRANSLATION">Mensaje de cadena simple</translation>
|
|
385
|
-
</translationbundle>`);
|
|
386
|
-
expect(result.STRING_TRANSLATION).toBe("Mensaje de cadena simple");
|
|
387
|
-
});
|
|
388
|
-
test("should handle XMB messages without #text property", () => {
|
|
389
|
-
// Test fallback to mixed content extraction when #text is not available
|
|
390
|
-
const xmbHandler = new XmbHandler();
|
|
391
|
-
const mockElement = {
|
|
392
|
-
"@_id": "TEST_MESSAGE",
|
|
393
|
-
ph: {
|
|
394
|
-
"@_name": "USER_NAME",
|
|
395
|
-
"#text": "John"
|
|
396
|
-
}
|
|
397
|
-
};
|
|
398
|
-
// This tests the extractMixedContent fallback path
|
|
399
|
-
const result = xmbHandler.parse(`<?xml version="1.0" encoding="UTF-8"?>
|
|
400
|
-
<messagebundle locale="en">
|
|
401
|
-
<msg id="FALLBACK_MESSAGE">
|
|
402
|
-
<ph name="USER_NAME">John</ph>
|
|
403
|
-
</msg>
|
|
404
|
-
</messagebundle>`);
|
|
405
|
-
expect(result.FALLBACK_MESSAGE).toContain("John");
|
|
406
|
-
});
|
|
407
|
-
test("should handle XTB translations without #text property", () => {
|
|
408
|
-
// Test fallback to mixed content extraction when #text is not available
|
|
409
|
-
const result = xtbHandler.parse(`<?xml version="1.0" encoding="UTF-8"?>
|
|
410
|
-
<translationbundle lang="es">
|
|
411
|
-
<translation id="FALLBACK_TRANSLATION">
|
|
412
|
-
<ph name="USER_NAME">Juan</ph>
|
|
413
|
-
</translation>
|
|
414
|
-
</translationbundle>`);
|
|
415
|
-
expect(result.FALLBACK_TRANSLATION).toContain("Juan");
|
|
416
|
-
});
|
|
417
|
-
test("should handle XMB with version attribute", () => {
|
|
418
|
-
const xmbWithVersion = `<?xml version="1.0" encoding="UTF-8"?>
|
|
419
|
-
<messagebundle locale="en" version="2.0">
|
|
420
|
-
<msg id="VERSIONED_MESSAGE">Hello with version</msg>
|
|
421
|
-
</messagebundle>`;
|
|
422
|
-
const result = xmbHandler.parse(xmbWithVersion);
|
|
423
|
-
expect(result._metadata?.xmbMetadata?.version).toBe("2.0");
|
|
424
|
-
expect(result.VERSIONED_MESSAGE).toBe("Hello with version");
|
|
425
|
-
});
|
|
426
|
-
test("should handle XTB with version attribute", () => {
|
|
427
|
-
const xtbWithVersion = `<?xml version="1.0" encoding="UTF-8"?>
|
|
428
|
-
<translationbundle lang="es" version="1.5">
|
|
429
|
-
<translation id="VERSIONED_TRANSLATION">Hola con versión</translation>
|
|
430
|
-
</translationbundle>`;
|
|
431
|
-
const result = xtbHandler.parse(xtbWithVersion);
|
|
432
|
-
expect(result._metadata?.xtbMetadata?.version).toBe("1.5");
|
|
433
|
-
expect(result.VERSIONED_TRANSLATION).toBe("Hola con versión");
|
|
434
|
-
});
|
|
435
|
-
test("should handle XTB update with empty existing translations", () => {
|
|
436
|
-
const emptyXtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
437
|
-
<translationbundle lang="es">
|
|
438
|
-
</translationbundle>`;
|
|
439
|
-
const newTranslations = {
|
|
440
|
-
NEW_MESSAGE_1: "Primer mensaje nuevo",
|
|
441
|
-
NEW_MESSAGE_2: "Segundo mensaje nuevo"
|
|
442
|
-
};
|
|
443
|
-
const updatedContent = xtbHandler.updateXtbTranslations(emptyXtbContent, newTranslations);
|
|
444
|
-
expect(updatedContent).toContain("NEW_MESSAGE_1");
|
|
445
|
-
expect(updatedContent).toContain("Primer mensaje nuevo");
|
|
446
|
-
expect(updatedContent).toContain("NEW_MESSAGE_2");
|
|
447
|
-
expect(updatedContent).toContain("Segundo mensaje nuevo");
|
|
448
|
-
});
|
|
449
|
-
});
|
|
450
|
-
describe("Real File Testing", () => {
|
|
451
|
-
const testDataPath = path.join(__dirname, "test-data");
|
|
452
|
-
test("should parse real XMB file correctly", () => {
|
|
453
|
-
const xmbContent = fs.readFileSync(path.join(testDataPath, "sample.xmb"), "utf-8");
|
|
454
|
-
const result = xmbHandler.parse(xmbContent);
|
|
455
|
-
expect(result.HELLO_WORLD).toBe("Hello, World!");
|
|
456
|
-
expect(result.USER_GREETING).toBe("Hello, {$username}!");
|
|
457
|
-
expect(result.PLACEHOLDER_EXAMPLE).toContain("Welcome");
|
|
458
|
-
expect(result.PLACEHOLDER_EXAMPLE).toContain("<ph name=\"USER_NAME\">John</ph>");
|
|
459
|
-
expect(result.PLACEHOLDER_EXAMPLE).toContain("<ph name=\"COUNT\">5</ph>");
|
|
460
|
-
expect(result.COMPLEX_MESSAGE).toContain("{$itemCount}");
|
|
461
|
-
expect(result.SIMPLE_TEXT).toBe("This is a simple message.");
|
|
462
|
-
expect(result._metadata?.format).toBe("xmb");
|
|
463
|
-
expect(result._metadata?.xmbMetadata?.locale).toBe("en");
|
|
464
|
-
});
|
|
465
|
-
test("should parse real XTB file correctly", () => {
|
|
466
|
-
const xtbContent = fs.readFileSync(path.join(testDataPath, "sample-es.xtb"), "utf-8");
|
|
467
|
-
const result = xtbHandler.parse(xtbContent);
|
|
468
|
-
expect(result.HELLO_WORLD).toBe("¡Hola, Mundo!");
|
|
469
|
-
expect(result.USER_GREETING).toBe("¡Hola, {$username}!");
|
|
470
|
-
expect(result.PLACEHOLDER_EXAMPLE).toContain("Bienvenido");
|
|
471
|
-
expect(result.PLACEHOLDER_EXAMPLE).toContain("<ph name=\"USER_NAME\">Juan</ph>");
|
|
472
|
-
expect(result.COMPLEX_MESSAGE).toContain("{$itemCount}");
|
|
473
|
-
expect(result.SIMPLE_TEXT).toBe("Este es un mensaje simple.");
|
|
474
|
-
expect(result._metadata?.format).toBe("xtb");
|
|
475
|
-
expect(result._metadata?.xtbMetadata?.language).toBe("es");
|
|
476
|
-
});
|
|
477
|
-
test("should validate bundle integrity with real files", () => {
|
|
478
|
-
const xmbContent = fs.readFileSync(path.join(testDataPath, "sample.xmb"), "utf-8");
|
|
479
|
-
const xtbContent = fs.readFileSync(path.join(testDataPath, "sample-es.xtb"), "utf-8");
|
|
480
|
-
const validation = utils.validateBundleIntegrity(xmbContent, xtbContent);
|
|
481
|
-
expect(validation.isValid).toBe(true);
|
|
482
|
-
expect(validation.errors).toHaveLength(0);
|
|
483
|
-
});
|
|
484
|
-
test("should handle malformed XMB files gracefully", () => {
|
|
485
|
-
const malformedXmbContent = fs.readFileSync(path.join(testDataPath, "malformed.xmb"), "utf-8");
|
|
486
|
-
const result = xmbHandler.parse(malformedXmbContent);
|
|
487
|
-
const validation = xmbHandler.validateStructure(result);
|
|
488
|
-
expect(validation.isValid).toBe(false);
|
|
489
|
-
expect(validation.errors.length).toBeGreaterThan(0);
|
|
490
|
-
expect(validation.errors.some(e => e.code === "UNMATCHED_PLACEHOLDER_BRACES")).toBe(true);
|
|
491
|
-
expect(validation.errors.some(e => e.code === "MISSING_MESSAGE_ID")).toBe(true);
|
|
492
|
-
// The empty placeholder name should be detected in the original structure
|
|
493
|
-
// Let's verify the structure contains the empty name
|
|
494
|
-
const originalStructure = result._metadata?.originalStructure;
|
|
495
|
-
expect(originalStructure).toBeDefined();
|
|
496
|
-
expect(originalStructure.messagebundle.msg.some((msg) => msg.ph && msg.ph["@_name"] === "")).toBe(true);
|
|
497
|
-
});
|
|
498
|
-
test("should handle malformed XTB files gracefully", () => {
|
|
499
|
-
const malformedXtbContent = fs.readFileSync(path.join(testDataPath, "malformed.xtb"), "utf-8");
|
|
500
|
-
const result = xtbHandler.parse(malformedXtbContent);
|
|
501
|
-
const validation = xtbHandler.validateStructure(result);
|
|
502
|
-
expect(validation.isValid).toBe(false);
|
|
503
|
-
expect(validation.errors.length).toBeGreaterThan(0);
|
|
504
|
-
expect(validation.errors.some(e => e.code === "UNMATCHED_PLACEHOLDER_BRACES")).toBe(true);
|
|
505
|
-
expect(validation.errors.some(e => e.code === "MISSING_TRANSLATION_ID")).toBe(true);
|
|
506
|
-
});
|
|
507
|
-
});
|
|
508
|
-
describe("Error Handling and Edge Cases", () => {
|
|
509
|
-
test("should handle empty XMB files", () => {
|
|
510
|
-
const emptyXmbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
511
|
-
<messagebundle locale="en">
|
|
512
|
-
</messagebundle>`;
|
|
513
|
-
const result = xmbHandler.parse(emptyXmbContent);
|
|
514
|
-
const validation = xmbHandler.validateStructure(result);
|
|
515
|
-
expect(validation.isValid).toBe(true);
|
|
516
|
-
expect(validation.warnings.some(w => w.code === "NO_MESSAGES")).toBe(true);
|
|
517
|
-
});
|
|
518
|
-
test("should handle empty XTB files", () => {
|
|
519
|
-
const emptyXtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
520
|
-
<translationbundle lang="es">
|
|
521
|
-
</translationbundle>`;
|
|
522
|
-
const result = xtbHandler.parse(emptyXtbContent);
|
|
523
|
-
const validation = xtbHandler.validateStructure(result);
|
|
524
|
-
expect(validation.isValid).toBe(true);
|
|
525
|
-
expect(validation.warnings.some(w => w.code === "NO_TRANSLATIONS")).toBe(true);
|
|
526
|
-
});
|
|
527
|
-
test("should handle invalid XML structure", () => {
|
|
528
|
-
const invalidXml = "This is not valid XML";
|
|
529
|
-
expect(() => xmbHandler.parse(invalidXml)).toThrow("Failed to parse XMB");
|
|
530
|
-
expect(() => xtbHandler.parse(invalidXml)).toThrow("Failed to parse XTB");
|
|
531
|
-
});
|
|
532
|
-
test("should handle missing root elements", () => {
|
|
533
|
-
const missingRootXmb = `<?xml version="1.0" encoding="UTF-8"?>
|
|
534
|
-
<wrongroot>
|
|
535
|
-
<msg id="TEST">Test</msg>
|
|
536
|
-
</wrongroot>`;
|
|
537
|
-
const missingRootXtb = `<?xml version="1.0" encoding="UTF-8"?>
|
|
538
|
-
<wrongroot>
|
|
539
|
-
<translation id="TEST">Test</translation>
|
|
540
|
-
</wrongroot>`;
|
|
541
|
-
expect(() => xmbHandler.parse(missingRootXmb)).toThrow("missing messagebundle root element");
|
|
542
|
-
expect(() => xtbHandler.parse(missingRootXtb)).toThrow("missing translationbundle root element");
|
|
543
|
-
});
|
|
544
|
-
test("should handle XTB without language attribute", () => {
|
|
545
|
-
const xtbWithoutLang = `<?xml version="1.0" encoding="UTF-8"?>
|
|
546
|
-
<translationbundle>
|
|
547
|
-
<translation id="TEST">Test</translation>
|
|
548
|
-
</translationbundle>`;
|
|
549
|
-
const result = xtbHandler.parse(xtbWithoutLang);
|
|
550
|
-
const validation = xtbHandler.validateStructure(result);
|
|
551
|
-
expect(validation.isValid).toBe(false);
|
|
552
|
-
expect(validation.errors.some(e => e.code === "MISSING_LANGUAGE")).toBe(true);
|
|
553
|
-
});
|
|
554
|
-
test("should preserve message descriptions and meanings", () => {
|
|
555
|
-
const xmbWithMetadata = `<?xml version="1.0" encoding="UTF-8"?>
|
|
556
|
-
<messagebundle locale="en">
|
|
557
|
-
<msg id="GREETING" desc="Welcome message for users" meaning="salutation">Hello!</msg>
|
|
558
|
-
</messagebundle>`;
|
|
559
|
-
const context = utils.extractTranslatorContext(xmbWithMetadata);
|
|
560
|
-
expect(context.GREETING).toBeDefined();
|
|
561
|
-
expect(context.GREETING.description).toBe("Welcome message for users");
|
|
562
|
-
expect(context.GREETING.meaning).toBe("salutation");
|
|
563
|
-
expect(context.GREETING.originalText).toBe("Hello!");
|
|
564
|
-
});
|
|
565
|
-
test("should detect orphaned translations", () => {
|
|
566
|
-
const xmbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
567
|
-
<messagebundle locale="en">
|
|
568
|
-
<msg id="EXISTING_MESSAGE">Hello</msg>
|
|
569
|
-
</messagebundle>`;
|
|
570
|
-
const xtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
571
|
-
<translationbundle lang="es">
|
|
572
|
-
<translation id="EXISTING_MESSAGE">Hola</translation>
|
|
573
|
-
<translation id="ORPHANED_MESSAGE">Mensaje huérfano</translation>
|
|
574
|
-
</translationbundle>`;
|
|
575
|
-
const validation = utils.validateBundleIntegrity(xmbContent, xtbContent);
|
|
576
|
-
expect(validation.isValid).toBe(true);
|
|
577
|
-
expect(validation.warnings.some(w => w.code === "ORPHANED_TRANSLATION")).toBe(true);
|
|
578
|
-
});
|
|
579
|
-
test("should detect missing translations", () => {
|
|
580
|
-
const xmbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
581
|
-
<messagebundle locale="en">
|
|
582
|
-
<msg id="MESSAGE_1">Hello</msg>
|
|
583
|
-
<msg id="MESSAGE_2">World</msg>
|
|
584
|
-
</messagebundle>`;
|
|
585
|
-
const xtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
586
|
-
<translationbundle lang="es">
|
|
587
|
-
<translation id="MESSAGE_1">Hola</translation>
|
|
588
|
-
</translationbundle>`;
|
|
589
|
-
const validation = utils.validateBundleIntegrity(xmbContent, xtbContent);
|
|
590
|
-
expect(validation.isValid).toBe(true);
|
|
591
|
-
expect(validation.warnings.some(w => w.code === "MISSING_TRANSLATION")).toBe(true);
|
|
592
|
-
});
|
|
593
|
-
test("should handle XMB with single message (non-array)", () => {
|
|
594
|
-
const singleMessageXmb = `<?xml version="1.0" encoding="UTF-8"?>
|
|
595
|
-
<messagebundle locale="en">
|
|
596
|
-
<msg id="SINGLE_MESSAGE" desc="Only one message">Hello World!</msg>
|
|
597
|
-
</messagebundle>`;
|
|
598
|
-
const result = xmbHandler.parse(singleMessageXmb);
|
|
599
|
-
expect(result.SINGLE_MESSAGE).toBe("Hello World!");
|
|
600
|
-
expect(result._metadata?.format).toBe("xmb");
|
|
601
|
-
expect(result._metadata?.xmbMetadata?.locale).toBe("en");
|
|
602
|
-
});
|
|
603
|
-
test("should handle XTB with single translation (non-array)", () => {
|
|
604
|
-
const singleTranslationXtb = `<?xml version="1.0" encoding="UTF-8"?>
|
|
605
|
-
<translationbundle lang="fr">
|
|
606
|
-
<translation id="SINGLE_MESSAGE">Bonjour le monde!</translation>
|
|
607
|
-
</translationbundle>`;
|
|
608
|
-
const result = xtbHandler.parse(singleTranslationXtb);
|
|
609
|
-
expect(result.SINGLE_MESSAGE).toBe("Bonjour le monde!");
|
|
610
|
-
expect(result._metadata?.format).toBe("xtb");
|
|
611
|
-
expect(result._metadata?.xtbMetadata?.language).toBe("fr");
|
|
612
|
-
});
|
|
613
|
-
test("should handle XMB serialization with custom formatting options", () => {
|
|
614
|
-
const xmbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
615
|
-
<messagebundle locale="en">
|
|
616
|
-
<msg id="TEST_MESSAGE">Hello</msg>
|
|
617
|
-
</messagebundle>`;
|
|
618
|
-
const parsed = xmbHandler.parse(xmbContent);
|
|
619
|
-
const serialized = xmbHandler.serialize(parsed, {
|
|
620
|
-
indentation: 4,
|
|
621
|
-
xmlDeclaration: true
|
|
622
|
-
});
|
|
623
|
-
// The serializer should produce valid XML with the expected content
|
|
624
|
-
expect(serialized).toContain("messagebundle");
|
|
625
|
-
expect(serialized).toContain("TEST_MESSAGE");
|
|
626
|
-
expect(serialized).toContain("Hello");
|
|
627
|
-
// XML declaration should be present when xmlDeclaration is not false
|
|
628
|
-
expect(serialized).toContain("<?xml");
|
|
629
|
-
});
|
|
630
|
-
test("should handle XTB serialization with custom formatting options", () => {
|
|
631
|
-
const xtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
632
|
-
<translationbundle lang="es">
|
|
633
|
-
<translation id="TEST_MESSAGE">Hola</translation>
|
|
634
|
-
</translationbundle>`;
|
|
635
|
-
const parsed = xtbHandler.parse(xtbContent);
|
|
636
|
-
const serialized = xtbHandler.serialize(parsed, {
|
|
637
|
-
indentation: "\t",
|
|
638
|
-
xmlDeclaration: false
|
|
639
|
-
});
|
|
640
|
-
// The serializer should produce valid XML with the expected content
|
|
641
|
-
expect(serialized).toContain("translationbundle");
|
|
642
|
-
expect(serialized).toContain("TEST_MESSAGE");
|
|
643
|
-
expect(serialized).toContain("Hola");
|
|
644
|
-
// Test that custom indentation option is accepted (even if not visibly different in test)
|
|
645
|
-
expect(typeof serialized).toBe("string");
|
|
646
|
-
});
|
|
647
|
-
test("should handle XMB reconstruction without original structure", () => {
|
|
648
|
-
const translationData = {
|
|
649
|
-
MESSAGE_1: "Hello",
|
|
650
|
-
MESSAGE_2: "World",
|
|
651
|
-
_metadata: {
|
|
652
|
-
format: "xmb",
|
|
653
|
-
xmbMetadata: {
|
|
654
|
-
locale: "de",
|
|
655
|
-
version: "2.0"
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
};
|
|
659
|
-
const serialized = xmbHandler.serialize(translationData);
|
|
660
|
-
expect(serialized).toContain("locale=\"de\"");
|
|
661
|
-
expect(serialized).toContain("version=\"2.0\"");
|
|
662
|
-
expect(serialized).toContain("MESSAGE_1");
|
|
663
|
-
expect(serialized).toContain("MESSAGE_2");
|
|
664
|
-
});
|
|
665
|
-
test("should handle XTB reconstruction without original structure", () => {
|
|
666
|
-
const translationData = {
|
|
667
|
-
MESSAGE_1: "Hola",
|
|
668
|
-
MESSAGE_2: "Mundo",
|
|
669
|
-
_metadata: {
|
|
670
|
-
format: "xtb",
|
|
671
|
-
xtbMetadata: {
|
|
672
|
-
language: "es",
|
|
673
|
-
version: "1.5"
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
};
|
|
677
|
-
const serialized = xtbHandler.serialize(translationData);
|
|
678
|
-
expect(serialized).toContain("lang=\"es\"");
|
|
679
|
-
expect(serialized).toContain("version=\"1.5\"");
|
|
680
|
-
expect(serialized).toContain("MESSAGE_1");
|
|
681
|
-
expect(serialized).toContain("MESSAGE_2");
|
|
682
|
-
});
|
|
683
|
-
test("should validate XMB structure with invalid data types", () => {
|
|
684
|
-
const invalidData = "not an object";
|
|
685
|
-
const validation = xmbHandler.validateStructure(invalidData);
|
|
686
|
-
expect(validation.isValid).toBe(false);
|
|
687
|
-
expect(validation.errors.some(e => e.code === "INVALID_STRUCTURE")).toBe(true);
|
|
688
|
-
});
|
|
689
|
-
test("should validate XTB structure with invalid data types", () => {
|
|
690
|
-
const invalidData = null;
|
|
691
|
-
const validation = xtbHandler.validateStructure(invalidData);
|
|
692
|
-
expect(validation.isValid).toBe(false);
|
|
693
|
-
expect(validation.errors.some(e => e.code === "INVALID_STRUCTURE")).toBe(true);
|
|
694
|
-
});
|
|
695
|
-
test("should handle XMB with missing locale attribute", () => {
|
|
696
|
-
const xmbWithoutLocale = `<?xml version="1.0" encoding="UTF-8"?>
|
|
697
|
-
<messagebundle>
|
|
698
|
-
<msg id="TEST_MESSAGE">Hello</msg>
|
|
699
|
-
</messagebundle>`;
|
|
700
|
-
const result = xmbHandler.parse(xmbWithoutLocale);
|
|
701
|
-
const validation = xmbHandler.validateStructure(result);
|
|
702
|
-
expect(validation.isValid).toBe(true);
|
|
703
|
-
expect(validation.warnings.some(w => w.code === "MISSING_LOCALE")).toBe(true);
|
|
704
|
-
expect(result._metadata?.xmbMetadata?.locale).toBe("en"); // Default fallback
|
|
705
|
-
});
|
|
706
|
-
test("should handle utils validation with parse errors", () => {
|
|
707
|
-
const invalidXmbContent = "invalid xml content";
|
|
708
|
-
const validXtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
709
|
-
<translationbundle lang="es">
|
|
710
|
-
<translation id="TEST">Test</translation>
|
|
711
|
-
</translationbundle>`;
|
|
712
|
-
const validation = utils.validateBundleIntegrity(invalidXmbContent, validXtbContent);
|
|
713
|
-
expect(validation.isValid).toBe(false);
|
|
714
|
-
expect(validation.errors.some(e => e.code === "BUNDLE_PARSE_ERROR")).toBe(true);
|
|
715
|
-
});
|
|
716
|
-
test("should handle utils message reference validation with parse errors", () => {
|
|
717
|
-
const invalidXmbContent = "invalid xml content";
|
|
718
|
-
const validXtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
719
|
-
<translationbundle lang="es">
|
|
720
|
-
<translation id="TEST">Test</translation>
|
|
721
|
-
</translationbundle>`;
|
|
722
|
-
const validation = utils.validateMessageReferences(invalidXmbContent, validXtbContent);
|
|
723
|
-
expect(validation.isValid).toBe(false);
|
|
724
|
-
expect(validation.errors.some(e => e.code === "REFERENCE_VALIDATION_ERROR")).toBe(true);
|
|
725
|
-
});
|
|
726
|
-
test("should handle utils extract translator context with parse errors", () => {
|
|
727
|
-
const invalidXmbContent = "invalid xml content";
|
|
728
|
-
const context = utils.extractTranslatorContext(invalidXmbContent);
|
|
729
|
-
expect(context).toEqual({});
|
|
730
|
-
});
|
|
731
|
-
test("should detect same language warning in message references", () => {
|
|
732
|
-
const xmbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
733
|
-
<messagebundle locale="en">
|
|
734
|
-
<msg id="TEST_MESSAGE">Hello</msg>
|
|
735
|
-
</messagebundle>`;
|
|
736
|
-
const xtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
737
|
-
<translationbundle lang="en">
|
|
738
|
-
<translation id="TEST_MESSAGE">Hello</translation>
|
|
739
|
-
</translationbundle>`;
|
|
740
|
-
const validation = utils.validateMessageReferences(xmbContent, xtbContent);
|
|
741
|
-
expect(validation.isValid).toBe(true);
|
|
742
|
-
expect(validation.warnings.some(w => w.code === "SAME_LANGUAGE")).toBe(true);
|
|
743
|
-
});
|
|
744
|
-
test("should detect invalid message references", () => {
|
|
745
|
-
const xmbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
746
|
-
<messagebundle locale="en">
|
|
747
|
-
<msg id="VALID_MESSAGE">Hello</msg>
|
|
748
|
-
</messagebundle>`;
|
|
749
|
-
const xtbContent = `<?xml version="1.0" encoding="UTF-8"?>
|
|
750
|
-
<translationbundle lang="es">
|
|
751
|
-
<translation id="INVALID_MESSAGE">Hola</translation>
|
|
752
|
-
</translationbundle>`;
|
|
753
|
-
const validation = utils.validateMessageReferences(xmbContent, xtbContent);
|
|
754
|
-
expect(validation.isValid).toBe(false);
|
|
755
|
-
expect(validation.errors.some(e => e.code === "INVALID_MESSAGE_REFERENCE")).toBe(true);
|
|
756
|
-
});
|
|
757
|
-
});
|
|
758
|
-
});
|