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 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,570 +0,0 @@
|
|
|
1
|
-
import { XmlHandler } from '../src/format/xml-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('XmlHandler', () => {
|
|
9
|
-
let handler;
|
|
10
|
-
beforeEach(() => {
|
|
11
|
-
handler = new XmlHandler();
|
|
12
|
-
});
|
|
13
|
-
describe('canHandle', () => {
|
|
14
|
-
it('should handle .xml files', () => {
|
|
15
|
-
expect(handler.canHandle('test.xml')).toBe(true);
|
|
16
|
-
expect(handler.canHandle('strings.xml')).toBe(true);
|
|
17
|
-
});
|
|
18
|
-
it('should not handle non-xml files', () => {
|
|
19
|
-
expect(handler.canHandle('test.json')).toBe(false);
|
|
20
|
-
expect(handler.canHandle('test.txt')).toBe(false);
|
|
21
|
-
});
|
|
22
|
-
it('should validate XML content when provided', () => {
|
|
23
|
-
expect(handler.canHandle('test.xml', '<?xml version="1.0"?><root></root>')).toBe(true);
|
|
24
|
-
expect(handler.canHandle('test.xml', '<root></root>')).toBe(true);
|
|
25
|
-
expect(handler.canHandle('test.xml', 'not xml content')).toBe(false); // Should be false for invalid XML
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
describe('Android strings.xml parsing', () => {
|
|
29
|
-
const androidXml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
30
|
-
<resources>
|
|
31
|
-
<string name="app_name">My App</string>
|
|
32
|
-
<string name="welcome_message">Welcome to our application!</string>
|
|
33
|
-
<string name="button_text">Click me</string>
|
|
34
|
-
<!-- This is a comment -->
|
|
35
|
-
<string name="formatted_text"><![CDATA[This is <b>bold</b> text]]></string>
|
|
36
|
-
</resources>`;
|
|
37
|
-
it('should parse Android strings.xml format correctly', () => {
|
|
38
|
-
const result = handler.parse(androidXml);
|
|
39
|
-
expect(result.app_name).toBe('My App');
|
|
40
|
-
expect(result.welcome_message).toBe('Welcome to our application!');
|
|
41
|
-
expect(result.button_text).toBe('Click me');
|
|
42
|
-
expect(result.formatted_text).toBe('This is <b>bold</b> text');
|
|
43
|
-
});
|
|
44
|
-
it('should preserve metadata for Android format', () => {
|
|
45
|
-
const result = handler.parse(androidXml);
|
|
46
|
-
expect(result._metadata).toBeDefined();
|
|
47
|
-
expect(result._metadata?.format).toBe('android-xml');
|
|
48
|
-
expect(result._metadata?.originalStructure).toBeDefined();
|
|
49
|
-
expect(result._metadata?.preserveComments).toBe(true);
|
|
50
|
-
expect(result._metadata?.preserveAttributes).toBe(true);
|
|
51
|
-
});
|
|
52
|
-
it('should handle Android XML with grouped strings', () => {
|
|
53
|
-
const groupedXml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
54
|
-
<resources>
|
|
55
|
-
<string name="app_name">My App</string>
|
|
56
|
-
<group name="errors">
|
|
57
|
-
<string name="network_error">Network connection failed</string>
|
|
58
|
-
<string name="validation_error">Please check your input</string>
|
|
59
|
-
</group>
|
|
60
|
-
</resources>`;
|
|
61
|
-
const result = handler.parse(groupedXml);
|
|
62
|
-
expect(result.app_name).toBe('My App');
|
|
63
|
-
expect(result.errors).toBeDefined();
|
|
64
|
-
expect(typeof result.errors).toBe('object');
|
|
65
|
-
expect(result.errors.network_error).toBe('Network connection failed');
|
|
66
|
-
expect(result.errors.validation_error).toBe('Please check your input');
|
|
67
|
-
});
|
|
68
|
-
it('should serialize Android XML correctly', () => {
|
|
69
|
-
const translationData = {
|
|
70
|
-
app_name: 'Mi Aplicación',
|
|
71
|
-
welcome_message: '¡Bienvenido a nuestra aplicación!',
|
|
72
|
-
button_text: 'Haz clic aquí',
|
|
73
|
-
_metadata: {
|
|
74
|
-
format: 'android-xml',
|
|
75
|
-
originalStructure: {
|
|
76
|
-
resources: {
|
|
77
|
-
string: [
|
|
78
|
-
{ '@_name': 'app_name', '#text': 'My App' },
|
|
79
|
-
{ '@_name': 'welcome_message', '#text': 'Welcome to our application!' },
|
|
80
|
-
{ '@_name': 'button_text', '#text': 'Click me' }
|
|
81
|
-
]
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
const result = handler.serialize(translationData);
|
|
87
|
-
expect(result).toContain('<?xml version="1.0" encoding="UTF-8"?>');
|
|
88
|
-
expect(result).toContain('<resources>');
|
|
89
|
-
expect(result).toContain('name="app_name"');
|
|
90
|
-
expect(result).toContain('Mi Aplicación');
|
|
91
|
-
expect(result).toContain('¡Bienvenido a nuestra aplicación!');
|
|
92
|
-
expect(result).toContain('Haz clic aquí');
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
describe('iOS XML parsing', () => {
|
|
96
|
-
const iosXml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
97
|
-
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
98
|
-
<plist version="1.0">
|
|
99
|
-
<dict>
|
|
100
|
-
<key>app_name</key>
|
|
101
|
-
<string>My iOS App</string>
|
|
102
|
-
<key>welcome_message</key>
|
|
103
|
-
<string>Welcome to iOS!</string>
|
|
104
|
-
<key>settings_title</key>
|
|
105
|
-
<string>Settings</string>
|
|
106
|
-
</dict>
|
|
107
|
-
</plist>`;
|
|
108
|
-
it('should parse iOS plist XML format correctly', () => {
|
|
109
|
-
const result = handler.parse(iosXml);
|
|
110
|
-
expect(result.app_name).toBe('My iOS App');
|
|
111
|
-
expect(result.welcome_message).toBe('Welcome to iOS!');
|
|
112
|
-
expect(result.settings_title).toBe('Settings');
|
|
113
|
-
});
|
|
114
|
-
it('should preserve metadata for iOS format', () => {
|
|
115
|
-
const result = handler.parse(iosXml);
|
|
116
|
-
expect(result._metadata).toBeDefined();
|
|
117
|
-
expect(result._metadata?.format).toBe('ios-xml');
|
|
118
|
-
expect(result._metadata?.originalStructure).toBeDefined();
|
|
119
|
-
});
|
|
120
|
-
it('should serialize iOS XML correctly', () => {
|
|
121
|
-
const translationData = {
|
|
122
|
-
app_name: 'Mi App iOS',
|
|
123
|
-
welcome_message: '¡Bienvenido a iOS!',
|
|
124
|
-
_metadata: {
|
|
125
|
-
format: 'ios-xml',
|
|
126
|
-
originalStructure: {
|
|
127
|
-
plist: {
|
|
128
|
-
'@_version': '1.0',
|
|
129
|
-
dict: {
|
|
130
|
-
key: [
|
|
131
|
-
{ '#text': 'app_name' },
|
|
132
|
-
{ '#text': 'welcome_message' }
|
|
133
|
-
],
|
|
134
|
-
string: [
|
|
135
|
-
{ '#text': 'My iOS App' },
|
|
136
|
-
{ '#text': 'Welcome to iOS!' }
|
|
137
|
-
]
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
const result = handler.serialize(translationData);
|
|
144
|
-
expect(result).toContain('<?xml version="1.0" encoding="UTF-8"?>');
|
|
145
|
-
expect(result).toContain('<plist');
|
|
146
|
-
expect(result).toContain('<dict>');
|
|
147
|
-
expect(result).toContain('Mi App iOS');
|
|
148
|
-
expect(result).toContain('¡Bienvenido a iOS!');
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
describe('Generic XML parsing', () => {
|
|
152
|
-
const genericXml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
153
|
-
<translations>
|
|
154
|
-
<messages>
|
|
155
|
-
<greeting>Hello World</greeting>
|
|
156
|
-
<farewell>Goodbye</farewell>
|
|
157
|
-
</messages>
|
|
158
|
-
<labels>
|
|
159
|
-
<submit>Submit</submit>
|
|
160
|
-
<cancel>Cancel</cancel>
|
|
161
|
-
</labels>
|
|
162
|
-
</translations>`;
|
|
163
|
-
it('should parse generic XML format correctly', () => {
|
|
164
|
-
const result = handler.parse(genericXml);
|
|
165
|
-
expect(result['messages.greeting']).toBe('Hello World');
|
|
166
|
-
expect(result['messages.farewell']).toBe('Goodbye');
|
|
167
|
-
expect(result['labels.submit']).toBe('Submit');
|
|
168
|
-
expect(result['labels.cancel']).toBe('Cancel');
|
|
169
|
-
});
|
|
170
|
-
it('should preserve metadata for generic format', () => {
|
|
171
|
-
const result = handler.parse(genericXml);
|
|
172
|
-
expect(result._metadata).toBeDefined();
|
|
173
|
-
expect(result._metadata?.format).toBe('generic-xml');
|
|
174
|
-
expect(result._metadata?.originalStructure).toBeDefined();
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
describe('validation', () => {
|
|
178
|
-
it('should validate Android XML structure', () => {
|
|
179
|
-
const validData = {
|
|
180
|
-
resources: {
|
|
181
|
-
string: [
|
|
182
|
-
{ '@_name': 'test', '#text': 'Test Value' }
|
|
183
|
-
]
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
const result = handler.validateStructure(validData);
|
|
187
|
-
expect(result.isValid).toBe(true);
|
|
188
|
-
expect(result.errors).toHaveLength(0);
|
|
189
|
-
});
|
|
190
|
-
it('should detect invalid Android XML structure', () => {
|
|
191
|
-
const invalidData = {
|
|
192
|
-
// Missing resources element
|
|
193
|
-
string: [{ '@_name': 'test', '#text': 'Test Value' }]
|
|
194
|
-
};
|
|
195
|
-
const result = handler.validateStructure(invalidData);
|
|
196
|
-
expect(result.isValid).toBe(false);
|
|
197
|
-
expect(result.errors.length).toBeGreaterThan(0);
|
|
198
|
-
expect(result.errors[0].code).toBe('MISSING_RESOURCES');
|
|
199
|
-
});
|
|
200
|
-
it('should validate iOS XML structure', () => {
|
|
201
|
-
const validData = {
|
|
202
|
-
plist: {
|
|
203
|
-
dict: {
|
|
204
|
-
key: [{ '#text': 'test_key' }],
|
|
205
|
-
string: [{ '#text': 'Test Value' }]
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
const result = handler.validateStructure(validData);
|
|
210
|
-
expect(result.isValid).toBe(true);
|
|
211
|
-
expect(result.errors).toHaveLength(0);
|
|
212
|
-
});
|
|
213
|
-
it('should detect invalid iOS XML structure', () => {
|
|
214
|
-
const invalidData = {
|
|
215
|
-
// Missing plist element
|
|
216
|
-
dict: {
|
|
217
|
-
key: [{ '#text': 'test_key' }]
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
const result = handler.validateStructure(invalidData);
|
|
221
|
-
expect(result.isValid).toBe(false);
|
|
222
|
-
expect(result.errors.length).toBeGreaterThan(0);
|
|
223
|
-
expect(result.errors[0].code).toBe('MISSING_PLIST');
|
|
224
|
-
});
|
|
225
|
-
it('should handle empty XML gracefully', () => {
|
|
226
|
-
const emptyData = {};
|
|
227
|
-
const result = handler.validateStructure(emptyData);
|
|
228
|
-
expect(result.isValid).toBe(true); // Empty is valid but should have warnings
|
|
229
|
-
expect(result.warnings.length).toBeGreaterThan(0);
|
|
230
|
-
expect(result.warnings[0].code).toBe('EMPTY_XML');
|
|
231
|
-
});
|
|
232
|
-
});
|
|
233
|
-
describe('error handling', () => {
|
|
234
|
-
it('should throw error for invalid XML content', () => {
|
|
235
|
-
const invalidXml = 'This is not valid XML content';
|
|
236
|
-
expect(() => handler.parse(invalidXml)).toThrow('Failed to parse XML');
|
|
237
|
-
});
|
|
238
|
-
it('should throw error for malformed XML', () => {
|
|
239
|
-
const malformedXml = '<?xml version="1.0"?><root><unclosed>';
|
|
240
|
-
expect(() => handler.parse(malformedXml)).toThrow('Failed to parse XML');
|
|
241
|
-
});
|
|
242
|
-
});
|
|
243
|
-
describe('getFileExtension', () => {
|
|
244
|
-
it('should return .xml extension', () => {
|
|
245
|
-
expect(handler.getFileExtension()).toBe('.xml');
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
describe('enhanced Android strings.xml support', () => {
|
|
249
|
-
let androidXmlContent;
|
|
250
|
-
let androidGroupedXmlContent;
|
|
251
|
-
beforeAll(() => {
|
|
252
|
-
androidXmlContent = fs.readFileSync(path.join(__dirname, 'test-data', 'android-strings.xml'), 'utf-8');
|
|
253
|
-
androidGroupedXmlContent = fs.readFileSync(path.join(__dirname, 'test-data', 'android-grouped-strings.xml'), 'utf-8');
|
|
254
|
-
});
|
|
255
|
-
it('should parse Android strings.xml with special characters and CDATA', () => {
|
|
256
|
-
const result = handler.parse(androidXmlContent);
|
|
257
|
-
expect(result.app_name).toBe('My Android App');
|
|
258
|
-
expect(result.welcome_message).toBe('Welcome to our application!');
|
|
259
|
-
expect(result.button_text).toBe('Click me');
|
|
260
|
-
expect(result.formatted_text).toBe('This is <b>bold</b> text');
|
|
261
|
-
expect(result.empty_string).toBe('');
|
|
262
|
-
expect(result.special_chars).toBe('Special chars: & < > " \'');
|
|
263
|
-
});
|
|
264
|
-
it('should preserve Android XML metadata correctly', () => {
|
|
265
|
-
const result = handler.parse(androidXmlContent);
|
|
266
|
-
expect(result._metadata).toBeDefined();
|
|
267
|
-
expect(result._metadata?.format).toBe('android-xml');
|
|
268
|
-
expect(result._metadata?.originalStructure).toBeDefined();
|
|
269
|
-
expect(result._metadata?.preserveComments).toBe(true);
|
|
270
|
-
expect(result._metadata?.preserveAttributes).toBe(true);
|
|
271
|
-
// Verify original structure contains resources
|
|
272
|
-
const original = result._metadata?.originalStructure;
|
|
273
|
-
expect(original.resources).toBeDefined();
|
|
274
|
-
expect(original.resources.string).toBeDefined();
|
|
275
|
-
});
|
|
276
|
-
it('should handle Android XML with grouped strings correctly', () => {
|
|
277
|
-
const result = handler.parse(androidGroupedXmlContent);
|
|
278
|
-
expect(result.app_name).toBe('My Android App');
|
|
279
|
-
expect(result.errors).toBeDefined();
|
|
280
|
-
expect(typeof result.errors).toBe('object');
|
|
281
|
-
const errors = result.errors;
|
|
282
|
-
expect(errors.network_error).toBe('Network connection failed');
|
|
283
|
-
expect(errors.validation_error).toBe('Please check your input');
|
|
284
|
-
expect(errors.server_error).toBe('Server is temporarily unavailable');
|
|
285
|
-
const buttons = result.buttons;
|
|
286
|
-
expect(buttons.submit).toBe('Submit');
|
|
287
|
-
expect(buttons.cancel).toBe('Cancel');
|
|
288
|
-
expect(buttons.retry).toBe('Retry');
|
|
289
|
-
});
|
|
290
|
-
it('should serialize Android XML correctly with proper structure', () => {
|
|
291
|
-
const translationData = {
|
|
292
|
-
app_name: 'Mi Aplicación Android',
|
|
293
|
-
welcome_message: '¡Bienvenido a nuestra aplicación!',
|
|
294
|
-
button_text: 'Haz clic aquí',
|
|
295
|
-
formatted_text: 'Este es texto <b>negrita</b>',
|
|
296
|
-
empty_string: '',
|
|
297
|
-
special_chars: 'Caracteres especiales: & < > " \'',
|
|
298
|
-
_metadata: {
|
|
299
|
-
format: 'android-xml',
|
|
300
|
-
originalStructure: {
|
|
301
|
-
resources: {
|
|
302
|
-
string: [
|
|
303
|
-
{ '@_name': 'app_name', '#text': 'My Android App' },
|
|
304
|
-
{ '@_name': 'welcome_message', '#text': 'Welcome to our application!' },
|
|
305
|
-
{ '@_name': 'button_text', '#text': 'Click me' },
|
|
306
|
-
{ '@_name': 'formatted_text', '#text': 'This is <b>bold</b> text' },
|
|
307
|
-
{ '@_name': 'empty_string', '#text': '' },
|
|
308
|
-
{ '@_name': 'special_chars', '#text': 'Special chars: & < > " \'' }
|
|
309
|
-
]
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
|
-
const result = handler.serialize(translationData);
|
|
315
|
-
expect(result).toContain('<?xml version="1.0" encoding="UTF-8"?>');
|
|
316
|
-
expect(result).toContain('<resources>');
|
|
317
|
-
expect(result).toContain('name="app_name"');
|
|
318
|
-
expect(result).toContain('Mi Aplicación Android');
|
|
319
|
-
expect(result).toContain('¡Bienvenido a nuestra aplicación!');
|
|
320
|
-
expect(result).toContain('Haz clic aquí');
|
|
321
|
-
expect(result).toContain('Este es texto <b>negrita</b>');
|
|
322
|
-
expect(result).toContain('Caracteres especiales: & < > " '');
|
|
323
|
-
expect(result).toContain('</resources>');
|
|
324
|
-
});
|
|
325
|
-
it('should handle round-trip translation for Android XML', () => {
|
|
326
|
-
const parsed = handler.parse(androidXmlContent);
|
|
327
|
-
// Simulate translation
|
|
328
|
-
const translated = {
|
|
329
|
-
...parsed,
|
|
330
|
-
app_name: 'Mi Aplicación Android',
|
|
331
|
-
welcome_message: '¡Bienvenido a nuestra aplicación!',
|
|
332
|
-
button_text: 'Haz clic aquí',
|
|
333
|
-
formatted_text: 'Este es texto <b>negrita</b>',
|
|
334
|
-
special_chars: 'Caracteres especiales: & < > " \''
|
|
335
|
-
};
|
|
336
|
-
const serialized = handler.serialize(translated);
|
|
337
|
-
const reparsed = handler.parse(serialized);
|
|
338
|
-
expect(reparsed.app_name).toBe('Mi Aplicación Android');
|
|
339
|
-
expect(reparsed.welcome_message).toBe('¡Bienvenido a nuestra aplicación!');
|
|
340
|
-
expect(reparsed.button_text).toBe('Haz clic aquí');
|
|
341
|
-
expect(reparsed.formatted_text).toBe('Este es texto <b>negrita</b>');
|
|
342
|
-
expect(reparsed.special_chars).toBe('Caracteres especiales: & < > " \'');
|
|
343
|
-
});
|
|
344
|
-
it('should validate Android XML structure correctly', () => {
|
|
345
|
-
const validAndroidStructure = {
|
|
346
|
-
resources: {
|
|
347
|
-
string: [
|
|
348
|
-
{ '@_name': 'test_key', '#text': 'Test Value' },
|
|
349
|
-
{ '@_name': 'another_key', '#text': 'Another Value' }
|
|
350
|
-
]
|
|
351
|
-
}
|
|
352
|
-
};
|
|
353
|
-
const result = handler.validateStructure(validAndroidStructure);
|
|
354
|
-
expect(result.isValid).toBe(true);
|
|
355
|
-
expect(result.errors).toHaveLength(0);
|
|
356
|
-
});
|
|
357
|
-
it('should detect invalid Android XML structure', () => {
|
|
358
|
-
const invalidAndroidStructure = {
|
|
359
|
-
// Missing resources root element
|
|
360
|
-
string: [{ '@_name': 'test', '#text': 'Test Value' }]
|
|
361
|
-
};
|
|
362
|
-
const result = handler.validateStructure(invalidAndroidStructure);
|
|
363
|
-
expect(result.isValid).toBe(false);
|
|
364
|
-
expect(result.errors.length).toBeGreaterThan(0);
|
|
365
|
-
expect(result.errors[0].code).toBe('MISSING_RESOURCES');
|
|
366
|
-
});
|
|
367
|
-
});
|
|
368
|
-
describe('enhanced iOS XML support', () => {
|
|
369
|
-
let iosXmlContent;
|
|
370
|
-
beforeAll(() => {
|
|
371
|
-
iosXmlContent = fs.readFileSync(path.join(__dirname, 'test-data', 'ios-strings.xml'), 'utf-8');
|
|
372
|
-
});
|
|
373
|
-
it('should parse iOS plist XML format correctly', () => {
|
|
374
|
-
const result = handler.parse(iosXmlContent);
|
|
375
|
-
expect(result.app_name).toBe('My iOS App');
|
|
376
|
-
expect(result.welcome_message).toBe('Welcome to iOS!');
|
|
377
|
-
expect(result.settings_title).toBe('Settings');
|
|
378
|
-
expect(result.empty_value).toBe('');
|
|
379
|
-
expect(result.special_chars).toBe('Special: & < >');
|
|
380
|
-
});
|
|
381
|
-
it('should preserve iOS XML metadata correctly', () => {
|
|
382
|
-
const result = handler.parse(iosXmlContent);
|
|
383
|
-
expect(result._metadata).toBeDefined();
|
|
384
|
-
expect(result._metadata?.format).toBe('ios-xml');
|
|
385
|
-
expect(result._metadata?.originalStructure).toBeDefined();
|
|
386
|
-
expect(result._metadata?.preserveComments).toBe(true);
|
|
387
|
-
expect(result._metadata?.preserveAttributes).toBe(true);
|
|
388
|
-
// Verify original structure contains plist
|
|
389
|
-
const original = result._metadata?.originalStructure;
|
|
390
|
-
expect(original.plist).toBeDefined();
|
|
391
|
-
expect(original.plist.dict).toBeDefined();
|
|
392
|
-
});
|
|
393
|
-
it('should serialize iOS XML correctly with proper plist structure', () => {
|
|
394
|
-
const translationData = {
|
|
395
|
-
app_name: 'Mi App iOS',
|
|
396
|
-
welcome_message: '¡Bienvenido a iOS!',
|
|
397
|
-
settings_title: 'Configuraciones',
|
|
398
|
-
empty_value: '',
|
|
399
|
-
special_chars: 'Especial: & < >',
|
|
400
|
-
_metadata: {
|
|
401
|
-
format: 'ios-xml',
|
|
402
|
-
originalStructure: {
|
|
403
|
-
plist: {
|
|
404
|
-
'@_version': '1.0',
|
|
405
|
-
dict: {
|
|
406
|
-
key: [
|
|
407
|
-
{ '#text': 'app_name' },
|
|
408
|
-
{ '#text': 'welcome_message' },
|
|
409
|
-
{ '#text': 'settings_title' },
|
|
410
|
-
{ '#text': 'empty_value' },
|
|
411
|
-
{ '#text': 'special_chars' }
|
|
412
|
-
],
|
|
413
|
-
string: [
|
|
414
|
-
{ '#text': 'My iOS App' },
|
|
415
|
-
{ '#text': 'Welcome to iOS!' },
|
|
416
|
-
{ '#text': 'Settings' },
|
|
417
|
-
{ '#text': '' },
|
|
418
|
-
{ '#text': 'Special: & < >' }
|
|
419
|
-
]
|
|
420
|
-
}
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
};
|
|
425
|
-
const result = handler.serialize(translationData);
|
|
426
|
-
expect(result).toContain('<?xml version="1.0" encoding="UTF-8"?>');
|
|
427
|
-
expect(result).toContain('<plist');
|
|
428
|
-
expect(result).toContain('<dict>');
|
|
429
|
-
expect(result).toContain('Mi App iOS');
|
|
430
|
-
expect(result).toContain('¡Bienvenido a iOS!');
|
|
431
|
-
expect(result).toContain('Configuraciones');
|
|
432
|
-
expect(result).toContain('Especial: & < >');
|
|
433
|
-
expect(result).toContain('</dict>');
|
|
434
|
-
expect(result).toContain('</plist>');
|
|
435
|
-
});
|
|
436
|
-
it('should handle round-trip translation for iOS XML', () => {
|
|
437
|
-
const parsed = handler.parse(iosXmlContent);
|
|
438
|
-
// Simulate translation
|
|
439
|
-
const translated = {
|
|
440
|
-
...parsed,
|
|
441
|
-
app_name: 'Mi App iOS',
|
|
442
|
-
welcome_message: '¡Bienvenido a iOS!',
|
|
443
|
-
settings_title: 'Configuraciones',
|
|
444
|
-
special_chars: 'Especial: & < >'
|
|
445
|
-
};
|
|
446
|
-
const serialized = handler.serialize(translated);
|
|
447
|
-
const reparsed = handler.parse(serialized);
|
|
448
|
-
expect(reparsed.app_name).toBe('Mi App iOS');
|
|
449
|
-
expect(reparsed.welcome_message).toBe('¡Bienvenido a iOS!');
|
|
450
|
-
expect(reparsed.settings_title).toBe('Configuraciones');
|
|
451
|
-
expect(reparsed.special_chars).toBe('Especial: & < >');
|
|
452
|
-
});
|
|
453
|
-
it('should validate iOS XML structure correctly', () => {
|
|
454
|
-
const validIosStructure = {
|
|
455
|
-
plist: {
|
|
456
|
-
'@_version': '1.0',
|
|
457
|
-
dict: {
|
|
458
|
-
key: [
|
|
459
|
-
{ '#text': 'test_key' },
|
|
460
|
-
{ '#text': 'another_key' }
|
|
461
|
-
],
|
|
462
|
-
string: [
|
|
463
|
-
{ '#text': 'Test Value' },
|
|
464
|
-
{ '#text': 'Another Value' }
|
|
465
|
-
]
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
};
|
|
469
|
-
const result = handler.validateStructure(validIosStructure);
|
|
470
|
-
expect(result.isValid).toBe(true);
|
|
471
|
-
expect(result.errors).toHaveLength(0);
|
|
472
|
-
});
|
|
473
|
-
it('should detect invalid iOS XML structure', () => {
|
|
474
|
-
const invalidIosStructure = {
|
|
475
|
-
// Missing plist root element
|
|
476
|
-
dict: {
|
|
477
|
-
key: [{ '#text': 'test_key' }],
|
|
478
|
-
string: [{ '#text': 'Test Value' }]
|
|
479
|
-
}
|
|
480
|
-
};
|
|
481
|
-
const result = handler.validateStructure(invalidIosStructure);
|
|
482
|
-
expect(result.isValid).toBe(false);
|
|
483
|
-
expect(result.errors.length).toBeGreaterThan(0);
|
|
484
|
-
expect(result.errors[0].code).toBe('MISSING_PLIST');
|
|
485
|
-
});
|
|
486
|
-
it('should handle iOS XML with missing dict element', () => {
|
|
487
|
-
const invalidIosStructure = {
|
|
488
|
-
plist: {
|
|
489
|
-
'@_version': '1.0'
|
|
490
|
-
// Missing dict element
|
|
491
|
-
}
|
|
492
|
-
};
|
|
493
|
-
const result = handler.validateStructure(invalidIosStructure);
|
|
494
|
-
expect(result.isValid).toBe(false);
|
|
495
|
-
expect(result.errors.length).toBeGreaterThan(0);
|
|
496
|
-
expect(result.errors[0].code).toBe('MISSING_DICT');
|
|
497
|
-
});
|
|
498
|
-
});
|
|
499
|
-
describe('XML formatting and serialization options', () => {
|
|
500
|
-
it('should apply custom indentation to XML output', () => {
|
|
501
|
-
const data = {
|
|
502
|
-
test_key: 'Test Value',
|
|
503
|
-
another_key: 'Another Value'
|
|
504
|
-
};
|
|
505
|
-
const resultWithTabs = handler.serialize(data, { indentation: '\t' });
|
|
506
|
-
const resultWith4Spaces = handler.serialize(data, { indentation: 4 });
|
|
507
|
-
// Both should contain proper XML structure
|
|
508
|
-
expect(resultWithTabs).toContain('<resources>');
|
|
509
|
-
expect(resultWith4Spaces).toContain('<resources>');
|
|
510
|
-
expect(resultWithTabs).toContain('Test Value');
|
|
511
|
-
expect(resultWith4Spaces).toContain('Test Value');
|
|
512
|
-
});
|
|
513
|
-
it('should handle XML declaration option', () => {
|
|
514
|
-
const data = {
|
|
515
|
-
test_key: 'Test Value'
|
|
516
|
-
};
|
|
517
|
-
const resultWithDeclaration = handler.serialize(data, { xmlDeclaration: true });
|
|
518
|
-
const resultWithoutDeclaration = handler.serialize(data, { xmlDeclaration: false });
|
|
519
|
-
expect(resultWithDeclaration).toMatch(/^<\?xml version="1\.0" encoding="UTF-8"\?>/);
|
|
520
|
-
expect(resultWithoutDeclaration).not.toMatch(/^<\?xml/);
|
|
521
|
-
});
|
|
522
|
-
});
|
|
523
|
-
describe('XML error handling and edge cases', () => {
|
|
524
|
-
it('should handle malformed XML gracefully', () => {
|
|
525
|
-
const malformedXml = '<?xml version="1.0"?><resources><string name="test">Unclosed tag';
|
|
526
|
-
expect(() => handler.parse(malformedXml)).toThrow('Failed to parse XML');
|
|
527
|
-
});
|
|
528
|
-
it('should handle XML with no translatable content', () => {
|
|
529
|
-
const emptyXml = '<?xml version="1.0"?><resources></resources>';
|
|
530
|
-
const result = handler.parse(emptyXml);
|
|
531
|
-
const nonMetadataKeys = Object.keys(result).filter(key => !key.startsWith('_'));
|
|
532
|
-
// Empty resources should result in no translatable content
|
|
533
|
-
expect(nonMetadataKeys.length).toBe(0);
|
|
534
|
-
const validation = handler.validateStructure(result);
|
|
535
|
-
expect(validation.isValid).toBe(true);
|
|
536
|
-
expect(validation.warnings.length).toBeGreaterThan(0);
|
|
537
|
-
expect(validation.warnings[0].code).toBe('NO_STRINGS');
|
|
538
|
-
});
|
|
539
|
-
it('should handle XML with complex nested structures', () => {
|
|
540
|
-
const complexXml = `<?xml version="1.0"?>
|
|
541
|
-
<resources>
|
|
542
|
-
<string name="simple">Simple string</string>
|
|
543
|
-
<group name="nested_group">
|
|
544
|
-
<string name="nested_string">Nested string</string>
|
|
545
|
-
<group name="deeply_nested">
|
|
546
|
-
<string name="deep_string">Deep string</string>
|
|
547
|
-
</group>
|
|
548
|
-
</group>
|
|
549
|
-
</resources>`;
|
|
550
|
-
const result = handler.parse(complexXml);
|
|
551
|
-
expect(result.simple).toBe('Simple string');
|
|
552
|
-
expect(result.nested_group).toBeDefined();
|
|
553
|
-
expect(typeof result.nested_group).toBe('object');
|
|
554
|
-
const nestedGroup = result.nested_group;
|
|
555
|
-
expect(nestedGroup.nested_string).toBe('Nested string');
|
|
556
|
-
});
|
|
557
|
-
it('should preserve XML attributes and special elements', () => {
|
|
558
|
-
const xmlWithAttributes = `<?xml version="1.0"?>
|
|
559
|
-
<resources xmlns:android="http://schemas.android.com/apk/res/android">
|
|
560
|
-
<string name="app_name" translatable="false">My App</string>
|
|
561
|
-
<string name="welcome">Welcome!</string>
|
|
562
|
-
</resources>`;
|
|
563
|
-
const result = handler.parse(xmlWithAttributes);
|
|
564
|
-
expect(result.app_name).toBe('My App');
|
|
565
|
-
expect(result.welcome).toBe('Welcome!');
|
|
566
|
-
expect(result._metadata?.originalStructure).toBeDefined();
|
|
567
|
-
expect(result._metadata?.preserveAttributes).toBe(true);
|
|
568
|
-
});
|
|
569
|
-
});
|
|
570
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|