From f0af387f6c965578fda8bd03e205a883965df7c7 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Tue, 25 Aug 2020 13:51:06 +0100 Subject: [PATCH] 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 `` 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 --- .../xliff1_translation_serializer.ts | 13 ++++++++++++- .../xliff2_translation_serializer.ts | 8 +++++++- .../src/tools/test/extract/integration/main_spec.ts | 6 +++--- .../xliff1_translation_serializer_spec.ts | 2 +- .../xliff2_translation_serializer_spec.ts | 2 +- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/localize/src/tools/src/extract/translation_files/xliff1_translation_serializer.ts b/packages/localize/src/tools/src/extract/translation_files/xliff1_translation_serializer.ts index 8dd6f2085b..c44f8f9d21 100644 --- a/packages/localize/src/tools/src/extract/translation_files/xliff1_translation_serializer.ts +++ b/packages/localize/src/tools/src/extract/translation_files/xliff1_translation_serializer.ts @@ -32,7 +32,18 @@ export class Xliff1TranslationSerializer implements TranslationSerializer { const ids = new Set(); const xml = new XmlFile(); 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 `` 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'); for (const message of messages) { const id = this.getMessageId(message); diff --git a/packages/localize/src/tools/src/extract/translation_files/xliff2_translation_serializer.ts b/packages/localize/src/tools/src/extract/translation_files/xliff2_translation_serializer.ts index 234ceaf45e..4dd7e4f4a2 100644 --- a/packages/localize/src/tools/src/extract/translation_files/xliff2_translation_serializer.ts +++ b/packages/localize/src/tools/src/extract/translation_files/xliff2_translation_serializer.ts @@ -36,7 +36,13 @@ export class Xliff2TranslationSerializer implements TranslationSerializer { 'xmlns': 'urn:oasis:names:tc:xliff:document:2.0', '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 `` 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) { const id = this.getMessageId(message); if (ids.has(id)) { diff --git a/packages/localize/src/tools/test/extract/integration/main_spec.ts b/packages/localize/src/tools/test/extract/integration/main_spec.ts index 72b9d1aa5f..3f69a2d22f 100644 --- a/packages/localize/src/tools/test/extract/integration/main_spec.ts +++ b/packages/localize/src/tools/test/extract/integration/main_spec.ts @@ -149,7 +149,7 @@ runInEachFileSystem(() => { expect(fs.readFile(outputPath)).toEqual([ ``, ``, - ` `, + ` `, ` `, ` `, ` Hello, !`, @@ -218,7 +218,7 @@ runInEachFileSystem(() => { expect(fs.readFile(outputPath)).toEqual([ ``, ``, - ` `, + ` `, ` `, ` `, ` Hello, !`, @@ -276,7 +276,7 @@ runInEachFileSystem(() => { expect(fs.readFile(outputPath)).toEqual([ ``, ``, - ` `, + ` `, ` `, ` `, ` Message in !`, diff --git a/packages/localize/src/tools/test/extract/translation_files/xliff1_translation_serializer_spec.ts b/packages/localize/src/tools/test/extract/translation_files/xliff1_translation_serializer_spec.ts index 31ff2d99c2..f421850a32 100644 --- a/packages/localize/src/tools/test/extract/translation_files/xliff1_translation_serializer_spec.ts +++ b/packages/localize/src/tools/test/extract/translation_files/xliff1_translation_serializer_spec.ts @@ -72,7 +72,7 @@ runInEachFileSystem(() => { expect(output).toEqual([ ``, ``, - ` `, + ` `, ` `, ` `, ``, - ` `, + ` `, ` `, ` `, ` file.ts:6`,