/
xliff2_translation_serializer_spec.ts
146 lines (143 loc) · 6.77 KB
/
xliff2_translation_serializer_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
/**
* @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 {absoluteFrom} from '@angular/compiler-cli/src/ngtsc/file_system';
import {runInEachFileSystem} from '@angular/compiler-cli/src/ngtsc/file_system/testing';
import {ɵParsedMessage, ɵSourceLocation} from '@angular/localize';
import {Xliff2TranslationSerializer} from '../../../src/extract/translation_files/xliff2_translation_serializer';
import {mockMessage} from './mock_message';
runInEachFileSystem(() => {
describe('Xliff2TranslationSerializer', () => {
[false, true].forEach(useLegacyIds => {
describe(`renderFile() [using ${useLegacyIds ? 'legacy' : 'canonical'} ids]`, () => {
it('should convert a set of parsed messages into an XML string', () => {
const phLocation: ɵSourceLocation = {
start: {line: 0, column: 10},
end: {line: 1, column: 15},
file: absoluteFrom('/project/file.ts'),
text: 'placeholder + 1'
};
const messagePartLocation: ɵSourceLocation = {
start: {line: 0, column: 5},
end: {line: 0, column: 10},
file: absoluteFrom('/project/file.ts'),
text: 'message part'
};
const messages: ɵParsedMessage[] = [
mockMessage('12345', ['a', 'b', 'c'], ['PH', 'PH_1'], {
meaning: 'some meaning',
location: {
file: absoluteFrom('/project/file.ts'),
start: {line: 5, column: 0},
end: {line: 5, column: 3}
},
legacyIds: ['1234567890ABCDEF1234567890ABCDEF12345678', '615790887472569365'],
}),
mockMessage('54321', ['a', 'b', 'c'], ['PH', 'PH_1'], {
customId: 'someId',
legacyIds: ['87654321FEDCBA0987654321FEDCBA0987654321', '563965274788097516'],
messagePartLocations: [undefined, messagePartLocation, undefined],
substitutionLocations: {'PH': phLocation, 'PH_1': undefined},
}),
mockMessage('67890', ['a', '', 'c'], ['START_TAG_SPAN', 'CLOSE_TAG_SPAN'], {
description: 'some description',
location: {
file: absoluteFrom('/project/file.ts'),
start: {line: 2, column: 7},
end: {line: 3, column: 2}
}
}),
mockMessage('13579', ['', 'b', ''], ['START_BOLD_TEXT', 'CLOSE_BOLD_TEXT'], {}),
mockMessage('24680', ['a'], [], {meaning: 'meaning', description: 'and description'}),
mockMessage('80808', ['multi\nlines'], [], {}),
mockMessage('90000', ['<escape', 'me>'], ['double-quotes-"'], {}),
mockMessage(
'100000',
[
'pre-ICU {VAR_SELECT, select, a {a} b {{INTERPOLATION}} c {pre {INTERPOLATION_1} post}} post-ICU'
],
[], {}),
mockMessage(
'100001',
[
'{VAR_PLURAL, plural, one {{START_BOLD_TEXT}something bold{CLOSE_BOLD_TEXT}} other {pre {START_TAG_SPAN}middle{CLOSE_TAG_SPAN} post}}'
],
[], {}),
];
const serializer =
new Xliff2TranslationSerializer('xx', absoluteFrom('/project'), useLegacyIds);
const output = serializer.serialize(messages);
expect(output).toEqual([
`<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="xx">`,
` <file id="ngi18n" original="ng.template">`,
` <unit id="${useLegacyIds ? '615790887472569365' : '12345'}">`,
` <notes>`,
` <note category="location">file.ts:6</note>`,
` <note category="meaning">some meaning</note>`,
` </notes>`,
` <segment>`,
` <source xml:space="preserve">a<ph id="0" equiv="PH"/>b<ph id="1" equiv="PH_1"/>c</source>`,
` </segment>`,
` </unit>`,
` <unit id="someId">`,
` <segment>`,
` <source xml:space="preserve">a<ph id="0" equiv="PH" disp="placeholder + 1"/>b<ph id="1" equiv="PH_1"/>c</source>`,
` </segment>`,
` </unit>`,
` <unit id="67890">`,
` <notes>`,
` <note category="location">file.ts:3,4</note>`,
` <note category="description">some description</note>`,
` </notes>`,
` <segment>`,
` <source xml:space="preserve">a<pc id="0" equivStart="START_TAG_SPAN" equivEnd="CLOSE_TAG_SPAN"></pc>c</source>`,
` </segment>`,
` </unit>`,
` <unit id="13579">`,
` <segment>`,
` <source xml:space="preserve"><pc id="0" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT">b</pc></source>`,
` </segment>`,
` </unit>`,
` <unit id="24680">`,
` <notes>`,
` <note category="description">and description</note>`,
` <note category="meaning">meaning</note>`,
` </notes>`,
` <segment>`,
` <source xml:space="preserve">a</source>`,
` </segment>`,
` </unit>`,
` <unit id="80808">`,
` <segment>`,
` <source xml:space="preserve">multi`,
`lines</source>`,
` </segment>`,
` </unit>`,
` <unit id="90000">`,
` <segment>`,
` <source xml:space="preserve"><escape<ph id="0" equiv="double-quotes-""/>me></source>`,
` </segment>`,
` </unit>`,
` <unit id="100000">`,
` <segment>`,
` <source xml:space="preserve">pre-ICU {VAR_SELECT, select, a {a} b {<ph id="0" equiv="INTERPOLATION"/>} c {pre <ph id="1" equiv="INTERPOLATION_1"/> post}} post-ICU</source>`,
` </segment>`,
` </unit>`,
` <unit id="100001">`,
` <segment>`,
` <source xml:space="preserve">{VAR_PLURAL, plural, one {<pc id="0" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT">something bold</pc>} other {pre <pc id="1" equivStart="START_TAG_SPAN" equivEnd="CLOSE_TAG_SPAN">middle</pc> post}}</source>`,
` </segment>`,
` </unit>`,
` </file>`,
`</xliff>\n`,
].join('\n'));
});
});
});
});
});