forked from angular/angular
/
xliff2_translation_parser_spec.ts
694 lines (661 loc) · 35.6 KB
/
xliff2_translation_parser_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ɵcomputeMsgId, ɵmakeParsedTranslation} from '@angular/localize';
import {ParseAnalysis, ParsedTranslationBundle} from '../../../../src/translate/translation_files/translation_parsers/translation_parser';
import {Xliff2TranslationParser} from '../../../../src/translate/translation_files/translation_parsers/xliff2_translation_parser';
describe(
'Xliff2TranslationParser', () => {
describe('canParse()', () => {
it('should return true if the file contains an <xliff> element with version="2.0" attribute',
() => {
const parser = new Xliff2TranslationParser();
expect(parser.canParse(
'/some/file.xlf',
'<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0">'))
.toBeTruthy();
expect(parser.canParse(
'/some/file.json',
'<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0">'))
.toBeTruthy();
expect(parser.canParse('/some/file.xliff', '<xliff version="2.0">')).toBeTruthy();
expect(parser.canParse('/some/file.json', '<xliff version="2.0">')).toBeTruthy();
expect(parser.canParse('/some/file.xlf', '<xliff>')).toBe(false);
expect(parser.canParse('/some/file.xlf', '<xliff version="1.2">')).toBe(false);
expect(parser.canParse('/some/file.xlf', '')).toBe(false);
expect(parser.canParse('/some/file.json', '')).toBe(false);
});
});
describe('analyze', () => {
it('should return a success object if the file contains an <xliff> element with version="2.0" attribute',
() => {
const parser = new Xliff2TranslationParser();
expect(parser.analyze(
'/some/file.xlf',
'<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0">'))
.toEqual(jasmine.objectContaining({canParse: true, hint: jasmine.any(Object)}));
expect(parser.analyze(
'/some/file.json',
'<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0">'))
.toEqual(jasmine.objectContaining({canParse: true, hint: jasmine.any(Object)}));
expect(parser.analyze('/some/file.xliff', '<xliff version="2.0">'))
.toEqual(jasmine.objectContaining({canParse: true, hint: jasmine.any(Object)}));
expect(parser.analyze('/some/file.json', '<xliff version="2.0">'))
.toEqual(jasmine.objectContaining({canParse: true, hint: jasmine.any(Object)}));
});
it('should return a failure object if the file cannot be parsed as XLIFF 2.0', () => {
const parser = new Xliff2TranslationParser();
expect(parser.analyze('/some/file.xlf', '<xliff>')).toEqual(jasmine.objectContaining({
canParse: false
}));
expect(parser.analyze('/some/file.xlf', '<xliff version="1.2">'))
.toEqual(jasmine.objectContaining({canParse: false}));
expect(parser.analyze('/some/file.xlf', '')).toEqual(jasmine.objectContaining({
canParse: false
}));
expect(parser.analyze('/some/file.json', '')).toEqual(jasmine.objectContaining({
canParse: false
}));
});
it('should return a diagnostics object when the file is not a valid format', () => {
let result: ParseAnalysis<any>;
const parser = new Xliff2TranslationParser();
result = parser.analyze('/some/file.xlf', '<moo>');
expect(result.diagnostics.messages).toEqual([
{type: 'warning', message: 'The XML file does not contain a <xliff> root node.'}
]);
result = parser.analyze('/some/file.xlf', '<xliff version="1.2">');
expect(result.diagnostics.messages).toEqual([{
type: 'warning',
message:
'The <xliff> node does not have the required attribute: version="2.0". ("[WARNING ->]<xliff version="1.2">"): /some/file.xlf@0:0'
}]);
result = parser.analyze('/some/file.xlf', '<xliff version="2.0"></file>');
expect(result.diagnostics.messages).toEqual([{
type: 'error',
message:
'Unexpected closing tag "file". It may happen when the tag has already been closed by another tag. For more info see https://www.w3.org/TR/html5/syntax.html#closing-elements-that-have-implied-end-tags ("<xliff version="2.0">[ERROR ->]</file>"): /some/file.xlf@0:21'
}]);
});
});
for (const withHint of [true, false]) {
describe(
`parse() [${withHint ? 'with' : 'without'} hint]`, () => {
const doParse: (fileName: string, XLIFF: string) => ParsedTranslationBundle =
withHint ? (fileName, XLIFF) => {
const parser = new Xliff2TranslationParser();
const hint = parser.canParse(fileName, XLIFF);
if (!hint) {
throw new Error('expected XLIFF to be valid');
}
return parser.parse(fileName, XLIFF, hint);
} : (fileName, XLIFF) => {
const parser = new Xliff2TranslationParser();
return parser.parse(fileName, XLIFF);
};
const expectToFail:
(fileName: string, XLIFF: string, errorMatcher: RegExp,
diagnosticMessage: string) => void =
withHint ? (fileName, XLIFF, _errorMatcher, diagnosticMessage) => {
const result = doParse(fileName, XLIFF);
expect(result.diagnostics.messages.length).toEqual(1);
expect(result.diagnostics.messages[0].message).toEqual(diagnosticMessage);
} : (fileName, XLIFF, errorMatcher, _diagnosticMessage) => {
expect(() => doParse(fileName, XLIFF)).toThrowError(errorMatcher);
};
it('should extract the locale from the file contents', () => {
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(result.locale).toEqual('fr');
});
it('should return undefined locale if there is no locale in the file', () => {
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en">`,
` <file original="ng.template" id="ngi18n">`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(result.locale).toBeUndefined();
});
it('should extract basic messages', () => {
/**
* Source HTML:
*
* ```
* <div i18n>translatable attribute</div>
* ```
*/
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="1933478729560469763">`,
` <notes>`,
` <note category="location">file.ts:2</note>`,
` </notes>`,
` <segment>`,
` <source>translatable attribute</source>`,
` <target>etubirtta elbatalsnart</target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(result.translations[ɵcomputeMsgId('translatable attribute', '')])
.toEqual(ɵmakeParsedTranslation(['etubirtta elbatalsnart']));
});
it('should extract translations with simple placeholders', () => {
/**
* Source HTML:
*
* ```
* <div i18n>translatable element <b>with placeholders</b> {{ interpolation}}</div>
* ```
*/
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="6949438802869886378">`,
` <notes>`,
` <note category="location">file.ts:3</note>`,
` </notes>`,
` <segment>`,
` <source>translatable element <pc id="0" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT" type="fmt" dispStart="<b>" dispEnd="</b>">with placeholders</pc> <ph id="1" equiv="INTERPOLATION" disp="{{ interpolation}}"/></source>`,
` <target><ph id="1" equiv="INTERPOLATION" disp="{{ interpolation}}"/> tnemele elbatalsnart <pc id="0" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT" type="fmt" dispStart="<b>" dispEnd="</b>">sredlohecalp htiw</pc></target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(
result.translations[ɵcomputeMsgId(
'translatable element {$START_BOLD_TEXT}with placeholders{$CLOSE_BOLD_TEXT} {$INTERPOLATION}')])
.toEqual(ɵmakeParsedTranslation(
['', ' tnemele elbatalsnart ', 'sredlohecalp htiw', ''],
['INTERPOLATION', 'START_BOLD_TEXT', 'CLOSE_BOLD_TEXT']));
});
it('should extract nested placeholder containers (i.e. nested HTML elements)', () => {
/**
* Source HTML:
*
* ```
* <div i18n>
* translatable <span>element <b>with placeholders</b></span> {{ interpolation}}
* </div>
* ```
*/
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="9051630253697141670">`,
` <notes>`,
` <note category="location">file.ts:3</note>`,
` </notes>`,
` <segment>`,
` <source>translatable <pc id="0" equivStart="START_TAG_SPAN" equivEnd="CLOSE_TAG_SPAN" type="other"` +
` dispStart="<span>" dispEnd="</span>">element <pc id="1" equivStart="START_BOLD_TEXT" equivEnd=` +
`"CLOSE_BOLD_TEXT" type="fmt" dispStart="<b>" dispEnd="</b>">with placeholders</pc></pc>` +
` <ph id="2" equiv="INTERPOLATION" disp="{{ interpolation}}"/></source>`,
` <target><pc id="0" equivStart="START_TAG_SPAN" equivEnd="CLOSE_TAG_SPAN" type="fmt" dispStart="<` +
`span>" dispEnd="</span>"><ph id="2" equiv="INTERPOLATION" disp="{{ interpolation}}"/> tnemele</pc>` +
` elbatalsnart <pc id="1" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT" type="fmt" dispStart=` +
`"<b>" dispEnd="</b>">sredlohecalp htiw</pc></target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(
result.translations[ɵcomputeMsgId(
'translatable {$START_TAG_SPAN}element {$START_BOLD_TEXT}with placeholders' +
'{$CLOSE_BOLD_TEXT}{$CLOSE_TAG_SPAN} {$INTERPOLATION}')])
.toEqual(ɵmakeParsedTranslation(
['', '', ' tnemele', ' elbatalsnart ', 'sredlohecalp htiw', ''], [
'START_TAG_SPAN',
'INTERPOLATION',
'CLOSE_TAG_SPAN',
'START_BOLD_TEXT',
'CLOSE_BOLD_TEXT',
]));
});
it('should extract translations with simple ICU expressions', () => {
/**
* Source HTML:
*
* ```
* <div i18n>{VAR_PLURAL, plural, =0 {<p>test</p>} }</div>
* ```
*/
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="2874455947211586270">`,
` <notes>`,
` <note category="location">file.ts:4</note>`,
` </notes>`,
` <segment>`,
` <source>{VAR_PLURAL, plural, =0 {<pc id="0" equivStart="START_PARAGRAPH" equivEnd="CLOSE_PARAGRAPH" type="other" dispStart="<p>" dispEnd="</p>">test</pc>} }</source>`,
` <target>{VAR_PLURAL, plural, =0 {<pc id="0" equivStart="START_PARAGRAPH" equivEnd="CLOSE_PARAGRAPH" type="other" dispStart="<p>" dispEnd="</p>">TEST</pc>} }</target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(result.translations[ɵcomputeMsgId(
'{VAR_PLURAL, plural, =0 {{START_PARAGRAPH}test{CLOSE_PARAGRAPH}}}')])
.toEqual(ɵmakeParsedTranslation(
['{VAR_PLURAL, plural, =0 {{START_PARAGRAPH}TEST{CLOSE_PARAGRAPH}}}'], []));
});
it('should extract translations with duplicate source messages', () => {
/**
* Source HTML:
*
* ```
* <div i18n>foo</div>
* <div i18n="m|d@@i">foo</div>
* <div i18=""m|d@@bar>foo</div>
* ```
*/
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="9205907420411818817">`,
` <notes>`,
` <note category="description">d</note>`,
` <note category="meaning">m</note>`,
` <note category="location">file.ts:5</note>`,
` </notes>`,
` <segment>`,
` <source>foo</source>`,
` <target>oof</target>`,
` </segment>`,
` </unit>`,
` <unit id="i">`,
` <notes>`,
` <note category="description">d</note>`,
` <note category="meaning">m</note>`,
` <note category="location">file.ts:5</note>`,
` </notes>`,
` <segment>`,
` <source>foo</source>`,
` <target>toto</target>`,
` </segment>`,
` </unit>`,
` <unit id="bar">`,
` <notes>`,
` <note category="description">d</note>`,
` <note category="meaning">m</note>`,
` <note category="location">file.ts:5</note>`,
` </notes>`,
` <segment>`,
` <source>foo</source>`,
` <target>tata</target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(result.translations[ɵcomputeMsgId('foo')])
.toEqual(ɵmakeParsedTranslation(['oof']));
expect(result.translations['i']).toEqual(ɵmakeParsedTranslation(['toto']));
expect(result.translations['bar']).toEqual(ɵmakeParsedTranslation(['tata']));
});
it('should extract translations with only placeholders, which are re-ordered', () => {
/**
* Source HTML:
*
* ```
* <div i18n><br><img/><img/></div>
* ```
*/
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="7118057989405618448">`,
` <notes>`,
` <note category="description">ph names</note>`,
` <note category="location">file.ts:7</note>`,
` </notes>`,
` <segment>`,
` <source><ph id="0" equiv="LINE_BREAK" type="fmt" disp="<br/>"/><ph id="1" equiv="TAG_IMG" type="image" disp="<img/>"/><ph id="2" equiv="TAG_IMG_1" type="image" disp="<img/>"/></source>`,
` <target><ph id="2" equiv="TAG_IMG_1" type="image" disp="<img/>"/><ph id="1" equiv="TAG_IMG" type="image" disp="<img/>"/><ph id="0" equiv="LINE_BREAK" type="fmt" disp="<br/>"/></target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(result.translations[ɵcomputeMsgId('{$LINE_BREAK}{$TAG_IMG}{$TAG_IMG_1}')])
.toEqual(ɵmakeParsedTranslation(
['', '', '', ''], ['TAG_IMG_1', 'TAG_IMG', 'LINE_BREAK']));
});
it('should extract translations with empty target', () => {
/**
* Source HTML:
*
* ```
* <div i18n>hello <span></span></div>
* ```
*/
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="2826198357052921524">`,
` <notes>`,
` <note category="description">empty element</note>`,
` <note category="location">file.ts:8</note>`,
` </notes>`,
` <segment>`,
` <source>hello <pc id="0" equivStart="START_TAG_SPAN" equivEnd="CLOSE_TAG_SPAN" type="other" dispStart="<span>" dispEnd="</span>"></pc></source>`,
` <target></target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(
result.translations[ɵcomputeMsgId('hello {$START_TAG_SPAN}{$CLOSE_TAG_SPAN}')])
.toEqual(ɵmakeParsedTranslation(['']));
});
it('should extract translations with deeply nested ICUs', () => {
/**
* Source HTML:
*
* ```
* Test: { count, plural, =0 { { sex, select, other {<p>deeply nested</p>}} }
* =other {a lot}}
* ```
*
* Note that the message gets split into two translation units:
* * The first one contains the outer message with an `ICU` placeholder
* * The second one is the ICU expansion itself
*
* Note that special markers `VAR_PLURAL` and `VAR_SELECT` are added, which are then
* replaced by IVY at runtime with the actual values being rendered by the ICU
* expansion.
*/
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="980940425376233536">`,
` <notes>`,
` <note category="location">file.ts:10</note>`,
` </notes>`,
` <segment>`,
` <source>Test: <ph id="0" equiv="ICU" disp="{ count, plural, =0 {...} =other {...}}"/></source>`,
` <target>Le test: <ph id="0" equiv="ICU" disp="{ count, plural, =0 {...} =other {...}}"/></target>`,
` </segment>`,
` </unit>`,
` <unit id="5207293143089349404">`,
` <notes>`,
` <note category="location">file.ts:10</note>`,
` </notes>`,
` <segment>`,
` <source>{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {<pc id="0" equivStart="START_PARAGRAPH" equivEnd="CLOSE_PARAGRAPH" type="other" dispStart="<p>" dispEnd="</p>">deeply nested</pc>}}} =other {a lot}}</source>`,
` <target>{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {<pc id="0" equivStart="START_PARAGRAPH" equivEnd="CLOSE_PARAGRAPH" type="other" dispStart="<p>" dispEnd="</p>">profondément imbriqué</pc>}}} =other {beaucoup}}</target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(result.translations[ɵcomputeMsgId('Test: {$ICU}')])
.toEqual(ɵmakeParsedTranslation(['Le test: ', ''], ['ICU']));
expect(
result.translations[ɵcomputeMsgId(
'{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {{START_PARAGRAPH}deeply nested{CLOSE_PARAGRAPH}}}} =other {beaucoup}}')])
.toEqual(ɵmakeParsedTranslation([
'{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {{START_PARAGRAPH}profondément imbriqué{CLOSE_PARAGRAPH}}}} =other {beaucoup}}'
]));
});
it('should extract translations containing multiple lines', () => {
/**
* Source HTML:
*
* ```
* <div i18n>multi
* lines</div>
* ```
*/
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="2340165783990709777">`,
` <notes>`,
` <note category="location">file.ts:11,12</note>`,
` </notes>`,
` <segment>`,
` <source>multi\nlines</source>`,
` <target>multi\nlignes</target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(result.translations[ɵcomputeMsgId('multi\nlines')])
.toEqual(ɵmakeParsedTranslation(['multi\nlignes']));
});
it('should extract translations with <mrk> elements', () => {
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="mrk-test">`,
` <segment>`,
` <source>First sentence.</source>`,
` <target>Translated <mrk id="m1" type="comment" ref="#n1">first sentence</mrk>.</target>`,
` </segment>`,
` </unit>`,
` <unit id="mrk-test2">`,
` <segment>`,
` <source>First sentence. Second sentence.</source>`,
` <target>Translated <mrk id="m1" type="comment" ref="#n1"><mrk id="m2" type="comment" ref="#n1">first</mrk> sentence</mrk>.</target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(result.translations['mrk-test'])
.toEqual(ɵmakeParsedTranslation(['Translated first sentence.']));
expect(result.translations['mrk-test2'])
.toEqual(ɵmakeParsedTranslation(['Translated first sentence.']));
});
it('should merge messages from each `<file>` element', () => {
/**
* Source HTML:
*
* ```
* <div i18n>translatable attribute</div>
* ```
*
* ```
* <div i18n>translatable element <b>with placeholders</b> {{ interpolation}}</div>
* ```
*/
const XLIFF = [
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="file-1">`,
` <unit id="1933478729560469763">`,
` <notes>`,
` <note category="location">file.ts:2</note>`,
` </notes>`,
` <segment>`,
` <source>translatable attribute</source>`,
` <target>etubirtta elbatalsnart</target>`,
` </segment>`,
` </unit>`,
` </file>`,
` <file original="ng.template" id="file-2">`,
` <unit id="5057824347511785081">`,
` <notes>`,
` <note category="location">file.ts:3</note>`,
` </notes>`,
` <segment>`,
` <source>translatable element <pc id="0" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT" type="fmt" dispStart="<b>" dispEnd="</b>">with placeholders</pc> <ph id="1" equiv="INTERPOLATION" disp="{{ interpolation}}"/></source>`,
` <target><ph id="1" equiv="INTERPOLATION" disp="{{ interpolation}}"/> tnemele elbatalsnart <pc id="0" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT" type="fmt" dispStart="<b>" dispEnd="</b>">sredlohecalp htiw</pc></target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
const result = doParse('/some/file.xlf', XLIFF);
expect(result.translations[ɵcomputeMsgId('translatable attribute', '')])
.toEqual(ɵmakeParsedTranslation(['etubirtta elbatalsnart']));
expect(
result.translations[ɵcomputeMsgId(
'translatable element {$START_BOLD_TEXT}with placeholders{$LOSE_BOLD_TEXT} {$INTERPOLATION}')])
.toEqual(ɵmakeParsedTranslation(
['', ' tnemele elbatalsnart ', 'sredlohecalp htiw', ''],
['INTERPOLATION', 'START_BOLD_TEXT', 'CLOSE_BOLD_TEXT']));
});
describe('[structure errors]', () => {
it('should provide a diagnostic error when a trans-unit has no translation', () => {
const XLIFF = [
`<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="missingtarget">`,
` <segment>`,
` <source/>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
expectToFail('/some/file.xlf', XLIFF, /Missing required <target> element/, [
`Missing required <target> element ("`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="missingtarget">`,
` [ERROR ->]<segment>`,
` <source/>`,
` </segment>`,
`"): /some/file.xlf@4:6`,
].join('\n'));
});
it('should provide a diagnostic error when a trans-unit has no id attribute', () => {
const XLIFF = [
`<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit>`,
` <segment>`,
` <source/>`,
` <target/>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
expectToFail('/some/file.xlf', XLIFF, /Missing required "id" attribute/, [
`Missing required "id" attribute on <trans-unit> element. ("s:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` [ERROR ->]<unit>`,
` <segment>`,
` <source/>`,
`"): /some/file.xlf@3:4`,
].join('\n'));
});
it('should provide a diagnostic error on duplicate trans-unit id', () => {
const XLIFF = [
`<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="deadbeef">`,
` <segment>`,
` <source/>`,
` <target/>`,
` </segment>`,
` </unit>`,
` <unit id="deadbeef">`,
` <segment>`,
` <source/>`,
` <target/>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
expectToFail(
'/some/file.xlf', XLIFF, /Duplicated translations for message "deadbeef"/, [
`Duplicated translations for message "deadbeef" ("`,
` </segment>`,
` </unit>`,
` [ERROR ->]<unit id="deadbeef">`,
` <segment>`,
` <source/>`,
'"): /some/file.xlf@9:4',
].join('\n'));
});
});
describe('[message errors]', () => {
it('should provide a diagnostic error on unknown message tags', () => {
const XLIFF = [
`<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="deadbeef">`,
` <segment>`,
` <source/>`,
` <target><b>msg should contain only ph and pc tags</b></target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
expectToFail('/some/file.xlf', XLIFF, /Invalid element found in message/, [
`Error: Invalid element found in message.`,
`At /some/file.xlf@6:16:`,
`...`,
` <source/>`,
` <target>[ERROR ->]<b>msg should contain only ph and pc tags</b></target>`,
` </segment>`,
`...`,
``,
].join('\n'));
});
it('should provide a diagnostic error when a placeholder misses an id attribute',
() => {
const XLIFF = [
`<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en" trgLang="fr">`,
` <file original="ng.template" id="ngi18n">`,
` <unit id="deadbeef">`,
` <segment>`,
` <source/>`,
` <target><ph/></target>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>`,
].join('\n');
expectToFail('/some/file.xlf', XLIFF, /Missing required "equiv" attribute/, [
`Error: Missing required "equiv" attribute:`,
`At /some/file.xlf@6:16:`,
`...`,
` <source/>`,
` <target>[ERROR ->]<ph/></target>`,
` </segment>`,
`...`,
``,
].join('\n'));
});
});
});
}
});