auto-translate-json-library 2.0.2 → 2.0.5

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.
Files changed (40) hide show
  1. package/README.MD +240 -3
  2. package/build/package.json +30 -16
  3. package/build/src/bin/cli.d.ts +1 -1
  4. package/build/src/bin/cli.js +82 -53
  5. package/build/src/config.d.ts +12 -1
  6. package/build/src/files.js +2 -2
  7. package/build/src/huggingface-local.d.ts +11 -0
  8. package/build/src/huggingface-local.js +71 -0
  9. package/build/src/huggingface.d.ts +12 -0
  10. package/build/src/huggingface.js +76 -0
  11. package/build/src/index.js +17 -0
  12. package/build/src/lib.js +9 -1
  13. package/build/src/openai.js +1 -0
  14. package/package.json +30 -16
  15. package/build/tests/arb-handler.test.d.ts +0 -1
  16. package/build/tests/arb-handler.test.js +0 -694
  17. package/build/tests/cli-integration.test.d.ts +0 -1
  18. package/build/tests/cli-integration.test.js +0 -352
  19. package/build/tests/csv-handler.test.d.ts +0 -1
  20. package/build/tests/csv-handler.test.js +0 -507
  21. package/build/tests/error-handling.test.d.ts +0 -1
  22. package/build/tests/error-handling.test.js +0 -456
  23. package/build/tests/files-integration.test.d.ts +0 -1
  24. package/build/tests/files-integration.test.js +0 -421
  25. package/build/tests/json-handler.test.d.ts +0 -1
  26. package/build/tests/json-handler.test.js +0 -526
  27. package/build/tests/po-handler.test.d.ts +0 -1
  28. package/build/tests/po-handler.test.js +0 -768
  29. package/build/tests/properties-handler.test.d.ts +0 -1
  30. package/build/tests/properties-handler.test.js +0 -316
  31. package/build/tests/setup.d.ts +0 -1
  32. package/build/tests/setup.js +0 -5
  33. package/build/tests/xliff-handler.test.d.ts +0 -1
  34. package/build/tests/xliff-handler.test.js +0 -459
  35. package/build/tests/xmb-xtb-handler.test.d.ts +0 -1
  36. package/build/tests/xmb-xtb-handler.test.js +0 -758
  37. package/build/tests/xml-handler.test.d.ts +0 -1
  38. package/build/tests/xml-handler.test.js +0 -570
  39. package/build/tests/yaml-handler.test.d.ts +0 -1
  40. package/build/tests/yaml-handler.test.js +0 -156
