From 0fe3cd9a4c59b945afaa6eba6b916e8ca65b4e75 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Thu, 15 Dec 2016 15:33:42 -0800 Subject: [PATCH] fix(i18n): add a default example to xmb placeholders (#13507) Otherwise the TC would not be able to load the message --- .../compiler/src/i18n/serializers/xmb.ts | 28 +++++++++++++++++-- .../test/i18n/serializers/xmb_spec.ts | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/modules/@angular/compiler/src/i18n/serializers/xmb.ts b/modules/@angular/compiler/src/i18n/serializers/xmb.ts index 3834ee3938..92842931a0 100644 --- a/modules/@angular/compiler/src/i18n/serializers/xmb.ts +++ b/modules/@angular/compiler/src/i18n/serializers/xmb.ts @@ -39,6 +39,7 @@ const _DOCTYPE = ` export class Xmb implements Serializer { write(messages: i18n.Message[]): string { + const exampleVisitor = new ExampleVisitor(); const visitor = new _Visitor(); const visited: {[id: string]: boolean} = {}; let rootNode = new xml.Tag(_MESSAGES_TAG); @@ -71,7 +72,7 @@ export class Xmb implements Serializer { new xml.CR(), new xml.Doctype(_MESSAGES_TAG, _DOCTYPE), new xml.CR(), - rootNode, + exampleVisitor.addDefaultExamples(rootNode), new xml.CR(), ]); } @@ -133,4 +134,27 @@ class _Visitor implements i18n.Visitor { export function digest(message: i18n.Message): string { return decimalDigest(message); -} \ No newline at end of file +} + +// TC requires at least one non-empty example on placeholders +class ExampleVisitor implements xml.IVisitor { + addDefaultExamples(node: xml.Node): xml.Node { + node.visit(this); + return node; + } + + visitTag(tag: xml.Tag): void { + if (tag.name === _PLACEHOLDER_TAG) { + if (!tag.children || tag.children.length == 0) { + const exText = new xml.Text(tag.attrs['name'] || '...'); + tag.children = [new xml.Tag(_EXEMPLE_TAG, {}, [exText])]; + } + } else if (tag.children) { + tag.children.forEach(node => node.visit(this)); + } + } + + visitText(text: xml.Text): void {} + visitDeclaration(decl: xml.Declaration): void {} + visitDoctype(doctype: xml.Doctype): void {} +} diff --git a/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts b/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts index ecfe0b1519..dc0c2c5cbc 100644 --- a/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts +++ b/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts @@ -46,7 +46,7 @@ export function main(): void { ]> - translatable element <b>with placeholders</b> + translatable element <b>with placeholders</b> INTERPOLATION {VAR_PLURAL, plural, =0 {<p>test</p>} } foo foo