156 lines
5.9 KiB
TypeScript
Raw Normal View History

/**
* @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';
2016-07-15 09:42:33 -07:00
import {Parser as ExpressionParser} from '../expression_parser/parser';
2016-07-21 13:56:58 -07:00
import * as html from '../html_parser/ast';
2016-07-21 11:41:25 -07:00
import {getHtmlTagDefinition} from '../html_parser/html_tags';
import {InterpolationConfig} from '../html_parser/interpolation_config';
2016-07-15 09:42:33 -07:00
import {ParseSourceSpan} from '../parse_util';
2016-07-15 09:42:33 -07:00
import {extractAstMessages} from './extractor';
2016-07-21 13:56:58 -07:00
import * as i18n from './i18n_ast';
import {PlaceholderRegistry} from './serializers/placeholder';
2016-07-15 09:42:33 -07:00
import {extractPlaceholderName} from './shared';
2016-07-21 13:56:58 -07:00
/**
* Extract all the i18n messages from a component template.
*/
export function extractI18nMessages(
2016-07-21 13:56:58 -07:00
sourceAst: html.Node[], interpolationConfig: InterpolationConfig, implicitTags: string[],
implicitAttrs: {[k: string]: string[]}): i18n.Message[] {
2016-07-15 09:42:33 -07:00
const extractionResult = extractAstMessages(sourceAst, implicitTags, implicitAttrs);
2016-07-15 09:42:33 -07:00
if (extractionResult.errors.length) {
return [];
}
2016-07-21 13:56:58 -07:00
const expParser = new ExpressionParser(new ExpressionLexer());
const visitor = new _I18nVisitor(expParser, interpolationConfig);
2016-07-21 13:56:58 -07:00
return extractionResult.messages.map(
(msg) => visitor.toI18nMessage(msg.nodes, msg.meaning, msg.description));
}
2016-07-21 13:56:58 -07:00
class _I18nVisitor implements html.Visitor {
private _isIcu: boolean;
private _icuDepth: number;
2016-07-15 09:42:33 -07:00
private _placeholderRegistry: PlaceholderRegistry;
2016-07-21 13:56:58 -07:00
private _placeholderToContent: {[name: string]: string};
2016-07-15 09:42:33 -07:00
constructor(
private _expressionParser: ExpressionParser,
private _interpolationConfig: InterpolationConfig) {}
2016-07-21 13:56:58 -07:00
public toI18nMessage(nodes: html.Node[], meaning: string, description: string): i18n.Message {
this._isIcu = nodes.length == 1 && nodes[0] instanceof html.Expansion;
this._icuDepth = 0;
this._placeholderRegistry = new PlaceholderRegistry();
this._placeholderToContent = {};
const i18nodes: i18n.Node[] = html.visitAll(this, nodes, {});
return new i18n.Message(i18nodes, this._placeholderToContent, meaning, description);
}
visitElement(el: html.Element, context: any): i18n.Node {
const children = html.visitAll(this, el.children);
const attrs: {[k: string]: string} = {};
el.attrs.forEach(attr => {
// Do not visit the attributes, translatable ones are top-level ASTs
attrs[attr.name] = attr.value;
});
2016-07-15 09:42:33 -07:00
const isVoid: boolean = getHtmlTagDefinition(el.name).isVoid;
const startPhName =
this._placeholderRegistry.getStartTagPlaceholderName(el.name, attrs, isVoid);
2016-07-21 13:56:58 -07:00
this._placeholderToContent[startPhName] = el.sourceSpan.toString();
2016-07-15 09:42:33 -07:00
2016-07-21 13:56:58 -07:00
let closePhName = '';
if (!isVoid) {
closePhName = this._placeholderRegistry.getCloseTagPlaceholderName(el.name);
this._placeholderToContent[closePhName] = `</${el.name}>`;
}
return new i18n.TagPlaceholder(
2016-07-15 09:42:33 -07:00
el.name, attrs, startPhName, closePhName, children, isVoid, el.sourceSpan);
}
2016-07-21 13:56:58 -07:00
visitAttribute(attribute: html.Attribute, context: any): i18n.Node {
return this._visitTextWithInterpolation(attribute.value, attribute.sourceSpan);
}
2016-07-21 13:56:58 -07:00
visitText(text: html.Text, context: any): i18n.Node {
return this._visitTextWithInterpolation(text.value, text.sourceSpan);
}
2016-07-21 13:56:58 -07:00
visitComment(comment: html.Comment, context: any): i18n.Node { return null; }
2016-07-21 13:56:58 -07:00
visitExpansion(icu: html.Expansion, context: any): i18n.Node {
this._icuDepth++;
2016-07-21 13:56:58 -07:00
const i18nIcuCases: {[k: string]: i18n.Node} = {};
const i18nIcu = new i18n.Icu(icu.switchValue, icu.type, i18nIcuCases, icu.sourceSpan);
icu.cases.forEach((caze): void => {
2016-07-21 13:56:58 -07:00
i18nIcuCases[caze.value] = new i18n.Container(
caze.expression.map((node) => node.visit(this, {})), caze.expSourceSpan);
2016-07-15 09:42:33 -07:00
});
this._icuDepth--;
if (this._isIcu || this._icuDepth > 0) {
2016-07-21 13:56:58 -07:00
// If the message (vs a part of the message) is an ICU message returns it
return i18nIcu;
}
// else returns a placeholder
2016-07-15 09:42:33 -07:00
const phName = this._placeholderRegistry.getPlaceholderName('ICU', icu.sourceSpan.toString());
2016-07-21 13:56:58 -07:00
this._placeholderToContent[phName] = icu.sourceSpan.toString();
return new i18n.IcuPlaceholder(i18nIcu, phName, icu.sourceSpan);
}
2016-07-21 13:56:58 -07:00
visitExpansionCase(icuCase: html.ExpansionCase, context: any): i18n.Node {
throw new Error('Unreachable code');
}
2016-07-21 13:56:58 -07:00
private _visitTextWithInterpolation(text: string, sourceSpan: ParseSourceSpan): i18n.Node {
2016-07-15 09:42:33 -07:00
const splitInterpolation = this._expressionParser.splitInterpolation(
text, sourceSpan.start.toString(), this._interpolationConfig);
if (!splitInterpolation) {
// No expression, return a single text
2016-07-21 13:56:58 -07:00
return new i18n.Text(text, sourceSpan);
}
// Return a group of text + expressions
2016-07-21 13:56:58 -07:00
const nodes: i18n.Node[] = [];
const container = new i18n.Container(nodes, sourceSpan);
const {start: sDelimiter, end: eDelimiter} = this._interpolationConfig;
for (let i = 0; i < splitInterpolation.strings.length - 1; i++) {
const expression = splitInterpolation.expressions[i];
2016-07-15 09:42:33 -07:00
const baseName = extractPlaceholderName(expression) || 'INTERPOLATION';
const phName = this._placeholderRegistry.getPlaceholderName(baseName, expression);
if (splitInterpolation.strings[i].length) {
2016-07-21 13:56:58 -07:00
// No need to add empty strings
nodes.push(new i18n.Text(splitInterpolation.strings[i], sourceSpan));
2016-07-15 09:42:33 -07:00
}
2016-07-21 13:56:58 -07:00
nodes.push(new i18n.Placeholder(expression, phName, sourceSpan));
this._placeholderToContent[phName] = sDelimiter + expression + eDelimiter;
}
// The last index contains no expression
const lastStringIdx = splitInterpolation.strings.length - 1;
2016-07-15 09:42:33 -07:00
if (splitInterpolation.strings[lastStringIdx].length) {
2016-07-21 13:56:58 -07:00
nodes.push(new i18n.Text(splitInterpolation.strings[lastStringIdx], sourceSpan));
2016-07-15 09:42:33 -07:00
}
return container;
}
}