Skip to content

Commit

Permalink
fix(ivy): i18n - handle translated text containing HTML comments
Browse files Browse the repository at this point in the history
Fixes FW-1536
  • Loading branch information
petebacondarwin committed Sep 4, 2019
1 parent 04d4fea commit c771f2c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ const verify = (input: string, output: string, extra: any = {}): void => {
}
};

describe('i18n support in the view compiler', () => {
describe('i18n support in the template compiler', () => {

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

it('should ignore HTML comments within translated text', () => {
const input = `
<div i18n>Some <!-- comments --> text</div>
`;

const output = String.raw `
var $I18N_0$;
if (ngI18nClosureMode) {
const $MSG_EXTERNAL_0$ = goog.getMsg("Some text");
$I18N_0$ = $MSG_EXTERNAL_0$;
}
else {
$I18N_0$ = $localize \`Some text\`;
}
`;
verify(input, output);
});

it('should properly escape quotes in content', () => {
const input = `
<div i18n>Some text 'with single quotes', "with double quotes" and without quotes.</div>
Expand Down
7 changes: 6 additions & 1 deletion packages/compiler/src/render3/view/i18n/localize_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ class PlaceholderPiece extends MessagePiece {
*/
class LocalizeSerializerVisitor implements i18n.Visitor {
visitText(text: i18n.Text, context: MessagePiece[]): any {
context.push(new LiteralPiece(text.value));
if (context[context.length - 1] instanceof LiteralPiece) {
// Two literal pieces in a row means that there was some comment node in-between.
context[context.length - 1].text += text.value;
} else {
context.push(new LiteralPiece(text.value));
}
}

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

0 comments on commit c771f2c

Please sign in to comment.