/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import { Lexer as ExpressionLexer } from '../expression_parser/lexer'; import { Parser as ExpressionParser } from '../expression_parser/parser'; import * as html from '../ml_parser/ast'; import { getHtmlTagDefinition } from '../ml_parser/html_tags'; import * as i18n from './i18n_ast'; import { PlaceholderRegistry } from './serializers/placeholder'; var /** @type {?} */ _expParser = new ExpressionParser(new ExpressionLexer()); /** * Returns a function converting html nodes to an i18n Message given an interpolationConfig * @param {?} interpolationConfig * @return {?} */ export function createI18nMessageFactory(interpolationConfig) { var /** @type {?} */ visitor = new _I18nVisitor(_expParser, interpolationConfig); return function (nodes, meaning, description) { return visitor.toI18nMessage(nodes, meaning, description); }; } var _I18nVisitor = (function () { /** * @param {?} _expressionParser * @param {?} _interpolationConfig */ function _I18nVisitor(_expressionParser, _interpolationConfig) { this._expressionParser = _expressionParser; this._interpolationConfig = _interpolationConfig; } /** * @param {?} nodes * @param {?} meaning * @param {?} description * @return {?} */ _I18nVisitor.prototype.toI18nMessage = function (nodes, meaning, description) { this._isIcu = nodes.length == 1 && nodes[0] instanceof html.Expansion; this._icuDepth = 0; this._placeholderRegistry = new PlaceholderRegistry(); this._placeholderToContent = {}; this._placeholderToMessage = {}; var /** @type {?} */ i18nodes = html.visitAll(this, nodes, {}); return new i18n.Message(i18nodes, this._placeholderToContent, this._placeholderToMessage, meaning, description); }; /** * @param {?} el * @param {?} context * @return {?} */ _I18nVisitor.prototype.visitElement = function (el, context) { var /** @type {?} */ children = html.visitAll(this, el.children); var /** @type {?} */ attrs = {}; el.attrs.forEach(function (attr) { // Do not visit the attributes, translatable ones are top-level ASTs attrs[attr.name] = attr.value; }); var /** @type {?} */ isVoid = getHtmlTagDefinition(el.name).isVoid; var /** @type {?} */ startPhName = this._placeholderRegistry.getStartTagPlaceholderName(el.name, attrs, isVoid); this._placeholderToContent[startPhName] = el.sourceSpan.toString(); var /** @type {?} */ closePhName = ''; if (!isVoid) { closePhName = this._placeholderRegistry.getCloseTagPlaceholderName(el.name); this._placeholderToContent[closePhName] = ""; } return new i18n.TagPlaceholder(el.name, attrs, startPhName, closePhName, children, isVoid, el.sourceSpan); }; /** * @param {?} attribute * @param {?} context * @return {?} */ _I18nVisitor.prototype.visitAttribute = function (attribute, context) { return this._visitTextWithInterpolation(attribute.value, attribute.sourceSpan); }; /** * @param {?} text * @param {?} context * @return {?} */ _I18nVisitor.prototype.visitText = function (text, context) { return this._visitTextWithInterpolation(text.value, text.sourceSpan); }; /** * @param {?} comment * @param {?} context * @return {?} */ _I18nVisitor.prototype.visitComment = function (comment, context) { return null; }; /** * @param {?} icu * @param {?} context * @return {?} */ _I18nVisitor.prototype.visitExpansion = function (icu, context) { var _this = this; this._icuDepth++; var /** @type {?} */ i18nIcuCases = {}; var /** @type {?} */ i18nIcu = new i18n.Icu(icu.switchValue, icu.type, i18nIcuCases, icu.sourceSpan); icu.cases.forEach(function (caze) { i18nIcuCases[caze.value] = new i18n.Container(caze.expression.map(function (node) { return node.visit(_this, {}); }), caze.expSourceSpan); }); this._icuDepth--; if (this._isIcu || this._icuDepth > 0) { // Returns an ICU node when: // - the message (vs a part of the message) is an ICU message, or // - the ICU message is nested. var /** @type {?} */ expPh = this._placeholderRegistry.getUniquePlaceholder("VAR_" + icu.type); i18nIcu.expressionPlaceholder = expPh; this._placeholderToContent[expPh] = icu.switchValue; return i18nIcu; } // Else returns a placeholder // ICU placeholders should not be replaced with their original content but with the their // translations. We need to create a new visitor (they are not re-entrant) to compute the // message id. // TODO(vicb): add a html.Node -> i18n.Message cache to avoid having to re-create the msg var /** @type {?} */ phName = this._placeholderRegistry.getPlaceholderName('ICU', icu.sourceSpan.toString()); var /** @type {?} */ visitor = new _I18nVisitor(this._expressionParser, this._interpolationConfig); this._placeholderToMessage[phName] = visitor.toI18nMessage([icu], '', ''); return new i18n.IcuPlaceholder(i18nIcu, phName, icu.sourceSpan); }; /** * @param {?} icuCase * @param {?} context * @return {?} */ _I18nVisitor.prototype.visitExpansionCase = function (icuCase, context) { throw new Error('Unreachable code'); }; /** * @param {?} text * @param {?} sourceSpan * @return {?} */ _I18nVisitor.prototype._visitTextWithInterpolation = function (text, sourceSpan) { var /** @type {?} */ splitInterpolation = this._expressionParser.splitInterpolation(text, sourceSpan.start.toString(), this._interpolationConfig); if (!splitInterpolation) { // No expression, return a single text return new i18n.Text(text, sourceSpan); } // Return a group of text + expressions var /** @type {?} */ nodes = []; var /** @type {?} */ container = new i18n.Container(nodes, sourceSpan); var _a = this._interpolationConfig, sDelimiter = _a.start, eDelimiter = _a.end; for (var /** @type {?} */ i = 0; i < splitInterpolation.strings.length - 1; i++) { var /** @type {?} */ expression = splitInterpolation.expressions[i]; var /** @type {?} */ baseName = _extractPlaceholderName(expression) || 'INTERPOLATION'; var /** @type {?} */ phName = this._placeholderRegistry.getPlaceholderName(baseName, expression); if (splitInterpolation.strings[i].length) { // No need to add empty strings nodes.push(new i18n.Text(splitInterpolation.strings[i], sourceSpan)); } nodes.push(new i18n.Placeholder(expression, phName, sourceSpan)); this._placeholderToContent[phName] = sDelimiter + expression + eDelimiter; } // The last index contains no expression var /** @type {?} */ lastStringIdx = splitInterpolation.strings.length - 1; if (splitInterpolation.strings[lastStringIdx].length) { nodes.push(new i18n.Text(splitInterpolation.strings[lastStringIdx], sourceSpan)); } return container; }; return _I18nVisitor; }()); function _I18nVisitor_tsickle_Closure_declarations() { /** @type {?} */ _I18nVisitor.prototype._isIcu; /** @type {?} */ _I18nVisitor.prototype._icuDepth; /** @type {?} */ _I18nVisitor.prototype._placeholderRegistry; /** @type {?} */ _I18nVisitor.prototype._placeholderToContent; /** @type {?} */ _I18nVisitor.prototype._placeholderToMessage; /** @type {?} */ _I18nVisitor.prototype._expressionParser; /** @type {?} */ _I18nVisitor.prototype._interpolationConfig; } var /** @type {?} */ _CUSTOM_PH_EXP = /\/\/[\s\S]*i18n[\s\S]*\([\s\S]*ph[\s\S]*=[\s\S]*"([\s\S]*?)"[\s\S]*\)/g; /** * @param {?} input * @return {?} */ function _extractPlaceholderName(input) { return input.split(_CUSTOM_PH_EXP)[1]; } //# sourceMappingURL=i18n_parser.js.map