Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ivy): i18n - handle translated text containing HTML comments #32475

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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