@@ -1,459 +0,0 @@
1
- import { XliffHandler } from '../src/format/xliff-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 * as fs from 'fs';
7
- import * as path from 'path';
8
- describe('XliffHandler', () => {
9
- let handler;
10
- beforeEach(() => {
11
- handler = new XliffHandler();
12
- });
13
- describe('canHandle', () => {
14
- it('should handle .xlf and .xliff files', () => {
15
- expect(handler.canHandle('test.xlf')).toBe(true);
16
- expect(handler.canHandle('messages.xliff')).toBe(true);
17
- });
18
- it('should not handle non-xliff files', () => {
19
- expect(handler.canHandle('test.json')).toBe(false);
20
- expect(handler.canHandle('test.xml')).toBe(false);
21
- expect(handler.canHandle('test.txt')).toBe(false);
22
- });
23
- it('should validate XLIFF content when provided', () => {
24
- const validXliff = '<?xml version="1.0"?><xliff version="1.2"><file><body></body></file></xliff>';
25
- const invalidContent = 'not xliff content';
26
- expect(handler.canHandle('test.xlf', validXliff)).toBe(true);
27
- expect(handler.canHandle('test.xlf', invalidContent)).toBe(false);
28
- });
29
- });
30
- describe('XLIFF 1.2 parsing', () => {
31
- let xliff12Content;
32
- beforeAll(() => {
33
- xliff12Content = fs.readFileSync(path.join(__dirname, 'test-data', 'xliff-1.2-sample.xlf'), 'utf-8');
34
- });
35
- it('should parse XLIFF 1.2 format correctly', () => {
36
- const result = handler.parse(xliff12Content);
37
- // Should include non-approved and untranslated entries
38
- expect(result.welcome_message).toBe('¡Bienvenido a nuestra aplicación!');
39
- expect(result.error_network).toBe('Network connection failed'); // Empty target, should use source
40
- expect(result.app_name).toBe('My Application'); // No target, should use source
41
- // Should NOT include approved entries
42
- expect(result.button_submit).toBeUndefined();
43
- });
44
- it('should preserve XLIFF 1.2 metadata correctly', () => {
45
- const result = handler.parse(xliff12Content);
46
- expect(result._metadata).toBeDefined();
47
- expect(result._metadata?.format).toBe('xliff');
48
- expect(result._metadata?.version).toBe('1.2');
49
- expect(result._metadata?.sourceLanguage).toBe('en');
50
- expect(result._metadata?.targetLanguage).toBe('es');
51
- expect(result._metadata?.originalStructure).toBeDefined();
52
- expect(result._metadata?.preserveComments).toBe(true);
53
- expect(result._metadata?.preserveAttributes).toBe(true);
54
- });
55
- it('should serialize XLIFF 1.2 correctly with updated translations', () => {
56
- const parsed = handler.parse(xliff12Content);
57
- // Update translations
58
- const updated = {
59
- ...parsed,
60
- error_network: 'Falló la conexión de red',
61
- app_name: 'Mi Aplicación'
62
- };
63
- const result = handler.serialize(updated);
64
- expect(result).toContain('<?xml version="1.0" encoding="UTF-8"?>');
65
- expect(result).toContain('<xliff version="1.2"');
66
- expect(result).toContain('source-language="en"');
67
- expect(result).toContain('target-language="es"');
68
- expect(result).toContain('Falló la conexión de red');
69
- expect(result).toContain('Mi Aplicación');
70
- // Approved translations should remain unchanged
71
- expect(result).toContain('approved="yes"');
72
- expect(result).toContain('Enviar'); // Original approved translation
73
- });
74
- it('should handle XLIFF 1.2 round-trip translation', () => {
75
- const parsed = handler.parse(xliff12Content);
76
- // Simulate translation
77
- const translated = {
78
- ...parsed,
79
- error_network: 'Falló la conexión de red',
80
- app_name: 'Mi Aplicación'
81
- };
82
- const serialized = handler.serialize(translated);
83
- const reparsed = handler.parse(serialized);
84
- expect(reparsed.error_network).toBe('Falló la conexión de red');
85
- expect(reparsed.app_name).toBe('Mi Aplicación');
86
- expect(reparsed.welcome_message).toBe('¡Bienvenido a nuestra aplicación!');
87
- });
88
- });
89
- describe('XLIFF 2.0 parsing', () => {
90
- let xliff20Content;
91
- beforeAll(() => {
92
- xliff20Content = fs.readFileSync(path.join(__dirname, 'test-data', 'xliff-2.0-sample.xlf'), 'utf-8');
93
- });
94
- it('should parse XLIFF 2.0 format correctly', () => {
95
- const result = handler.parse(xliff20Content);
96
- // Should include non-approved and untranslated entries
97
- expect(result.welcome_message).toBe('Bienvenue dans notre application!');
98
- expect(result.error_network).toBe('Network connection failed'); // Empty target, should use source
99
- expect(result.app_name).toBe('My Application'); // No target, should use source
100
- // Multi-segment handling
101
- expect(result['multi_segment_message.0']).toBe('Première partie du message');
102
- expect(result['multi_segment_message.1']).toBe('Deuxième partie du message');
103
- // Should NOT include approved entries
104
- expect(result.button_submit).toBeUndefined();
105
- });
106
- it('should preserve XLIFF 2.0 metadata correctly', () => {
107
- const result = handler.parse(xliff20Content);
108
- expect(result._metadata).toBeDefined();
109
- expect(result._metadata?.format).toBe('xliff');
110
- expect(result._metadata?.version).toBe('2.0');
111
- expect(result._metadata?.sourceLanguage).toBe('en');
112
- expect(result._metadata?.targetLanguage).toBe('fr');
113
- expect(result._metadata?.originalStructure).toBeDefined();
114
- });
115
- it('should serialize XLIFF 2.0 correctly with updated translations', () => {
116
- const parsed = handler.parse(xliff20Content);
117
- // Update translations
118
- const updated = {
119
- ...parsed,
120
- error_network: 'Échec de la connexion réseau',
121
- app_name: 'Mon Application'
122
- };
123
- const result = handler.serialize(updated);
124
- expect(result).toContain('<?xml version="1.0" encoding="UTF-8"?>');
125
- expect(result).toContain('<xliff version="2.0"');
126
- expect(result).toContain('srcLang="en"');
127
- expect(result).toContain('trgLang="fr"');
128
- expect(result).toContain('Échec de la connexion réseau');
129
- expect(result).toContain('Mon Application');
130
- // Approved translations should remain unchanged
131
- expect(result).toContain('approved="yes"');
132
- expect(result).toContain('Soumettre'); // Original approved translation
133
- });
134
- it('should handle XLIFF 2.0 round-trip translation', () => {
135
- const parsed = handler.parse(xliff20Content);
136
- // Simulate translation
137
- const translated = {
138
- ...parsed,
139
- error_network: 'Échec de la connexion réseau',
140
- app_name: 'Mon Application',
141
- 'multi_segment_message.0': 'Première partie traduite',
142
- 'multi_segment_message.1': 'Deuxième partie traduite'
143
- };
144
- const serialized = handler.serialize(translated);
145
- const reparsed = handler.parse(serialized);
146
- expect(reparsed.error_network).toBe('Échec de la connexion réseau');
147
- expect(reparsed.app_name).toBe('Mon Application');
148
- expect(reparsed['multi_segment_message.0']).toBe('Première partie traduite');
149
- expect(reparsed['multi_segment_message.1']).toBe('Deuxième partie traduite');
150
- });
151
- });
152
- describe('workflow state management', () => {
153
- it('should respect approved translation states in XLIFF 1.2', () => {
154
- const xliffWithApproved = `<?xml version="1.0" encoding="UTF-8"?>
155
- <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
156
- <file original="test.properties" source-language="en" target-language="es" datatype="plaintext">
157
- <body>
158
- <trans-unit id="approved_text" approved="yes">
159
- <source>Hello</source>
160
- <target>Hola</target>
161
- </trans-unit>
162
- <trans-unit id="unapproved_text" approved="no">
163
- <source>Goodbye</source>
164
- <target>Adiós</target>
165
- </trans-unit>
166
- <trans-unit id="no_target">
167
- <source>New text</source>
168
- </trans-unit>
169
- </body>
170
- </file>
171
- </xliff>`;
172
- const result = handler.parse(xliffWithApproved);
173
- // Should NOT include approved entries
174
- expect(result.approved_text).toBeUndefined();
175
- // Should include unapproved and entries without targets
176
- expect(result.unapproved_text).toBe('Adiós');
177
- expect(result.no_target).toBe('New text');
178
- });
179
- it('should respect approved translation states in XLIFF 2.0', () => {
180
- const xliffWithApproved = `<?xml version="1.0" encoding="UTF-8"?>
181
- <xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">
182
- <file id="test">
183
- <unit id="approved_text" approved="yes">
184
- <segment>
185
- <source>Hello</source>
186
- <target>Bonjour</target>
187
- </segment>
188
- </unit>
189
- <unit id="unapproved_text" approved="no">
190
- <segment>
191
- <source>Goodbye</source>
192
- <target>Au revoir</target>
193
- </segment>
194
- </unit>
195
- <unit id="no_target">
196
- <segment>
197
- <source>New text</source>
198
- </segment>
199
- </unit>
200
- </file>
201
- </xliff>`;
202
- const result = handler.parse(xliffWithApproved);
203
- // Should NOT include approved entries
204
- expect(result.approved_text).toBeUndefined();
205
- // Should include unapproved and entries without targets
206
- expect(result.unapproved_text).toBe('Au revoir');
207
- expect(result.no_target).toBe('New text');
208
- });
209
- it('should not overwrite approved translations during serialization', () => {
210
- const xliffContent = `<?xml version="1.0" encoding="UTF-8"?>
211
- <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
212
- <file original="test.properties" source-language="en" target-language="es" datatype="plaintext">
213
- <body>
214
- <trans-unit id="approved_text" approved="yes">
215
- <source>Hello</source>
216
- <target>Hola</target>
217
- </trans-unit>
218
- <trans-unit id="unapproved_text" approved="no">
219
- <source>Goodbye</source>
220
- <target>Adiós</target>
221
- </trans-unit>
222
- </body>
223
- </file>
224
- </xliff>`;
225
- const parsed = handler.parse(xliffContent);
226
- // Try to "translate" both entries (but approved should be ignored)
227
- const updated = {
228
- ...parsed,
229
- approved_text: 'Should not appear', // This should be ignored
230
- unapproved_text: 'Hasta luego'
231
- };
232
- const result = handler.serialize(updated);
233
- // Approved translation should remain unchanged
234
- expect(result).toContain('Hola');
235
- expect(result).not.toContain('Should not appear');
236
- // Unapproved translation should be updated
237
- expect(result).toContain('Hasta luego');
238
- });
239
- it('should update translation states when adding new translations', () => {
240
- const xliffContent = `<?xml version="1.0" encoding="UTF-8"?>
241
- <xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
242
- <file original="test.properties" source-language="en" target-language="es" datatype="plaintext">
243
- <body>
244
- <trans-unit id="new_text">
245
- <source>New message</source>
246
- </trans-unit>
247
- </body>
248
- </file>
249
- </xliff>`;
250
- const parsed = handler.parse(xliffContent);
251
- const updated = {
252
- ...parsed,
253
- new_text: 'Nuevo mensaje'
254
- };
255
- const result = handler.serialize(updated);
256
- // Should contain the new translation
257
- expect(result).toContain('Nuevo mensaje');
258
- // Should mark as not approved since it's a new translation
259
- expect(result).toContain('approved="no"');
260
- });
261
- });
262
- describe('validation', () => {
263
- it('should validate XLIFF 1.2 structure', () => {
264
- const validXliff12 = {
265
- xliff: {
266
- '@_version': '1.2',
267
- file: {
268
- body: {
269
- 'trans-unit': [
270
- {
271
- '@_id': 'test',
272
- source: { '#text': 'Test' },
273
- target: { '#text': 'Prueba' }
274
- }
275
- ]
276
- }
277
- }
278
- }
279
- };
280
- const result = handler.validateStructure({
281
- _metadata: { originalStructure: validXliff12 }
282
- });
283
- expect(result.isValid).toBe(true);
284
- expect(result.errors).toHaveLength(0);
285
- });
286
- it('should validate XLIFF 2.0 structure', () => {
287
- const validXliff20 = {
288
- xliff: {
289
- '@_version': '2.0',
290
- file: {
291
- unit: [
292
- {
293
- '@_id': 'test',
294
- segment: {
295
- source: { '#text': 'Test' },
296
- target: { '#text': 'Test' }
297
- }
298
- }
299
- ]
300
- }
301
- }
302
- };
303
- const result = handler.validateStructure({
304
- _metadata: { originalStructure: validXliff20 }
305
- });
306
- expect(result.isValid).toBe(true);
307
- expect(result.errors).toHaveLength(0);
308
- });
309
- it('should detect missing xliff root element', () => {
310
- const invalidStructure = {
311
- file: {
312
- body: {
313
- 'trans-unit': []
314
- }
315
- }
316
- };
317
- const result = handler.validateStructure({
318
- _metadata: { originalStructure: invalidStructure }
319
- });
320
- expect(result.isValid).toBe(false);
321
- expect(result.errors.length).toBeGreaterThan(0);
322
- expect(result.errors[0].code).toBe('MISSING_XLIFF_ROOT');
323
- });
324
- it('should detect missing file element in XLIFF 1.2', () => {
325
- const invalidXliff12 = {
326
- xliff: {
327
- '@_version': '1.2'
328
- // Missing file element
329
- }
330
- };
331
- const result = handler.validateStructure({
332
- _metadata: { originalStructure: invalidXliff12, version: '1.2' }
333
- });
334
- expect(result.isValid).toBe(false);
335
- expect(result.errors.length).toBeGreaterThan(0);
336
- expect(result.errors[0].code).toBe('MISSING_FILE_ELEMENT');
337
- });
338
- it('should detect missing body element in XLIFF 1.2', () => {
339
- const invalidXliff12 = {
340
- xliff: {
341
- '@_version': '1.2',
342
- file: {
343
- // Missing body element
344
- }
345
- }
346
- };
347
- const result = handler.validateStructure({
348
- _metadata: { originalStructure: invalidXliff12, version: '1.2' }
349
- });
350
- expect(result.isValid).toBe(false);
351
- expect(result.errors.length).toBeGreaterThan(0);
352
- expect(result.errors[0].code).toBe('MISSING_BODY_ELEMENT');
353
- });
354
- it('should warn about missing trans-units in XLIFF 1.2', () => {
355
- const emptyXliff12 = {
356
- xliff: {
357
- '@_version': '1.2',
358
- file: {
359
- body: {
360
- // No trans-unit elements
361
- }
362
- }
363
- }
364
- };
365
- const result = handler.validateStructure({
366
- _metadata: { originalStructure: emptyXliff12, version: '1.2' }
367
- });
368
- expect(result.isValid).toBe(true); // Valid but with warnings
369
- expect(result.warnings.length).toBeGreaterThan(0);
370
- expect(result.warnings.some(w => w.code === 'NO_TRANS_UNITS')).toBe(true);
371
- });
372
- it('should warn about missing units in XLIFF 2.0', () => {
373
- const emptyXliff20 = {
374
- xliff: {
375
- '@_version': '2.0',
376
- file: {
377
- // No unit elements
378
- }
379
- }
380
- };
381
- const result = handler.validateStructure({
382
- _metadata: { originalStructure: emptyXliff20, version: '2.0' }
383
- });
384
- expect(result.isValid).toBe(true); // Valid but with warnings
385
- expect(result.warnings.length).toBeGreaterThan(0);
386
- expect(result.warnings.some(w => w.code === 'NO_UNITS')).toBe(true);
387
- });
388
- it('should handle empty XLIFF gracefully', () => {
389
- const emptyData = {};
390
- const result = handler.validateStructure(emptyData);
391
- expect(result.isValid).toBe(true); // Empty is valid but should have warnings
392
- expect(result.warnings.length).toBeGreaterThan(0);
393
- expect(result.warnings[0].code).toBe('EMPTY_XLIFF');
394
- });
395
- });
396
- describe('error handling', () => {
397
- it('should throw error for invalid XLIFF content', () => {
398
- const invalidXliff = 'This is not valid XLIFF content';
399
- expect(() => handler.parse(invalidXliff)).toThrow('Failed to parse XLIFF');
400
- });
401
- it('should throw error for malformed XML', () => {
402
- // fast-xml-parser is very lenient, so we test with completely invalid content
403
- const malformedXliff = 'completely invalid content that is not XML at all';
404
- expect(() => handler.parse(malformedXliff)).toThrow('Failed to parse XLIFF');
405
- });
406
- it('should throw error for XML without xliff root element', () => {
407
- const nonXliffXml = '<?xml version="1.0"?><root><element>content</element></root>';
408
- expect(() => handler.parse(nonXliffXml)).toThrow('Invalid XLIFF format: missing xliff root element');
409
- });
410
- });
411
- describe('getFileExtension', () => {
412
- it('should return .xlf extension', () => {
413
- expect(handler.getFileExtension()).toBe('.xlf');
414
- });
415
- });
416
- describe('format reconstruction', () => {
417
- it('should reconstruct XLIFF 1.2 structure when no original structure is available', () => {
418
- const data = {
419
- test_key: 'Test Value',
420
- another_key: 'Another Value',
421
- _metadata: {
422
- format: 'xliff',
423
- sourceLanguage: 'en',
424
- targetLanguage: 'es'
425
- }
426
- };
427
- const result = handler.serialize(data);
428
- expect(result).toContain('<?xml version="1.0" encoding="UTF-8"?>');
429
- expect(result).toContain('<xliff version="1.2"');
430
- expect(result).toContain('source-language="en"');
431
- expect(result).toContain('target-language="es"');
432
- expect(result).toContain('Test Value');
433
- expect(result).toContain('Another Value');
434
- expect(result).toContain('id="test_key"');
435
- expect(result).toContain('id="another_key"');
436
- });
437
- it('should apply formatting options to XLIFF output', () => {
438
- const data = {
439
- test_key: 'Test Value'
440
- };
441
- const resultWithTabs = handler.serialize(data, { indentation: '\t' });
442
- const resultWith4Spaces = handler.serialize(data, { indentation: 4 });
443
- // Both should contain proper XLIFF structure
444
- expect(resultWithTabs).toContain('<xliff');
445
- expect(resultWith4Spaces).toContain('<xliff');
446
- expect(resultWithTabs).toContain('Test Value');
447
- expect(resultWith4Spaces).toContain('Test Value');
448
- });
449
- it('should handle XML declaration option', () => {
450
- const data = {
451
- test_key: 'Test Value'
452
- };
453
- const resultWithDeclaration = handler.serialize(data, { xmlDeclaration: true });
454
- const resultWithoutDeclaration = handler.serialize(data, { xmlDeclaration: false });
455
- expect(resultWithDeclaration).toMatch(/^<\?xml version="1\.0" encoding="UTF-8"\?>/);
456
- expect(resultWithoutDeclaration).not.toMatch(/^<\?xml/);
457
- });
458
- });
459
- });
@@ -1 +0,0 @@
1
- export {};