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
This commit is contained in:
Pete Bacon Darwin 2020-08-25 13:51:06 +01:00 committed by Misko Hevery
parent 14e90bef58
commit f0af387f6c
5 changed files with 24 additions and 7 deletions

View File

@ -32,7 +32,18 @@ export class Xliff1TranslationSerializer implements TranslationSerializer {
const ids = new Set<string>(); const ids = new Set<string>();
const xml = new XmlFile(); const xml = new XmlFile();
xml.startTag('xliff', {'version': '1.2', 'xmlns': 'urn:oasis:names:tc:xliff:document:1.2'}); xml.startTag('xliff', {'version': '1.2', 'xmlns': 'urn:oasis:names:tc:xliff:document:1.2'});
xml.startTag('file', {'source-language': this.sourceLocale, 'datatype': 'plaintext'}); // NOTE: the `original` property is set to the legacy `ng2.template` value for backward
// compatibility.
// We could compute the file from the `message.location` property, but there could
// be multiple values for this in the collection of `messages`. In that case we would probably
// need to change the serializer to output a new `<file>` element for each collection of
// messages that come from a particular original file, and the translation file parsers may not
// be able to cope with this.
xml.startTag('file', {
'source-language': this.sourceLocale,
'datatype': 'plaintext',
'original': 'ng2.template',
});
xml.startTag('body'); xml.startTag('body');
for (const message of messages) { for (const message of messages) {
const id = this.getMessageId(message); const id = this.getMessageId(message);

View File

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

View File

@ -149,7 +149,7 @@ runInEachFileSystem(() => {
expect(fs.readFile(outputPath)).toEqual([ expect(fs.readFile(outputPath)).toEqual([
`<?xml version="1.0" encoding="UTF-8" ?>`, `<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">`, `<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">`,
` <file source-language="en-CA" datatype="plaintext">`, ` <file source-language="en-CA" datatype="plaintext" original="ng2.template">`,
` <body>`, ` <body>`,
` <trans-unit id="3291030485717846467" datatype="html">`, ` <trans-unit id="3291030485717846467" datatype="html">`,
` <source>Hello, <x id="PH" equiv-text="name"/>!</source>`, ` <source>Hello, <x id="PH" equiv-text="name"/>!</source>`,
@ -218,7 +218,7 @@ runInEachFileSystem(() => {
expect(fs.readFile(outputPath)).toEqual([ expect(fs.readFile(outputPath)).toEqual([
`<?xml version="1.0" encoding="UTF-8" ?>`, `<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en-AU">`, `<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="en-AU">`,
` <file>`, ` <file id="ngi18n" original="ng.template">`,
` <unit id="3291030485717846467">`, ` <unit id="3291030485717846467">`,
` <segment>`, ` <segment>`,
` <source>Hello, <ph id="0" equiv="PH" disp="name"/>!</source>`, ` <source>Hello, <ph id="0" equiv="PH" disp="name"/>!</source>`,
@ -276,7 +276,7 @@ runInEachFileSystem(() => {
expect(fs.readFile(outputPath)).toEqual([ expect(fs.readFile(outputPath)).toEqual([
`<?xml version="1.0" encoding="UTF-8" ?>`, `<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">`, `<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">`,
` <file source-language="en-CA" datatype="plaintext">`, ` <file source-language="en-CA" datatype="plaintext" original="ng2.template">`,
` <body>`, ` <body>`,
` <trans-unit id="157258427077572998" datatype="html">`, ` <trans-unit id="157258427077572998" datatype="html">`,
` <source>Message in <x id="a-file" equiv-text="file"/>!</source>`, ` <source>Message in <x id="a-file" equiv-text="file"/>!</source>`,

View File

@ -72,7 +72,7 @@ runInEachFileSystem(() => {
expect(output).toEqual([ expect(output).toEqual([
`<?xml version="1.0" encoding="UTF-8" ?>`, `<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">`, `<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">`,
` <file source-language="xx" datatype="plaintext">`, ` <file source-language="xx" datatype="plaintext" original="ng2.template">`,
` <body>`, ` <body>`,
` <trans-unit id="${ ` <trans-unit id="${
useLegacyIds ? '1234567890ABCDEF1234567890ABCDEF12345678' : useLegacyIds ? '1234567890ABCDEF1234567890ABCDEF12345678' :

View File

@ -77,7 +77,7 @@ runInEachFileSystem(() => {
expect(output).toEqual([ expect(output).toEqual([
`<?xml version="1.0" encoding="UTF-8" ?>`, `<?xml version="1.0" encoding="UTF-8" ?>`,
`<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="xx">`, `<xliff version="2.0" xmlns="urn:oasis:names:tc:xliff:document:2.0" srcLang="xx">`,
` <file>`, ` <file id="ngi18n" original="ng.template">`,
` <unit id="${useLegacyIds ? '615790887472569365' : '12345'}">`, ` <unit id="${useLegacyIds ? '615790887472569365' : '12345'}">`,
` <notes>`, ` <notes>`,
` <note category="location">file.ts:6</note>`, ` <note category="location">file.ts:6</note>`,