Skip to content

Commit 5d8eb74

Browse files
petebacondarwinmhevery
authored andcommitted
fix(ivy): i18n - handle translated text containing HTML comments (#32475)
Fixes FW-1536 PR Close #32475
1 parent 7cc4225 commit 5d8eb74

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

packages/compiler-cli/test/compliance/r3_view_compiler_i18n_spec.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ const verify = (input: string, output: string, extra: any = {}): void => {
169169
}
170170
};
171171

172-
describe('i18n support in the view compiler', () => {
172+
describe('i18n support in the template compiler', () => {
173173

174174
describe('element attributes', () => {
175175
it('should add the meaning and description as JsDoc comments', () => {
@@ -943,6 +943,24 @@ describe('i18n support in the view compiler', () => {
943943
verify(input, output, {exceptions});
944944
});
945945

946+
it('should ignore HTML comments within translated text', () => {
947+
const input = `
948+
<div i18n>Some <!-- comments --> text</div>
949+
`;
950+
951+
const output = String.raw `
952+
var $I18N_0$;
953+
if (ngI18nClosureMode) {
954+
const $MSG_EXTERNAL_0$ = goog.getMsg("Some text");
955+
$I18N_0$ = $MSG_EXTERNAL_0$;
956+
}
957+
else {
958+
$I18N_0$ = $localize \`Some text\`;
959+
}
960+
`;
961+
verify(input, output);
962+
});
963+
946964
it('should properly escape quotes in content', () => {
947965
const input = `
948966
<div i18n>Some text 'with single quotes', "with double quotes" and without quotes.</div>

packages/compiler/src/render3/view/i18n/localize_utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ class PlaceholderPiece extends MessagePiece {
4444
*/
4545
class LocalizeSerializerVisitor implements i18n.Visitor {
4646
visitText(text: i18n.Text, context: MessagePiece[]): any {
47-
context.push(new LiteralPiece(text.value));
47+
if (context[context.length - 1] instanceof LiteralPiece) {
48+
// Two literal pieces in a row means that there was some comment node in-between.
49+
context[context.length - 1].text += text.value;
50+
} else {
51+
context.push(new LiteralPiece(text.value));
52+
}
4853
}
4954

5055
visitContainer(container: i18n.Container, context: MessagePiece[]): any {

0 commit comments

Comments
 (0)