From 99587ea4edaefb23907c951cc55559dec0fc5968 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Fri, 17 Jun 2016 17:03:37 -0700 Subject: [PATCH] refactor(i18n): misc --- .../compiler/src/i18n/i18n_html_parser.ts | 55 +------------------ .../compiler/src/i18n/message_extractor.ts | 2 + modules/@angular/compiler/src/i18n/shared.ts | 2 +- 3 files changed, 4 insertions(+), 55 deletions(-) diff --git a/modules/@angular/compiler/src/i18n/i18n_html_parser.ts b/modules/@angular/compiler/src/i18n/i18n_html_parser.ts index f0d5e8eb0c..bb10cf3676 100644 --- a/modules/@angular/compiler/src/i18n/i18n_html_parser.ts +++ b/modules/@angular/compiler/src/i18n/i18n_html_parser.ts @@ -20,60 +20,7 @@ let _PLACEHOLDER_EXPANDED_REGEXP = /<\/ph>/gi; * * Algorithm: * - * To understand the algorithm, you need to know how partitioning works. - * Partitioning is required as we can use two i18n comments to group node siblings together. - * That is why we cannot just use nodes. - * - * Partitioning transforms an array of HtmlAst into an array of Part. - * A part can optionally contain a root element or a root text node. And it can also contain - * children. - * A part can contain i18n property, in which case it needs to be translated. - * - * Example: - * - * The following array of nodes will be split into four parts: - * - * ``` - * A - * B - * - * C - * D - * - * E - * ``` - * - * Part 1 containing the a tag. It should not be translated. - * Part 2 containing the b tag. It should be translated. - * Part 3 containing the c tag and the D text node. It should be translated. - * Part 4 containing the E text node. It should not be translated. - * - * - * It is also important to understand how we stringify nodes to create a message. - * - * We walk the tree and replace every element node with a placeholder. We also replace - * all expressions in interpolation with placeholders. We also insert a placeholder element - * to wrap a text node containing interpolation. - * - * Example: - * - * The following tree: - * - * ``` - * A{{I}}B - * ``` - * - * will be stringified into: - * ``` - * AB - * ``` - * - * This is what the algorithm does: - * - * 1. Use the provided html parser to get the html AST of the template. - * 2. Partition the root nodes, and process each part separately. - * 3. If a part does not have the i18n attribute, recurse to process children and attributes. - * 4. If a part has the i18n attribute, merge the translated i18n part with the original tree. + * See `message_extractor.ts` for details on the partitioning algorithm. * * This is how the merging works: * diff --git a/modules/@angular/compiler/src/i18n/message_extractor.ts b/modules/@angular/compiler/src/i18n/message_extractor.ts index c4be339f07..ac1bce4717 100644 --- a/modules/@angular/compiler/src/i18n/message_extractor.ts +++ b/modules/@angular/compiler/src/i18n/message_extractor.ts @@ -147,6 +147,7 @@ export class MessageExtractor { isPresent(this._implicitAttrs[p.name]) ? this._implicitAttrs[p.name] : []; let explicitAttrs: string[] = []; + // `i18n-` prefixed attributes should be translated p.attrs.filter(attr => attr.name.startsWith(I18N_ATTR_PREFIX)).forEach(attr => { try { explicitAttrs.push(attr.name.substring(I18N_ATTR_PREFIX.length)); @@ -161,6 +162,7 @@ export class MessageExtractor { } }); + // implicit attributes should also be translated p.attrs.filter(attr => !attr.name.startsWith(I18N_ATTR_PREFIX)) .filter(attr => explicitAttrs.indexOf(attr.name) == -1) .filter(attr => transAttrs.indexOf(attr.name) > -1) diff --git a/modules/@angular/compiler/src/i18n/shared.ts b/modules/@angular/compiler/src/i18n/shared.ts index 6df28474c9..72dd20e7d1 100644 --- a/modules/@angular/compiler/src/i18n/shared.ts +++ b/modules/@angular/compiler/src/i18n/shared.ts @@ -77,7 +77,7 @@ function _isOpeningComment(n: HtmlAst): boolean { } function _isClosingComment(n: HtmlAst): boolean { - return n instanceof HtmlCommentAst && isPresent(n.value) && n.value == '/i18n'; + return n instanceof HtmlCommentAst && isPresent(n.value) && n.value === '/i18n'; } function _findI18nAttr(p: HtmlElementAst): HtmlAttrAst {