From 0dcae69aafc558b897954fc688282a385c586114 Mon Sep 17 00:00:00 2001
From: Doug Parker
Date: Thu, 14 Mar 2024 13:06:41 -0700
Subject: [PATCH] refactor(compiler): add `handler` attribute to XMB output
(#54865)
This allows tracking of which tools generated which XMB files and helps attribute Angular usage.
PR Close #54865
---
packages/compiler-cli/test/extract_i18n_spec.ts | 2 +-
packages/compiler-cli/test/ngtsc/xi18n_spec.ts | 2 +-
packages/compiler/src/i18n/serializers/xmb.ts | 12 +++++++++++-
packages/compiler/test/i18n/serializers/xmb_spec.ts | 2 +-
.../translation_files/xmb_translation_serializer.ts | 12 +++++++++++-
.../tools/test/extract/integration/main_spec.ts | 2 +-
.../xmb_translation_serializer_spec.ts | 4 ++--
.../tools/test/migrate/integration/main_spec.ts | 2 +-
.../test/migrate/integration/test_files/messages.xmb | 2 +-
9 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/packages/compiler-cli/test/extract_i18n_spec.ts b/packages/compiler-cli/test/extract_i18n_spec.ts
index b3eb6a74d58..372f438133b 100644
--- a/packages/compiler-cli/test/extract_i18n_spec.ts
+++ b/packages/compiler-cli/test/extract_i18n_spec.ts
@@ -35,7 +35,7 @@ const EXPECTED_XMB = `
]>
-
+
src/basic.html:1src/comp2.ts:1src/basic.html:1translate me
src/basic.html:3,4src/comp2.ts:3,4src/comp2.ts:2,3src/basic.html:3,4
Welcome
diff --git a/packages/compiler-cli/test/ngtsc/xi18n_spec.ts b/packages/compiler-cli/test/ngtsc/xi18n_spec.ts
index fb49abc00ed..baaa91c4cc8 100644
--- a/packages/compiler-cli/test/ngtsc/xi18n_spec.ts
+++ b/packages/compiler-cli/test/ngtsc/xi18n_spec.ts
@@ -78,7 +78,7 @@ const EXPECTED_XMB = `
]>
-
+
src/basic.html:1src/comp2.ts:1src/basic.html:1translate me
src/basic.html:3,4src/comp2.ts:3,4src/comp2.ts:2,3src/basic.html:3,4
Welcome
diff --git a/packages/compiler/src/i18n/serializers/xmb.ts b/packages/compiler/src/i18n/serializers/xmb.ts
index cd4129abc3c..02ea6e82387 100644
--- a/packages/compiler/src/i18n/serializers/xmb.ts
+++ b/packages/compiler/src/i18n/serializers/xmb.ts
@@ -12,6 +12,15 @@ import * as i18n from '../i18n_ast';
import {PlaceholderMapper, Serializer, SimplePlaceholderMapper} from './serializer';
import * as xml from './xml_helper';
+/**
+ * Defines the `handler` value on the serialized XMB, indicating that Angular
+ * generated the bundle. This is useful for analytics in Translation Console.
+ *
+ * NOTE: Keep in sync with
+ * packages/localize/tools/src/extract/translation_files/xmb_translation_serializer.ts.
+ */
+const _XMB_HANDLER = 'angular';
+
const _MESSAGES_TAG = 'messagebundle';
const _MESSAGE_TAG = 'msg';
const _PLACEHOLDER_TAG = 'ph';
@@ -42,7 +51,8 @@ export class Xmb extends Serializer {
override write(messages: i18n.Message[], locale: string | null): string {
const exampleVisitor = new ExampleVisitor();
const visitor = new _Visitor();
- let rootNode = new xml.Tag(_MESSAGES_TAG);
+ const rootNode = new xml.Tag(_MESSAGES_TAG);
+ rootNode.attrs['handler'] = _XMB_HANDLER;
messages.forEach((message) => {
const attrs: {[k: string]: string} = {id: message.id};
diff --git a/packages/compiler/test/i18n/serializers/xmb_spec.ts b/packages/compiler/test/i18n/serializers/xmb_spec.ts
index 79d2ad2e90d..6d3eb50a328 100644
--- a/packages/compiler/test/i18n/serializers/xmb_spec.ts
+++ b/packages/compiler/test/i18n/serializers/xmb_spec.ts
@@ -47,7 +47,7 @@ lines
]>
-
+
file.ts:3translatable element <b><b>with placeholders</b></b> {{ interpolation}}{{ interpolation}}
file.ts:4{VAR_PLURAL, plural, =0 {<p><p>test</p></p>} }
file.ts:5foo
diff --git a/packages/localize/tools/src/extract/translation_files/xmb_translation_serializer.ts b/packages/localize/tools/src/extract/translation_files/xmb_translation_serializer.ts
index 3f91215d52c..37a7344ded1 100644
--- a/packages/localize/tools/src/extract/translation_files/xmb_translation_serializer.ts
+++ b/packages/localize/tools/src/extract/translation_files/xmb_translation_serializer.ts
@@ -17,6 +17,14 @@ import {TranslationSerializer} from './translation_serializer';
import {consolidateMessages} from './utils';
import {XmlFile} from './xml_file';
+/**
+ * Defines the `handler` value on the serialized XMB, indicating that Angular
+ * generated the bundle. This is useful for analytics in Translation Console.
+ *
+ * NOTE: Keep in sync with packages/compiler/src/i18n/serializers/xmb.ts.
+ */
+const XMB_HANDLER = 'angular';
+
/**
* A translation serializer that can write files in XMB format.
*
@@ -58,7 +66,9 @@ export class XmbTranslationSerializer implements TranslationSerializer {
`\n` +
`]>\n`,
);
- xml.startTag('messagebundle');
+ xml.startTag('messagebundle', {
+ 'handler': XMB_HANDLER,
+ });
for (const duplicateMessages of messageGroups) {
const message = duplicateMessages[0];
const id = this.getMessageId(message);
diff --git a/packages/localize/tools/test/extract/integration/main_spec.ts b/packages/localize/tools/test/extract/integration/main_spec.ts
index e3ac89201f8..304ad277901 100644
--- a/packages/localize/tools/test/extract/integration/main_spec.ts
+++ b/packages/localize/tools/test/extract/integration/main_spec.ts
@@ -218,7 +218,7 @@ runInNativeFileSystem(() => {
``,
``,
`]>`,
- ``,
+ ``,
` test_files/test.js:1Hello, !`,
` test_files/test.js:2tryme`,
` test_files/test.js:3Custom id message`,
diff --git a/packages/localize/tools/test/extract/translation_files/xmb_translation_serializer_spec.ts b/packages/localize/tools/test/extract/translation_files/xmb_translation_serializer_spec.ts
index ec98e2ae375..123dd1eff4d 100644
--- a/packages/localize/tools/test/extract/translation_files/xmb_translation_serializer_spec.ts
+++ b/packages/localize/tools/test/extract/translation_files/xmb_translation_serializer_spec.ts
@@ -79,7 +79,7 @@ runInEachFileSystem(() => {
const output = serializer.serialize(messages);
expect(output).toContain(
[
- ``,
+ ``,
` abc`,
@@ -139,7 +139,7 @@ runInEachFileSystem(() => {
'',
'',
']>',
- '',
+ '',
' a-1.ts:5message-1',
' a-2.ts:5message-1',
'',
diff --git a/packages/localize/tools/test/migrate/integration/main_spec.ts b/packages/localize/tools/test/migrate/integration/main_spec.ts
index 9a824052a6e..6c4b70592ad 100644
--- a/packages/localize/tools/test/migrate/integration/main_spec.ts
+++ b/packages/localize/tools/test/migrate/integration/main_spec.ts
@@ -157,7 +157,7 @@ runInNativeFileSystem(() => {
``,
``,
`]>`,
- ``,
+ ``,
` test.js:1Hello`,
` test.js:2Custom id message`,
` test.js:3Goodbye`,
diff --git a/packages/localize/tools/test/migrate/integration/test_files/messages.xmb b/packages/localize/tools/test/migrate/integration/test_files/messages.xmb
index 87bc4cace0d..193b5afebd9 100644
--- a/packages/localize/tools/test/migrate/integration/test_files/messages.xmb
+++ b/packages/localize/tools/test/migrate/integration/test_files/messages.xmb
@@ -20,7 +20,7 @@
]>
-
+
test.js:1Hello
test.js:2Custom id message
test.js:3Goodbye