i18n_parser.js
8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* @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] = "</" + el.name + ">";
}
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