Skip to content

Commit f0af387

Browse files
petebacondarwinmhevery
authored andcommitted
fix(localize): ensure required XLIFF parameters are serialized (#38575)
When extracting i18n messages from source code, the XLIFF serializers were missing some required attributes on the `<file>` element. This commit re-introduces the `original` property to each of XLIFF 1.2 and 2.0 serializers. Also it adds in the required `id` property for the XLIFF 2.0 seralizer. Fixes #38570 PR Close #38575
1 parent 14e90be commit f0af387

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

packages/localize/src/tools/src/extract/translation_files/xliff1_translation_serializer.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ export class Xliff1TranslationSerializer implements TranslationSerializer {
3232
const ids = new Set<string>();
3333
const xml = new XmlFile();
3434
xml.startTag('xliff', {'version': '1.2', 'xmlns': 'urn:oasis:names:tc:xliff:document:1.2'});
35-
xml.startTag('file', {'source-language': this.sourceLocale, 'datatype': 'plaintext'});
35+
// NOTE: the `original` property is set to the legacy `ng2.template` value for backward
36+
// compatibility.
37+
// We could compute the file from the `message.location` property, but there could
38+
// be multiple values for this in the collection of `messages`. In that case we would probably
39+
// need to change the serializer to output a new `<file>` element for each collection of
40+
// messages that come from a particular original file, and the translation file parsers may not
41+
// be able to cope with this.
42+
xml.startTag('file', {
43+
'source-language': this.sourceLocale,
44+
'datatype': 'plaintext',
45+
'original': 'ng2.template',
46+
});
3647
xml.startTag('body');
3748
for (const message of messages) {
3849
const id = this.getMessageId(message);

packages/localize/src/tools/src/extract/translation_files/xliff2_translation_serializer.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ export class Xliff2TranslationSerializer implements TranslationSerializer {
3636
'xmlns': 'urn:oasis:names:tc:xliff:document:2.0',
3737
'srcLang': this.sourceLocale
3838
});
39-
xml.startTag('file');
39+
// NOTE: the `original` property is set to the legacy `ng.template` value for backward
40+
// compatibility.
41+
// We could compute the file from the `message.location` property, but there could
42+
// be multiple values for this in the collection of `messages`. In that case we would probably
43+
// need to change the serializer to output a new `<file>` element for each collection of
44+
// messages that come from a particular original file, and the translation file parsers may not
45+
xml.startTag('file', {'id': 'ngi18n', 'original': 'ng.template'});
4046
for (const message of messages) {
4147
const id = this.getMessageId(message);
4248
if (ids.has(id)) {

packages/localize/src/tools/test/extract/integration/main_spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ runInEachFileSystem(() => {
149149
expect(fs.readFile(outputPath)).toEqual([
150150
`<?xml version="1.0" encoding="UTF-8" ?>`,
151151
`<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">`,
152-
` <file source-language="en-CA" datatype="plaintext">`,
152+
` <file source-language="en-CA" datatype="plaintext" original="ng2.template">`,
153153
` <body>`,
154154
` <trans-unit id="3291030485717846467" datatype="html">`,
155155
` <source>Hello, <x id="PH" equiv-text="name"/>!</source>`,
@@ -218,7 +218,7 @@ runInEachFileSystem(() => {
218218
expect(fs.readFile(outputPath)).toEqual([
219219
`<?xml version="1.0" encoding="UTF-8" ?>`,
220220
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en-AU">`,
221-
` <file>`,
221+
` <file id="ngi18n" original="ng.template">`,
222222
` <unit id="3291030485717846467">`,
223223
` <segment>`,
224224
` <source>Hello, <ph id="0" equiv="PH" disp="name"/>!</source>`,
@@ -276,7 +276,7 @@ runInEachFileSystem(() => {
276276
expect(fs.readFile(outputPath)).toEqual([
277277
`<?xml version="1.0" encoding="UTF-8" ?>`,
278278
`<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">`,
279-
` <file source-language="en-CA" datatype="plaintext">`,
279+
` <file source-language="en-CA" datatype="plaintext" original="ng2.template">`,
280280
` <body>`,
281281
` <trans-unit id="157258427077572998" datatype="html">`,
282282
` <source>Message in <x id="a-file" equiv-text="file"/>!</source>`,

packages/localize/src/tools/test/extract/translation_files/xliff1_translation_serializer_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ runInEachFileSystem(() => {
7272
expect(output).toEqual([
7373
`<?xml version="1.0" encoding="UTF-8" ?>`,
7474
`<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">`,
75-
` <file source-language="xx" datatype="plaintext">`,
75+
` <file source-language="xx" datatype="plaintext" original="ng2.template">`,
7676
` <body>`,
7777
` <trans-unit id="${
7878
useLegacyIds ? '1234567890ABCDEF1234567890ABCDEF12345678' :

packages/localize/src/tools/test/extract/translation_files/xliff2_translation_serializer_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ runInEachFileSystem(() => {
7777
expect(output).toEqual([
7878
`<?xml version="1.0" encoding="UTF-8" ?>`,
7979
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="xx">`,
80-
` <file>`,
80+
` <file id="ngi18n" original="ng.template">`,
8181
` <unit id="${useLegacyIds ? '615790887472569365' : '12345'}">`,
8282
` <notes>`,
8383
` <note category="location">file.ts:6</note>`,

0 commit comments

Comments
 (0)