feat(i18n): xmb serializer
This commit is contained in:
parent
48f230a951
commit
cc5cfe87c3
@ -140,8 +140,8 @@ class TreeBuilder {
|
|||||||
|
|
||||||
// read {
|
// read {
|
||||||
if (this.peek.type !== HtmlTokenType.EXPANSION_CASE_EXP_START) {
|
if (this.peek.type !== HtmlTokenType.EXPANSION_CASE_EXP_START) {
|
||||||
this.errors.push(HtmlTreeError.create(
|
this.errors.push(
|
||||||
null, this.peek.sourceSpan, `Invalid ICU message. Missing '{'.`));
|
HtmlTreeError.create(null, this.peek.sourceSpan, `Invalid ICU message. Missing '{'.`));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {isPresent, isBlank, normalizeBool, RegExpWrapper,} from '../src/facade/lang';
|
import {normalizeBool, RegExpWrapper,} from '../src/facade/lang';
|
||||||
|
|
||||||
// see http://www.w3.org/TR/html51/syntax.html#named-character-references
|
// see http://www.w3.org/TR/html51/syntax.html#named-character-references
|
||||||
// see https://html.spec.whatwg.org/multipage/entities.json
|
// see https://html.spec.whatwg.org/multipage/entities.json
|
||||||
@ -294,31 +294,32 @@ export class HtmlTagDefinition {
|
|||||||
isVoid?: boolean,
|
isVoid?: boolean,
|
||||||
ignoreFirstLf?: boolean
|
ignoreFirstLf?: boolean
|
||||||
} = {}) {
|
} = {}) {
|
||||||
if (isPresent(closedByChildren) && closedByChildren.length > 0) {
|
if (closedByChildren && closedByChildren.length > 0) {
|
||||||
closedByChildren.forEach(tagName => this.closedByChildren[tagName] = true);
|
closedByChildren.forEach(tagName => this.closedByChildren[tagName] = true);
|
||||||
}
|
}
|
||||||
this.isVoid = normalizeBool(isVoid);
|
this.isVoid = normalizeBool(isVoid);
|
||||||
this.closedByParent = normalizeBool(closedByParent) || this.isVoid;
|
this.closedByParent = normalizeBool(closedByParent) || this.isVoid;
|
||||||
if (isPresent(requiredParents) && requiredParents.length > 0) {
|
if (requiredParents && requiredParents.length > 0) {
|
||||||
this.requiredParents = {};
|
this.requiredParents = {};
|
||||||
|
// The first parent is the list is automatically when none of the listed parents are present
|
||||||
this.parentToAdd = requiredParents[0];
|
this.parentToAdd = requiredParents[0];
|
||||||
requiredParents.forEach(tagName => this.requiredParents[tagName] = true);
|
requiredParents.forEach(tagName => this.requiredParents[tagName] = true);
|
||||||
}
|
}
|
||||||
this.implicitNamespacePrefix = implicitNamespacePrefix;
|
this.implicitNamespacePrefix = implicitNamespacePrefix;
|
||||||
this.contentType = isPresent(contentType) ? contentType : HtmlTagContentType.PARSABLE_DATA;
|
this.contentType = contentType || HtmlTagContentType.PARSABLE_DATA;
|
||||||
this.ignoreFirstLf = normalizeBool(ignoreFirstLf);
|
this.ignoreFirstLf = normalizeBool(ignoreFirstLf);
|
||||||
}
|
}
|
||||||
|
|
||||||
requireExtraParent(currentParent: string): boolean {
|
requireExtraParent(currentParent: string): boolean {
|
||||||
if (isBlank(this.requiredParents)) {
|
if (!this.requiredParents) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isBlank(currentParent)) {
|
if (!currentParent) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
let lcParent = currentParent.toLowerCase();
|
const lcParent = currentParent.toLowerCase();
|
||||||
return this.requiredParents[lcParent] != true && lcParent != 'template';
|
return this.requiredParents[lcParent] != true && lcParent != 'template';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -382,20 +383,19 @@ var TAG_DEFINITIONS: {[key: string]: HtmlTagDefinition} = {
|
|||||||
{contentType: HtmlTagContentType.ESCAPABLE_RAW_TEXT, ignoreFirstLf: true}),
|
{contentType: HtmlTagContentType.ESCAPABLE_RAW_TEXT, ignoreFirstLf: true}),
|
||||||
};
|
};
|
||||||
|
|
||||||
var DEFAULT_TAG_DEFINITION = new HtmlTagDefinition();
|
const _DEFAULT_TAG_DEFINITION = new HtmlTagDefinition();
|
||||||
|
|
||||||
export function getHtmlTagDefinition(tagName: string): HtmlTagDefinition {
|
export function getHtmlTagDefinition(tagName: string): HtmlTagDefinition {
|
||||||
var result = TAG_DEFINITIONS[tagName.toLowerCase()];
|
return TAG_DEFINITIONS[tagName.toLowerCase()] || _DEFAULT_TAG_DEFINITION;
|
||||||
return isPresent(result) ? result : DEFAULT_TAG_DEFINITION;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var NS_PREFIX_RE = /^:([^:]+):(.+)/g;
|
const _NS_PREFIX_RE = /^:([^:]+):(.+)/g;
|
||||||
|
|
||||||
export function splitNsName(elementName: string): string[] {
|
export function splitNsName(elementName: string): [string, string] {
|
||||||
if (elementName[0] != ':') {
|
if (elementName[0] != ':') {
|
||||||
return [null, elementName];
|
return [null, elementName];
|
||||||
}
|
}
|
||||||
let match = RegExpWrapper.firstMatch(NS_PREFIX_RE, elementName);
|
const match = RegExpWrapper.firstMatch(_NS_PREFIX_RE, elementName);
|
||||||
return [match[1], match[2]];
|
return [match[1], match[2]];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -404,5 +404,5 @@ export function getNsPrefix(elementName: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function mergeNsAndName(prefix: string, localName: string): string {
|
export function mergeNsAndName(prefix: string, localName: string): string {
|
||||||
return isPresent(prefix) ? `:${prefix}:${localName}` : localName;
|
return prefix ? `:${prefix}:${localName}` : localName;
|
||||||
}
|
}
|
||||||
|
112
modules/@angular/compiler/src/i18n/catalog.ts
Normal file
112
modules/@angular/compiler/src/i18n/catalog.ts
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
/**
|
||||||
|
* @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 {HtmlParser} from '../html_parser';
|
||||||
|
import {InterpolationConfig} from '../interpolation_config';
|
||||||
|
|
||||||
|
import * as i18nAst from './i18n_ast';
|
||||||
|
import {extractI18nMessages} from './i18n_parser';
|
||||||
|
import {Serializer} from './serializers/serializer';
|
||||||
|
|
||||||
|
export class Catalog {
|
||||||
|
private _messageMap: {[k: string]: i18nAst.Message} = {};
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private _htmlParser: HtmlParser, private _implicitTags: string[],
|
||||||
|
private _implicitAttrs: {[k: string]: string[]}) {}
|
||||||
|
|
||||||
|
public updateFromTemplate(html: string, url: string, interpolationConfig: InterpolationConfig):
|
||||||
|
void {
|
||||||
|
const htmlParserResult = this._htmlParser.parse(html, url, true, interpolationConfig);
|
||||||
|
|
||||||
|
if (htmlParserResult.errors.length) {
|
||||||
|
throw new Error();
|
||||||
|
}
|
||||||
|
|
||||||
|
const messages = extractI18nMessages(
|
||||||
|
htmlParserResult.rootNodes, interpolationConfig, this._implicitTags, this._implicitAttrs);
|
||||||
|
|
||||||
|
messages.forEach((message) => {
|
||||||
|
const id = strHash(serializeAst(message.nodes).join('') + `[${message.meaning}]`);
|
||||||
|
this._messageMap[id] = message;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public load(content: string, serializer: Serializer): void {
|
||||||
|
const nodeMap = serializer.load(content);
|
||||||
|
this._messageMap = {};
|
||||||
|
|
||||||
|
Object.getOwnPropertyNames(nodeMap).forEach(
|
||||||
|
(id) => { this._messageMap[id] = new i18nAst.Message(nodeMap[id], '', ''); });
|
||||||
|
}
|
||||||
|
|
||||||
|
public write(serializer: Serializer): string { return serializer.write(this._messageMap); }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* String hash function similar to java.lang.String.hashCode().
|
||||||
|
* The hash code for a string is computed as
|
||||||
|
* s[0] * 31 ^ (n - 1) + s[1] * 31 ^ (n - 2) + ... + s[n - 1],
|
||||||
|
* where s[i] is the ith character of the string and n is the length of
|
||||||
|
* the string. We mod the result to make it between 0 (inclusive) and 2^32 (exclusive).
|
||||||
|
*
|
||||||
|
* Based on goog.string.hashCode from the Google Closure library
|
||||||
|
* https://github.com/google/closure-library/
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
// TODO(vicb): better algo (less collisions) ?
|
||||||
|
export function strHash(str: string): string {
|
||||||
|
let result: number = 0;
|
||||||
|
for (var i = 0; i < str.length; ++i) {
|
||||||
|
// Normalize to 4 byte range, 0 ... 2^32.
|
||||||
|
result = (31 * result + str.charCodeAt(i)) >>> 0;
|
||||||
|
}
|
||||||
|
return result.toString(16);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serialize the i18n ast to something xml-like in order to generate an UID.
|
||||||
|
*
|
||||||
|
* The visitor is also used in the i18n parser tests
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
class _SerializerVisitor implements i18nAst.Visitor {
|
||||||
|
visitText(text: i18nAst.Text, context: any): any { return text.value; }
|
||||||
|
|
||||||
|
visitContainer(container: i18nAst.Container, context: any): any {
|
||||||
|
return `[${container.children.map(child => child.visit(this)).join(', ')}]`;
|
||||||
|
}
|
||||||
|
|
||||||
|
visitIcu(icu: i18nAst.Icu, context: any): any {
|
||||||
|
let strCases = Object.keys(icu.cases).map((k: string) => `${k} {${icu.cases[k].visit(this)}}`);
|
||||||
|
return `{${icu.expression}, ${icu.type}, ${strCases.join(', ')}}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
visitTagPlaceholder(ph: i18nAst.TagPlaceholder, context: any): any {
|
||||||
|
return ph.isVoid ?
|
||||||
|
`<ph tag name="${ph.startName}"/>` :
|
||||||
|
`<ph tag name="${ph.startName}">${ph.children.map(child => child.visit(this)).join(', ')}</ph name="${ph.closeName}">`;
|
||||||
|
}
|
||||||
|
|
||||||
|
visitPlaceholder(ph: i18nAst.Placeholder, context: any): any {
|
||||||
|
return `<ph name="${ph.name}">${ph.value}</ph>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
visitIcuPlaceholder(ph: i18nAst.IcuPlaceholder, context?: any): any {
|
||||||
|
return `<ph icu name="${ph.name}">${ph.value.visit(this)}</ph>`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const serializerVisitor = new _SerializerVisitor();
|
||||||
|
|
||||||
|
export function serializeAst(ast: i18nAst.Node[]): string[] {
|
||||||
|
return ast.map(a => a.visit(serializerVisitor, null));
|
||||||
|
}
|
@ -6,58 +6,53 @@
|
|||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {ParseSourceSpan} from "../parse_util";
|
import {ParseSourceSpan} from '../parse_util';
|
||||||
|
|
||||||
export interface I18nNode {
|
export class Message {
|
||||||
visit(visitor: Visitor, context?: any): any;
|
constructor(public nodes: Node[], public meaning: string, public description: string) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Text implements I18nNode {
|
export interface Node { visit(visitor: Visitor, context?: any): any; }
|
||||||
|
|
||||||
|
export class Text implements Node {
|
||||||
constructor(public value: string, public sourceSpan: ParseSourceSpan) {}
|
constructor(public value: string, public sourceSpan: ParseSourceSpan) {}
|
||||||
|
|
||||||
visit(visitor: Visitor, context?: any): any {
|
visit(visitor: Visitor, context?: any): any { return visitor.visitText(this, context); }
|
||||||
return visitor.visitText(this, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Container implements I18nNode {
|
export class Container implements Node {
|
||||||
constructor(public children: I18nNode[], public sourceSpan: ParseSourceSpan) {}
|
constructor(public children: Node[], public sourceSpan: ParseSourceSpan) {}
|
||||||
|
|
||||||
visit(visitor: Visitor, context?: any): any {
|
visit(visitor: Visitor, context?: any): any { return visitor.visitContainer(this, context); }
|
||||||
return visitor.visitContainer(this, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Icu implements I18nNode {
|
export class Icu implements Node {
|
||||||
constructor(public expression: string, public type: string, public cases: {[k: string]: I18nNode}, public sourceSpan: ParseSourceSpan) {}
|
constructor(
|
||||||
|
public expression: string, public type: string, public cases: {[k: string]: Node},
|
||||||
|
public sourceSpan: ParseSourceSpan) {}
|
||||||
|
|
||||||
visit(visitor: Visitor, context?: any): any {
|
visit(visitor: Visitor, context?: any): any { return visitor.visitIcu(this, context); }
|
||||||
return visitor.visitIcu(this, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class TagPlaceholder {
|
export class TagPlaceholder {
|
||||||
constructor(public name: string, public attrs: {[k: string]: string}, public children: I18nNode[], public sourceSpan: ParseSourceSpan) {}
|
constructor(
|
||||||
|
public tag: string, public attrs: {[k: string]: string}, public startName: string,
|
||||||
|
public closeName: string, public children: Node[], public isVoid: boolean,
|
||||||
|
public sourceSpan: ParseSourceSpan) {}
|
||||||
|
|
||||||
visit(visitor: Visitor, context?: any): any {
|
visit(visitor: Visitor, context?: any): any { return visitor.visitTagPlaceholder(this, context); }
|
||||||
return visitor.visitTagPlaceholder(this, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Placeholder {
|
export class Placeholder {
|
||||||
constructor(public value: string, public name: string = '', public sourceSpan: ParseSourceSpan) {}
|
constructor(public value: string, public name: string = '', public sourceSpan: ParseSourceSpan) {}
|
||||||
|
|
||||||
visit(visitor: Visitor, context?: any): any {
|
visit(visitor: Visitor, context?: any): any { return visitor.visitPlaceholder(this, context); }
|
||||||
return visitor.visitPlaceholder(this, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class IcuPlaceholder {
|
export class IcuPlaceholder {
|
||||||
constructor(public value: Icu, public name: string = '', public sourceSpan: ParseSourceSpan) {}
|
constructor(public value: Icu, public name: string = '', public sourceSpan: ParseSourceSpan) {}
|
||||||
|
|
||||||
visit(visitor: Visitor, context?: any): any {
|
visit(visitor: Visitor, context?: any): any { return visitor.visitIcuPlaceholder(this, context); }
|
||||||
return visitor.visitIcuPlaceholder(this, context);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Visitor {
|
export interface Visitor {
|
||||||
@ -68,7 +63,3 @@ export interface Visitor {
|
|||||||
visitPlaceholder(ph: Placeholder, context?: any): any;
|
visitPlaceholder(ph: Placeholder, context?: any): any;
|
||||||
visitIcuPlaceholder(ph: IcuPlaceholder, context?: any): any;
|
visitIcuPlaceholder(ph: IcuPlaceholder, context?: any): any;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,70 +6,78 @@
|
|||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { extractAstMessages} from './extractor';
|
|
||||||
import * as hAst from '../html_ast';
|
|
||||||
import * as i18nAst from './i18n_ast';
|
|
||||||
import {Parser as ExpressionParser} from '../expression_parser/parser';
|
|
||||||
import {Lexer as ExpressionLexer} from '../expression_parser/lexer';
|
import {Lexer as ExpressionLexer} from '../expression_parser/lexer';
|
||||||
import {ParseSourceSpan} from "../parse_util";
|
import {Parser as ExpressionParser} from '../expression_parser/parser';
|
||||||
import {HtmlAst} from "../html_ast";
|
import * as hAst from '../html_ast';
|
||||||
import {extractPlaceholderName} from "@angular/compiler/src/i18n/shared";
|
import {getHtmlTagDefinition} from '../html_tags';
|
||||||
|
import {InterpolationConfig} from '../interpolation_config';
|
||||||
|
import {ParseSourceSpan} from '../parse_util';
|
||||||
|
|
||||||
export class Message {
|
import {extractAstMessages} from './extractor';
|
||||||
constructor(public nodes: i18nAst.I18nNode[], public meaning: string, public description: string) {}
|
import * as i18nAst from './i18n_ast';
|
||||||
}
|
import {PlaceholderRegistry} from './serializers/util';
|
||||||
|
import {extractPlaceholderName} from './shared';
|
||||||
|
|
||||||
// TODO: should get the interpolation config
|
|
||||||
export function extractI18nMessages(
|
export function extractI18nMessages(
|
||||||
sourceAst: HtmlAst[], implicitTags: string[],
|
sourceAst: hAst.HtmlAst[], interpolationConfig: InterpolationConfig, implicitTags: string[],
|
||||||
implicitAttrs: {[k: string]: string[]}): Message[] {
|
implicitAttrs: {[k: string]: string[]}): i18nAst.Message[] {
|
||||||
const extractionResult = extractAstMessages(sourceAst, implicitTags, implicitAttrs);
|
const extractionResult = extractAstMessages(sourceAst, implicitTags, implicitAttrs);
|
||||||
|
|
||||||
if (extractionResult.errors.length) {
|
if (extractionResult.errors.length) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const visitor = new _I18nVisitor(new ExpressionParser(new ExpressionLexer()));
|
const visitor =
|
||||||
|
new _I18nVisitor(new ExpressionParser(new ExpressionLexer()), interpolationConfig);
|
||||||
|
|
||||||
return extractionResult.messages.map((msg): Message => {
|
return extractionResult.messages.map((msg): i18nAst.Message => {
|
||||||
return new Message(visitor.convertToI18nAst(msg.nodes), msg.meaning, msg.description);
|
return new i18nAst.Message(visitor.convertToI18nAst(msg.nodes), msg.meaning, msg.description);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
class _I18nVisitor implements hAst.HtmlAstVisitor {
|
class _I18nVisitor implements hAst.HtmlAstVisitor {
|
||||||
private _isIcu: boolean;
|
private _isIcu: boolean;
|
||||||
private _icuDepth: number;
|
private _icuDepth: number;
|
||||||
|
private _placeholderRegistry: PlaceholderRegistry;
|
||||||
|
|
||||||
constructor(private _expressionParser: ExpressionParser) {}
|
constructor(
|
||||||
|
private _expressionParser: ExpressionParser,
|
||||||
|
private _interpolationConfig: InterpolationConfig) {}
|
||||||
|
|
||||||
visitElement(el:hAst.HtmlElementAst, context:any):i18nAst.I18nNode {
|
visitElement(el: hAst.HtmlElementAst, context: any): i18nAst.Node {
|
||||||
const children = hAst.htmlVisitAll(this, el.children);
|
const children = hAst.htmlVisitAll(this, el.children);
|
||||||
const attrs: {[k: string]: string} = {};
|
const attrs: {[k: string]: string} = {};
|
||||||
el.attrs.forEach(attr => {
|
el.attrs.forEach(attr => {
|
||||||
// Do not visit the attributes, translatable ones are top-level ASTs
|
// Do not visit the attributes, translatable ones are top-level ASTs
|
||||||
attrs[attr.name] = attr.value;
|
attrs[attr.name] = attr.value;
|
||||||
});
|
});
|
||||||
return new i18nAst.TagPlaceholder(el.name, attrs, children, el.sourceSpan);
|
|
||||||
|
const isVoid: boolean = getHtmlTagDefinition(el.name).isVoid;
|
||||||
|
const startPhName =
|
||||||
|
this._placeholderRegistry.getStartTagPlaceholderName(el.name, attrs, isVoid);
|
||||||
|
const closePhName = isVoid ? '' : this._placeholderRegistry.getCloseTagPlaceholderName(el.name);
|
||||||
|
|
||||||
|
return new i18nAst.TagPlaceholder(
|
||||||
|
el.name, attrs, startPhName, closePhName, children, isVoid, el.sourceSpan);
|
||||||
}
|
}
|
||||||
|
|
||||||
visitAttr(attr:hAst.HtmlAttrAst, context:any):i18nAst.I18nNode {
|
visitAttr(attr: hAst.HtmlAttrAst, context: any): i18nAst.Node {
|
||||||
return this._visitTextWithInterpolation(attr.value, attr.sourceSpan);
|
return this._visitTextWithInterpolation(attr.value, attr.sourceSpan);
|
||||||
}
|
}
|
||||||
|
|
||||||
visitText(text:hAst.HtmlTextAst, context:any):i18nAst.I18nNode {
|
visitText(text: hAst.HtmlTextAst, context: any): i18nAst.Node {
|
||||||
return this._visitTextWithInterpolation(text.value, text.sourceSpan);
|
return this._visitTextWithInterpolation(text.value, text.sourceSpan);
|
||||||
}
|
}
|
||||||
|
|
||||||
visitComment(comment:hAst.HtmlCommentAst, context:any):i18nAst.I18nNode {
|
visitComment(comment: hAst.HtmlCommentAst, context: any): i18nAst.Node { return null; }
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
visitExpansion(icu:hAst.HtmlExpansionAst, context:any):i18nAst.I18nNode {
|
visitExpansion(icu: hAst.HtmlExpansionAst, context: any): i18nAst.Node {
|
||||||
this._icuDepth++;
|
this._icuDepth++;
|
||||||
const i18nIcuCases: {[k: string]: i18nAst.I18nNode} = {};
|
const i18nIcuCases: {[k: string]: i18nAst.Node} = {};
|
||||||
const i18nIcu = new i18nAst.Icu(icu.switchValue, icu.type, i18nIcuCases, icu.sourceSpan);
|
const i18nIcu = new i18nAst.Icu(icu.switchValue, icu.type, i18nIcuCases, icu.sourceSpan);
|
||||||
icu.cases.forEach((caze): void => {
|
icu.cases.forEach((caze): void => {
|
||||||
i18nIcuCases[caze.value] = new i18nAst.Container(caze.expression.map((hAst) => hAst.visit(this, {})), caze.expSourceSpan);
|
i18nIcuCases[caze.value] = new i18nAst.Container(
|
||||||
|
caze.expression.map((hAst) => hAst.visit(this, {})), caze.expSourceSpan);
|
||||||
});
|
});
|
||||||
this._icuDepth--;
|
this._icuDepth--;
|
||||||
|
|
||||||
@ -79,21 +87,25 @@ class _I18nVisitor implements hAst.HtmlAstVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// else returns a placeholder
|
// else returns a placeholder
|
||||||
return new i18nAst.IcuPlaceholder(i18nIcu, 'icu', icu.sourceSpan);
|
const phName = this._placeholderRegistry.getPlaceholderName('ICU', icu.sourceSpan.toString());
|
||||||
|
return new i18nAst.IcuPlaceholder(i18nIcu, phName, icu.sourceSpan);
|
||||||
}
|
}
|
||||||
|
|
||||||
visitExpansionCase(icuCase:hAst.HtmlExpansionCaseAst, context:any):i18nAst.I18nNode {
|
visitExpansionCase(icuCase: hAst.HtmlExpansionCaseAst, context: any): i18nAst.Node {
|
||||||
throw new Error('Unreachable code');
|
throw new Error('Unreachable code');
|
||||||
}
|
}
|
||||||
|
|
||||||
public convertToI18nAst(htmlAsts: hAst.HtmlAst[]): i18nAst.I18nNode[] {
|
public convertToI18nAst(htmlAsts: hAst.HtmlAst[]): i18nAst.Node[] {
|
||||||
this._isIcu = htmlAsts.length == 1 && htmlAsts[0] instanceof hAst.HtmlExpansionAst;
|
this._isIcu = htmlAsts.length == 1 && htmlAsts[0] instanceof hAst.HtmlExpansionAst;
|
||||||
this._icuDepth = 0;
|
this._icuDepth = 0;
|
||||||
|
this._placeholderRegistry = new PlaceholderRegistry();
|
||||||
|
|
||||||
return hAst.htmlVisitAll(this, htmlAsts, {});
|
return hAst.htmlVisitAll(this, htmlAsts, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _visitTextWithInterpolation(text: string, sourceSpan: ParseSourceSpan): i18nAst.I18nNode {
|
private _visitTextWithInterpolation(text: string, sourceSpan: ParseSourceSpan): i18nAst.Node {
|
||||||
const splitInterpolation = this._expressionParser.splitInterpolation(text, sourceSpan.start.toString());
|
const splitInterpolation = this._expressionParser.splitInterpolation(
|
||||||
|
text, sourceSpan.start.toString(), this._interpolationConfig);
|
||||||
|
|
||||||
if (!splitInterpolation) {
|
if (!splitInterpolation) {
|
||||||
// No expression, return a single text
|
// No expression, return a single text
|
||||||
@ -101,23 +113,26 @@ class _I18nVisitor implements hAst.HtmlAstVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return a group of text + expressions
|
// Return a group of text + expressions
|
||||||
const nodes: i18nAst.I18nNode[] = [];
|
const nodes: i18nAst.Node[] = [];
|
||||||
const container = new i18nAst.Container(nodes, sourceSpan);
|
const container = new i18nAst.Container(nodes, sourceSpan);
|
||||||
|
|
||||||
for (let i = 0; i < splitInterpolation.strings.length - 1; i++) {
|
for (let i = 0; i < splitInterpolation.strings.length - 1; i++) {
|
||||||
const expression = splitInterpolation.expressions[i];
|
const expression = splitInterpolation.expressions[i];
|
||||||
const phName = extractPlaceholderName(expression);
|
const baseName = extractPlaceholderName(expression) || 'INTERPOLATION';
|
||||||
nodes.push(
|
const phName = this._placeholderRegistry.getPlaceholderName(baseName, expression);
|
||||||
new i18nAst.Text(splitInterpolation.strings[i], sourceSpan),
|
|
||||||
new i18nAst.Placeholder(expression, phName, sourceSpan)
|
if (splitInterpolation.strings[i].length) {
|
||||||
)
|
nodes.push(new i18nAst.Text(splitInterpolation.strings[i], sourceSpan));
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes.push(new i18nAst.Placeholder(expression, phName, sourceSpan));
|
||||||
}
|
}
|
||||||
|
|
||||||
// The last index contains no expression
|
// The last index contains no expression
|
||||||
const lastStringIdx = splitInterpolation.strings.length - 1;
|
const lastStringIdx = splitInterpolation.strings.length - 1;
|
||||||
|
if (splitInterpolation.strings[lastStringIdx].length) {
|
||||||
nodes.push(new i18nAst.Text(splitInterpolation.strings[lastStringIdx], sourceSpan));
|
nodes.push(new i18nAst.Text(splitInterpolation.strings[lastStringIdx], sourceSpan));
|
||||||
|
}
|
||||||
return container;
|
return container;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,15 @@
|
|||||||
|
/**
|
||||||
|
* @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 * as i18nAst from '../i18n_ast';
|
||||||
|
|
||||||
|
export interface Serializer {
|
||||||
|
write(messageMap: {[k: string]: i18nAst.Message}): string;
|
||||||
|
|
||||||
|
load(content: string): {[k: string]: i18nAst.Node[]};
|
||||||
|
}
|
122
modules/@angular/compiler/src/i18n/serializers/util.ts
Normal file
122
modules/@angular/compiler/src/i18n/serializers/util.ts
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
|
||||||
|
const TAG_TO_PLACEHOLDER_NAMES: {[k: string]: string} = {
|
||||||
|
'A': 'LINK',
|
||||||
|
'B': 'BOLD_TEXT',
|
||||||
|
'BR': 'LINE_BREAK',
|
||||||
|
'EM': 'EMPHASISED_TEXT',
|
||||||
|
'H1': 'HEADING_LEVEL1',
|
||||||
|
'H2': 'HEADING_LEVEL2',
|
||||||
|
'H3': 'HEADING_LEVEL3',
|
||||||
|
'H4': 'HEADING_LEVEL4',
|
||||||
|
'H5': 'HEADING_LEVEL5',
|
||||||
|
'H6': 'HEADING_LEVEL6',
|
||||||
|
'HR': 'HORIZONTAL_RULE',
|
||||||
|
'I': 'ITALIC_TEXT',
|
||||||
|
'LI': 'LIST_ITEM',
|
||||||
|
'LINK': 'MEDIA_LINK',
|
||||||
|
'OL': 'ORDERED_LIST',
|
||||||
|
'P': 'PARAGRAPH',
|
||||||
|
'Q': 'QUOTATION',
|
||||||
|
'S': 'STRIKETHROUGH_TEXT',
|
||||||
|
'SMALL': 'SMALL_TEXT',
|
||||||
|
'SUB': 'SUBSTRIPT',
|
||||||
|
'SUP': 'SUPERSCRIPT',
|
||||||
|
'TBODY': 'TABLE_BODY',
|
||||||
|
'TD': 'TABLE_CELL',
|
||||||
|
'TFOOT': 'TABLE_FOOTER',
|
||||||
|
'TH': 'TABLE_HEADER_CELL',
|
||||||
|
'THEAD': 'TABLE_HEADER',
|
||||||
|
'TR': 'TABLE_ROW',
|
||||||
|
'TT': 'MONOSPACED_TEXT',
|
||||||
|
'U': 'UNDERLINED_TEXT',
|
||||||
|
'UL': 'UNORDERED_LIST',
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates unique names for placeholder with different content
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export class PlaceholderRegistry {
|
||||||
|
private _placeHolderNameCounts: {[k: string]: number} = {};
|
||||||
|
private _signatureToName: {[k: string]: string} = {};
|
||||||
|
|
||||||
|
getStartTagPlaceholderName(tag: string, attrs: {[k: string]: string}, isVoid: boolean): string {
|
||||||
|
const signature = this._hashTag(tag, attrs, isVoid);
|
||||||
|
if (this._signatureToName[signature]) {
|
||||||
|
return this._signatureToName[signature];
|
||||||
|
}
|
||||||
|
|
||||||
|
const upperTag = tag.toUpperCase();
|
||||||
|
const baseName = TAG_TO_PLACEHOLDER_NAMES[upperTag] || `TAG_${upperTag}`;
|
||||||
|
const name = this._generateUniqueName(isVoid ? baseName : `START_${baseName}`);
|
||||||
|
|
||||||
|
this._signatureToName[signature] = name;
|
||||||
|
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
getCloseTagPlaceholderName(tag: string): string {
|
||||||
|
const signature = this._hashClosingTag(tag);
|
||||||
|
if (this._signatureToName[signature]) {
|
||||||
|
return this._signatureToName[signature];
|
||||||
|
}
|
||||||
|
|
||||||
|
const upperTag = tag.toUpperCase();
|
||||||
|
const baseName = TAG_TO_PLACEHOLDER_NAMES[upperTag] || `TAG_${upperTag}`;
|
||||||
|
const name = this._generateUniqueName(`CLOSE_${baseName}`);
|
||||||
|
|
||||||
|
this._signatureToName[signature] = name;
|
||||||
|
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
getPlaceholderName(name: string, content: string): string {
|
||||||
|
const upperName = name.toUpperCase();
|
||||||
|
const signature = `PH: ${upperName}=${content}`;
|
||||||
|
if (this._signatureToName[signature]) {
|
||||||
|
return this._signatureToName[signature];
|
||||||
|
}
|
||||||
|
|
||||||
|
const uniqueName = this._generateUniqueName(upperName);
|
||||||
|
this._signatureToName[signature] = uniqueName;
|
||||||
|
|
||||||
|
return uniqueName;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _hashTag(tag: string, attrs: {[k: string]: string}, isVoid: boolean): string {
|
||||||
|
const start = `<${tag.toUpperCase()}`;
|
||||||
|
const strAttrs =
|
||||||
|
Object.getOwnPropertyNames(attrs).sort().map((name) => ` ${name}=${attrs[name]}`).join('');
|
||||||
|
const end = isVoid ? '/>' : `></${tag.toUpperCase()}>`;
|
||||||
|
|
||||||
|
return start + strAttrs + end;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _hashClosingTag(tag: string): string {
|
||||||
|
return this._hashTag(`/${tag.toUpperCase()}`, {}, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _generateUniqueName(base: string): string {
|
||||||
|
let name = base;
|
||||||
|
let next = this._placeHolderNameCounts[name];
|
||||||
|
|
||||||
|
if (!next) {
|
||||||
|
next = 1;
|
||||||
|
} else {
|
||||||
|
name += `_${next}`;
|
||||||
|
next++;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._placeHolderNameCounts[base] = next;
|
||||||
|
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,101 @@
|
|||||||
|
/**
|
||||||
|
* @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 {ListWrapper} from '../../facade/collection';
|
||||||
|
import * as i18nAst from '../i18n_ast';
|
||||||
|
|
||||||
|
import {Serializer} from './serializer';
|
||||||
|
import * as xml from './xml_helper';
|
||||||
|
|
||||||
|
const _MESSAGES_TAG = 'messagebundle';
|
||||||
|
const _MESSAGE_TAG = 'msg';
|
||||||
|
const _PLACEHOLDER_TAG = 'ph';
|
||||||
|
const _EXEMPLE_TAG = 'ex';
|
||||||
|
|
||||||
|
export class XmbSerializer implements Serializer {
|
||||||
|
// TODO(vicb): DOCTYPE
|
||||||
|
write(messageMap: {[k: string]: i18nAst.Message}): string {
|
||||||
|
const visitor = new _Visitor();
|
||||||
|
const declaration = new xml.Declaration({version: '1.0', encoding: 'UTF-8'});
|
||||||
|
let rootNode = new xml.Tag(_MESSAGES_TAG);
|
||||||
|
rootNode.children.push(new xml.Text('\n'));
|
||||||
|
|
||||||
|
Object.getOwnPropertyNames(messageMap).forEach((id) => {
|
||||||
|
const message = messageMap[id];
|
||||||
|
let attrs: {[k: string]: string} = {id};
|
||||||
|
|
||||||
|
if (message.description) {
|
||||||
|
attrs['desc'] = message.description;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message.meaning) {
|
||||||
|
attrs['meaning'] = message.meaning;
|
||||||
|
}
|
||||||
|
|
||||||
|
rootNode.children.push(
|
||||||
|
new xml.Text(' '), new xml.Tag(_MESSAGE_TAG, attrs, visitor.serialize(message.nodes)),
|
||||||
|
new xml.Text('\n'));
|
||||||
|
});
|
||||||
|
|
||||||
|
return xml.serialize([
|
||||||
|
declaration,
|
||||||
|
new xml.Text('\n'),
|
||||||
|
rootNode,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
load(content: string): {[k: string]: i18nAst.Node[]} { throw new Error('Unsupported'); }
|
||||||
|
}
|
||||||
|
|
||||||
|
class _Visitor implements i18nAst.Visitor {
|
||||||
|
visitText(text: i18nAst.Text, context?: any): xml.Node[] { return [new xml.Text(text.value)]; }
|
||||||
|
|
||||||
|
visitContainer(container: i18nAst.Container, context?: any): xml.Node[] {
|
||||||
|
const nodes: xml.Node[] = [];
|
||||||
|
container.children.forEach((node: i18nAst.Node) => nodes.push(...node.visit(this)));
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
visitIcu(icu: i18nAst.Icu, context?: any): xml.Node[] {
|
||||||
|
const nodes = [new xml.Text(`{${icu.expression}, ${icu.type}, `)];
|
||||||
|
|
||||||
|
Object.getOwnPropertyNames(icu.cases).forEach((c: string) => {
|
||||||
|
nodes.push(new xml.Text(`${c} {`), ...icu.cases[c].visit(this), new xml.Text(`}`));
|
||||||
|
});
|
||||||
|
|
||||||
|
nodes.push(new xml.Text(`}`));
|
||||||
|
|
||||||
|
return nodes;
|
||||||
|
}
|
||||||
|
|
||||||
|
visitTagPlaceholder(ph: i18nAst.TagPlaceholder, context?: any): xml.Node[] {
|
||||||
|
const startEx = new xml.Tag(_EXEMPLE_TAG, {}, [new xml.Text(`<${ph.tag}>`)]);
|
||||||
|
const startTagPh = new xml.Tag(_PLACEHOLDER_TAG, {name: ph.startName}, [startEx]);
|
||||||
|
if (ph.isVoid) {
|
||||||
|
// void tags have no children nor closing tags
|
||||||
|
return [startTagPh];
|
||||||
|
}
|
||||||
|
|
||||||
|
const closeEx = new xml.Tag(_EXEMPLE_TAG, {}, [new xml.Text(`</${ph.tag}>`)]);
|
||||||
|
const closeTagPh = new xml.Tag(_PLACEHOLDER_TAG, {name: ph.closeName}, [closeEx]);
|
||||||
|
|
||||||
|
return [startTagPh, ...this.serialize(ph.children), closeTagPh];
|
||||||
|
}
|
||||||
|
|
||||||
|
visitPlaceholder(ph: i18nAst.Placeholder, context?: any): xml.Node[] {
|
||||||
|
return [new xml.Tag(_PLACEHOLDER_TAG, {name: ph.name})];
|
||||||
|
}
|
||||||
|
|
||||||
|
visitIcuPlaceholder(ph: i18nAst.IcuPlaceholder, context?: any): xml.Node[] {
|
||||||
|
return [new xml.Tag(_PLACEHOLDER_TAG, {name: ph.name})];
|
||||||
|
}
|
||||||
|
|
||||||
|
serialize(nodes: i18nAst.Node[]): xml.Node[] {
|
||||||
|
return ListWrapper.flatten(nodes.map(node => node.visit(this)));
|
||||||
|
}
|
||||||
|
}
|
93
modules/@angular/compiler/src/i18n/serializers/xml_helper.ts
Normal file
93
modules/@angular/compiler/src/i18n/serializers/xml_helper.ts
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface IVisitor {
|
||||||
|
visitTag(tag: Tag): any;
|
||||||
|
visitText(text: Text): any;
|
||||||
|
visitDeclaration(decl: Declaration): any;
|
||||||
|
}
|
||||||
|
|
||||||
|
class _Visitor implements IVisitor {
|
||||||
|
visitTag(tag: Tag): string {
|
||||||
|
const strAttrs = this._serializeAttributes(tag.attrs);
|
||||||
|
|
||||||
|
if (tag.children.length == 0) {
|
||||||
|
return `<${tag.name}${strAttrs}/>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
const strChildren = tag.children.map(node => node.visit(this));
|
||||||
|
return `<${tag.name}${strAttrs}>${strChildren.join('')}</${tag.name}>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
visitText(text: Text): string { return text.value; }
|
||||||
|
|
||||||
|
visitDeclaration(decl: Declaration): string {
|
||||||
|
return `<? xml${this._serializeAttributes(decl.attrs)} ?>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _serializeAttributes(attrs: {[k: string]: string}) {
|
||||||
|
const strAttrs = Object.getOwnPropertyNames(attrs)
|
||||||
|
.map((name: string) => `${name}="${attrs[name]}"`)
|
||||||
|
.join(' ');
|
||||||
|
return strAttrs.length > 0 ? ' ' + strAttrs : '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const _visitor = new _Visitor();
|
||||||
|
|
||||||
|
export function serialize(nodes: Node[]): string {
|
||||||
|
return nodes.map((node: Node): string => node.visit(_visitor)).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Node { visit(visitor: IVisitor): any; }
|
||||||
|
|
||||||
|
export class Declaration implements Node {
|
||||||
|
public attrs: {[k: string]: string} = {};
|
||||||
|
|
||||||
|
constructor(unescapedAttrs: {[k: string]: string}) {
|
||||||
|
Object.getOwnPropertyNames(unescapedAttrs).forEach((k: string) => {
|
||||||
|
this.attrs[k] = _escapeXml(unescapedAttrs[k]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
visit(visitor: IVisitor): any { return visitor.visitDeclaration(this); }
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Tag implements Node {
|
||||||
|
public attrs: {[k: string]: string} = {};
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public name: string, unescapedAttrs: {[k: string]: string} = {},
|
||||||
|
public children: Node[] = []) {
|
||||||
|
Object.getOwnPropertyNames(unescapedAttrs).forEach((k: string) => {
|
||||||
|
this.attrs[k] = _escapeXml(unescapedAttrs[k]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
visit(visitor: IVisitor): any { return visitor.visitTag(this); }
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Text implements Node {
|
||||||
|
value: string;
|
||||||
|
constructor(unescapedValue: string) { this.value = _escapeXml(unescapedValue); };
|
||||||
|
|
||||||
|
visit(visitor: IVisitor): any { return visitor.visitText(this); }
|
||||||
|
}
|
||||||
|
|
||||||
|
const _ESCAPED_CHARS: [RegExp, string][] = [
|
||||||
|
[/&/g, '&'],
|
||||||
|
[/"/g, '"'],
|
||||||
|
[/'/g, '''],
|
||||||
|
[/</g, '<'],
|
||||||
|
[/>/g, '>'],
|
||||||
|
];
|
||||||
|
|
||||||
|
function _escapeXml(text: string): string {
|
||||||
|
return _ESCAPED_CHARS.reduce(
|
||||||
|
(text: string, entry: [RegExp, string]) => text.replace(entry[0], entry[1]), text);
|
||||||
|
}
|
@ -175,8 +175,7 @@ export function extractPhNameFromInterpolation(input: string, index: number): st
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function extractPlaceholderName(input: string): string {
|
export function extractPlaceholderName(input: string): string {
|
||||||
const matches = StringWrapper.split(input, _CUSTOM_PH_EXP);
|
return StringWrapper.split(input, _CUSTOM_PH_EXP)[1];
|
||||||
return matches[1] || `interpolation`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {RegExpWrapper, isBlank, isPresent} from '../facade/lang';
|
import {RegExpWrapper, isPresent} from '../facade/lang';
|
||||||
import {HtmlAst, HtmlElementAst} from '../html_ast';
|
import {HtmlAst, HtmlElementAst} from '../html_ast';
|
||||||
import {HtmlParser} from '../html_parser';
|
import {HtmlParser} from '../html_parser';
|
||||||
import {ParseError, ParseSourceSpan} from '../parse_util';
|
import {ParseError, ParseSourceSpan} from '../parse_util';
|
||||||
|
91
modules/@angular/compiler/test/i18n/catalog_spec.ts
Normal file
91
modules/@angular/compiler/test/i18n/catalog_spec.ts
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
/**
|
||||||
|
* @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 {HtmlParser} from '@angular/compiler/src/html_parser';
|
||||||
|
import {Catalog, strHash} from '@angular/compiler/src/i18n/catalog';
|
||||||
|
import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/interpolation_config';
|
||||||
|
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||||
|
|
||||||
|
import Serializable = webdriver.Serializable;
|
||||||
|
import {Serializer} from '@angular/compiler/src/i18n/serializers/serializer';
|
||||||
|
import {serializeAst} from '@angular/compiler/src/i18n/catalog';
|
||||||
|
import * as i18nAst from '@angular/compiler/src/i18n/i18n_ast';
|
||||||
|
|
||||||
|
export function main(): void {
|
||||||
|
ddescribe('Catalog', () => {
|
||||||
|
|
||||||
|
describe('write', () => {
|
||||||
|
let catalog: Catalog;
|
||||||
|
|
||||||
|
beforeEach(() => { catalog = new Catalog(new HtmlParser, [], {}); });
|
||||||
|
|
||||||
|
it('should extract the message to the catalog', () => {
|
||||||
|
catalog.updateFromTemplate(
|
||||||
|
'<p i18n="m|d">Translate Me</p>', 'url', DEFAULT_INTERPOLATION_CONFIG);
|
||||||
|
expect(humanizeCatalog(catalog)).toEqual([
|
||||||
|
'a486901=Translate Me',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should extract the same message with different meaning in different entries', () => {
|
||||||
|
catalog.updateFromTemplate(
|
||||||
|
'<p i18n="m|d">Translate Me</p><p i18n>Translate Me</p>', 'url',
|
||||||
|
DEFAULT_INTERPOLATION_CONFIG);
|
||||||
|
expect(humanizeCatalog(catalog)).toEqual([
|
||||||
|
'a486901=Translate Me',
|
||||||
|
'8475f2cc=Translate Me',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe(
|
||||||
|
'load', () => {
|
||||||
|
// TODO
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('strHash', () => {
|
||||||
|
it('should return a hash value', () => {
|
||||||
|
// https://github.com/google/closure-library/blob/1fb19a857b96b74e6523f3e9d33080baf25be046/closure/goog/string/string_test.js#L1115
|
||||||
|
expectHash('', 0);
|
||||||
|
expectHash('foo', 101574);
|
||||||
|
expectHash('\uAAAAfoo', 1301670364);
|
||||||
|
expectHash('a', 92567585, 5);
|
||||||
|
expectHash('a', 2869595232, 6);
|
||||||
|
expectHash('a', 3058106369, 7);
|
||||||
|
expectHash('a', 312017024, 8);
|
||||||
|
expectHash('a', 2929737728, 1024);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
class _TestSerializer implements Serializer {
|
||||||
|
write(messageMap: {[k: string]: i18nAst.Message}): string {
|
||||||
|
return Object.keys(messageMap)
|
||||||
|
.map(id => `${id}=${serializeAst(messageMap[id].nodes)}`)
|
||||||
|
.join('//');
|
||||||
|
}
|
||||||
|
|
||||||
|
load(content: string): {[k: string]: i18nAst.Node[]} { return null; }
|
||||||
|
}
|
||||||
|
|
||||||
|
function humanizeCatalog(catalog: Catalog): string[] {
|
||||||
|
return catalog.write(new _TestSerializer()).split('//');
|
||||||
|
}
|
||||||
|
|
||||||
|
function expectHash(text: string, decimal: number, repeat: number = 1) {
|
||||||
|
let acc = text;
|
||||||
|
for (let i = 1; i < repeat; i++) {
|
||||||
|
acc += text;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hash = strHash(acc);
|
||||||
|
expect(typeof(hash)).toEqual('string');
|
||||||
|
expect(hash.length > 0).toBe(true);
|
||||||
|
expect(parseInt(hash, 16)).toEqual(decimal);
|
||||||
|
}
|
@ -263,8 +263,8 @@ export function main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getExtractionResult(
|
function getExtractionResult(
|
||||||
html: string, implicitTags: string[],
|
html: string, implicitTags: string[], implicitAttrs:
|
||||||
implicitAttrs: {[k: string]: string[]}): ExtractionResult {
|
{[k: string]: string[]}): ExtractionResult {
|
||||||
const htmlParser = new HtmlParser();
|
const htmlParser = new HtmlParser();
|
||||||
const parseResult = htmlParser.parse(html, 'extractor spec', true);
|
const parseResult = htmlParser.parse(html, 'extractor spec', true);
|
||||||
if (parseResult.errors.length > 1) {
|
if (parseResult.errors.length > 1) {
|
||||||
@ -275,8 +275,8 @@ function getExtractionResult(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function extract(
|
function extract(
|
||||||
html: string, implicitTags: string[] = [],
|
html: string, implicitTags: string[] = [], implicitAttrs:
|
||||||
implicitAttrs: {[k: string]: string[]} = {}): [string[], string, string][] {
|
{[k: string]: string[]} = {}): [string[], string, string][] {
|
||||||
const messages = getExtractionResult(html, implicitTags, implicitAttrs).messages;
|
const messages = getExtractionResult(html, implicitTags, implicitAttrs).messages;
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
@ -287,8 +287,8 @@ function extract(
|
|||||||
}
|
}
|
||||||
|
|
||||||
function extractErrors(
|
function extractErrors(
|
||||||
html: string, implicitTags: string[] = [],
|
html: string, implicitTags: string[] = [], implicitAttrs:
|
||||||
implicitAttrs: {[k: string]: string[]} = {}): any[] {
|
{[k: string]: string[]} = {}): any[] {
|
||||||
const errors = getExtractionResult(html, implicitTags, implicitAttrs).errors;
|
const errors = getExtractionResult(html, implicitTags, implicitAttrs).errors;
|
||||||
|
|
||||||
return errors.map((e): [string, string] => [e.msg, e.span.toString()]);
|
return errors.map((e): [string, string] => [e.msg, e.span.toString()]);
|
||||||
|
@ -6,10 +6,12 @@
|
|||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {HtmlParser} from "@angular/compiler/src/html_parser";
|
import {HtmlParser} from '@angular/compiler/src/html_parser';
|
||||||
import * as i18nAst from "@angular/compiler/src/i18n/i18n_ast";
|
import {serializeAst} from '@angular/compiler/src/i18n/catalog';
|
||||||
import {ddescribe, describe, expect, it} from "@angular/core/testing/testing_internal";
|
import {extractI18nMessages} from '@angular/compiler/src/i18n/i18n_parser';
|
||||||
import {extractI18nMessages} from "@angular/compiler/src/i18n/i18n_parser";
|
import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/interpolation_config';
|
||||||
|
import {ddescribe, describe, expect, it} from '@angular/core/testing/testing_internal';
|
||||||
|
|
||||||
export function main() {
|
export function main() {
|
||||||
ddescribe('I18nParser', () => {
|
ddescribe('I18nParser', () => {
|
||||||
|
|
||||||
@ -22,7 +24,13 @@ export function main() {
|
|||||||
|
|
||||||
it('should extract from nested elements', () => {
|
it('should extract from nested elements', () => {
|
||||||
expect(extract('<div i18n="m|d">text<span><b>nested</b></span></div>')).toEqual([
|
expect(extract('<div i18n="m|d">text<span><b>nested</b></span></div>')).toEqual([
|
||||||
[['text', '<ph tag name="span"><ph tag name="b">nested</ph></ph>'], 'm', 'd'],
|
[
|
||||||
|
[
|
||||||
|
'text',
|
||||||
|
'<ph tag name="START_TAG_SPAN"><ph tag name="START_BOLD_TEXT">nested</ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_TAG_SPAN">'
|
||||||
|
],
|
||||||
|
'm', 'd'
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -31,6 +39,17 @@ export function main() {
|
|||||||
|
|
||||||
it('should not create a message for plain elements',
|
it('should not create a message for plain elements',
|
||||||
() => { expect(extract('<div></div>')).toEqual([]); });
|
() => { expect(extract('<div></div>')).toEqual([]); });
|
||||||
|
|
||||||
|
it('should suppoprt void elements', () => {
|
||||||
|
expect(extract('<div i18n="m|d"><p><br></p></div>')).toEqual([
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'<ph tag name="START_PARAGRAPH"><ph tag name="LINE_BREAK"/></ph name="CLOSE_PARAGRAPH">'
|
||||||
|
],
|
||||||
|
'm', 'd'
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('attributes', () => {
|
describe('attributes', () => {
|
||||||
@ -42,17 +61,26 @@ export function main() {
|
|||||||
|
|
||||||
it('should extract from attributes in translatable element', () => {
|
it('should extract from attributes in translatable element', () => {
|
||||||
expect(extract('<div i18n><p><b i18n-title="m|d" title="msg"></b></p></div>')).toEqual([
|
expect(extract('<div i18n><p><b i18n-title="m|d" title="msg"></b></p></div>')).toEqual([
|
||||||
[['<ph tag name="p"><ph tag name="b"></ph></ph>'], '', ''],
|
[
|
||||||
|
[
|
||||||
|
'<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">'
|
||||||
|
],
|
||||||
|
'', ''
|
||||||
|
],
|
||||||
[['msg'], 'm', 'd'],
|
[['msg'], 'm', 'd'],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should extract from attributes in translatable block', () => {
|
it('should extract from attributes in translatable block', () => {
|
||||||
expect(
|
expect(extract('<!-- i18n --><p><b i18n-title="m|d" title="msg"></b></p><!-- /i18n -->'))
|
||||||
extract('<!-- i18n --><p><b i18n-title="m|d" title="msg"></b></p><!-- /i18n -->'))
|
|
||||||
.toEqual([
|
.toEqual([
|
||||||
[['msg'], 'm', 'd'],
|
[['msg'], 'm', 'd'],
|
||||||
[['<ph tag name="p"><ph tag name="b"></ph></ph>'], '', ''],
|
[
|
||||||
|
[
|
||||||
|
'<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">'
|
||||||
|
],
|
||||||
|
'', ''
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -62,7 +90,12 @@ export function main() {
|
|||||||
'<!-- i18n -->{count, plural, =0 {<p><b i18n-title="m|d" title="msg"></b></p>}}<!-- /i18n -->'))
|
'<!-- i18n -->{count, plural, =0 {<p><b i18n-title="m|d" title="msg"></b></p>}}<!-- /i18n -->'))
|
||||||
.toEqual([
|
.toEqual([
|
||||||
[['msg'], 'm', 'd'],
|
[['msg'], 'm', 'd'],
|
||||||
[['{count, plural, =0 {[<ph tag name="p"><ph tag name="b"></ph></ph>]}}'], '', ''],
|
[
|
||||||
|
[
|
||||||
|
'{count, plural, =0 {[<ph tag name="START_PARAGRAPH"><ph tag name="START_BOLD_TEXT"></ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">]}}'
|
||||||
|
],
|
||||||
|
'', ''
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -80,13 +113,13 @@ export function main() {
|
|||||||
describe('interpolation', () => {
|
describe('interpolation', () => {
|
||||||
it('should replace interpolation with placeholder', () => {
|
it('should replace interpolation with placeholder', () => {
|
||||||
expect(extract('<div i18n="m|d">before{{ exp }}after</div>')).toEqual([
|
expect(extract('<div i18n="m|d">before{{ exp }}after</div>')).toEqual([
|
||||||
[['[before, <ph name="interpolation"> exp </ph>, after]'], 'm', 'd'],
|
[['[before, <ph name="INTERPOLATION"> exp </ph>, after]'], 'm', 'd'],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should support named interpolation', () => {
|
it('should support named interpolation', () => {
|
||||||
expect(extract('<div i18n="m|d">before{{ exp //i18n(ph="teSt") }}after</div>')).toEqual([
|
expect(extract('<div i18n="m|d">before{{ exp //i18n(ph="teSt") }}after</div>')).toEqual([
|
||||||
[['[before, <ph name="teSt"> exp //i18n(ph="teSt") </ph>, after]'], 'm', 'd'],
|
[['[before, <ph name="TEST"> exp //i18n(ph="teSt") </ph>, after]'], 'm', 'd'],
|
||||||
]);
|
]);
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
@ -104,10 +137,14 @@ export function main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should extract all siblings', () => {
|
it('should extract all siblings', () => {
|
||||||
expect(
|
expect(extract(`<!-- i18n -->text<p>html<b>nested</b></p><!-- /i18n -->`)).toEqual([
|
||||||
extract(`<!-- i18n -->text<p>html<b>nested</b></p><!-- /i18n -->`))
|
[
|
||||||
.toEqual([
|
[
|
||||||
[[ 'text', '<ph tag name="p">html, <ph tag name="b">nested</ph></ph>'], '', '' ],
|
'text',
|
||||||
|
'<ph tag name="START_PARAGRAPH">html, <ph tag name="START_BOLD_TEXT">nested</ph name="CLOSE_BOLD_TEXT"></ph name="CLOSE_PARAGRAPH">'
|
||||||
|
],
|
||||||
|
'', ''
|
||||||
|
],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -121,7 +158,7 @@ export function main() {
|
|||||||
|
|
||||||
it('should extract as ICU + ph when not single child of an element', () => {
|
it('should extract as ICU + ph when not single child of an element', () => {
|
||||||
expect(extract('<div i18n="m|d">b{count, plural, =0 {zero}}a</div>')).toEqual([
|
expect(extract('<div i18n="m|d">b{count, plural, =0 {zero}}a</div>')).toEqual([
|
||||||
[[ 'b', '<ph icu name="icu">{count, plural, =0 {[zero]}}</ph>', 'a'], 'm', 'd'],
|
[['b', '<ph icu name="ICU">{count, plural, =0 {[zero]}}</ph>', 'a'], 'm', 'd'],
|
||||||
[['{count, plural, =0 {[zero]}}'], '', ''],
|
[['{count, plural, =0 {[zero]}}'], '', ''],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
@ -135,13 +172,20 @@ export function main() {
|
|||||||
it('should extract as ICU + ph when not single child of a block', () => {
|
it('should extract as ICU + ph when not single child of a block', () => {
|
||||||
expect(extract('<!-- i18n:m|d -->b{count, plural, =0 {zero}}a<!-- /i18n -->')).toEqual([
|
expect(extract('<!-- i18n:m|d -->b{count, plural, =0 {zero}}a<!-- /i18n -->')).toEqual([
|
||||||
[['{count, plural, =0 {[zero]}}'], '', ''],
|
[['{count, plural, =0 {[zero]}}'], '', ''],
|
||||||
[[ 'b', '<ph icu name="icu">{count, plural, =0 {[zero]}}</ph>', 'a'], 'm', 'd'],
|
[['b', '<ph icu name="ICU">{count, plural, =0 {[zero]}}</ph>', 'a'], 'm', 'd'],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not extract nested ICU messages', () => {
|
it('should not extract nested ICU messages', () => {
|
||||||
expect(extract('<div i18n="m|d">b{count, plural, =0 {{sex, gender, =m {m}}}}a</div>')).toEqual([
|
expect(extract('<div i18n="m|d">b{count, plural, =0 {{sex, gender, =m {m}}}}a</div>'))
|
||||||
[[ 'b', '<ph icu name="icu">{count, plural, =0 {[{sex, gender, =m {[m]}}]}}</ph>', 'a'], 'm', 'd'],
|
.toEqual([
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'b', '<ph icu name="ICU">{count, plural, =0 {[{sex, gender, =m {[m]}}]}}</ph>',
|
||||||
|
'a'
|
||||||
|
],
|
||||||
|
'm', 'd'
|
||||||
|
],
|
||||||
[['{count, plural, =0 {[{sex, gender, =m {[m]}}]}}'], '', ''],
|
[['{count, plural, =0 {[{sex, gender, =m {[m]}}]}}'], '', ''],
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
@ -163,40 +207,53 @@ export function main() {
|
|||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('placeholders', () => {
|
||||||
|
it('should reuse the same placeholder name for tags', () => {
|
||||||
|
expect(extract('<div i18n="m|d"><p>one</p><p>two</p><p other>three</p></div>')).toEqual([
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'<ph tag name="START_PARAGRAPH">one</ph name="CLOSE_PARAGRAPH">',
|
||||||
|
'<ph tag name="START_PARAGRAPH">two</ph name="CLOSE_PARAGRAPH">',
|
||||||
|
'<ph tag name="START_PARAGRAPH_1">three</ph name="CLOSE_PARAGRAPH">',
|
||||||
|
],
|
||||||
|
'm', 'd'
|
||||||
|
],
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
class _SerializerVisitor implements i18nAst.Visitor {
|
it('should reuse the same placeholder name for interpolations', () => {
|
||||||
visitText(text:i18nAst.Text, context:any):any {
|
expect(extract('<div i18n="m|d">{{ a }}{{ a }}{{ b }}</div>')).toEqual([
|
||||||
return text.value;
|
[
|
||||||
}
|
[
|
||||||
|
'[<ph name="INTERPOLATION"> a </ph>, <ph name="INTERPOLATION"> a </ph>, <ph name="INTERPOLATION_1"> b </ph>]'
|
||||||
|
],
|
||||||
|
'm', 'd'
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
visitContainer(container:i18nAst.Container, context:any):any {
|
it('should reuse the same placeholder name for icu messages', () => {
|
||||||
return `[${container.children.map(child => child.visit(this)).join(', ')}]`
|
expect(
|
||||||
}
|
extract(
|
||||||
|
'<div i18n="m|d">{count, plural, =0 {0}}{count, plural, =0 {0}}{count, plural, =1 {1}}</div>'))
|
||||||
|
.toEqual([
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'<ph icu name="ICU">{count, plural, =0 {[0]}}</ph>',
|
||||||
|
'<ph icu name="ICU">{count, plural, =0 {[0]}}</ph>',
|
||||||
|
'<ph icu name="ICU_1">{count, plural, =1 {[1]}}</ph>',
|
||||||
|
],
|
||||||
|
'm', 'd'
|
||||||
|
],
|
||||||
|
[['{count, plural, =0 {[0]}}'], '', ''],
|
||||||
|
[['{count, plural, =0 {[0]}}'], '', ''],
|
||||||
|
[['{count, plural, =1 {[1]}}'], '', ''],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
visitIcu(icu:i18nAst.Icu, context:any):any {
|
});
|
||||||
let strCases = Object.keys(icu.cases).map((k: string) => `${k} {${icu.cases[k].visit(this)}}`);
|
});
|
||||||
return `{${icu.expression}, ${icu.type}, ${strCases.join(', ')}}`
|
|
||||||
}
|
|
||||||
|
|
||||||
visitTagPlaceholder(ph:i18nAst.TagPlaceholder, context:any):any {
|
|
||||||
return `<ph tag name="${ph.name}">${ph.children.map(child => child.visit(this)).join(', ')}</ph>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
visitPlaceholder(ph:i18nAst.Placeholder, context:any):any {
|
|
||||||
return `<ph name="${ph.name}">${ph.value}</ph>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
visitIcuPlaceholder(ph:i18nAst.IcuPlaceholder, context?:any):any {
|
|
||||||
return `<ph icu name="${ph.name}">${ph.value.visit(this)}</ph>`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const serializerVisitor = new _SerializerVisitor();
|
|
||||||
|
|
||||||
export function serializeAst(ast: i18nAst.I18nNode[]): string[] {
|
|
||||||
return ast.map(a => a.visit(serializerVisitor, null));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function extract(
|
function extract(
|
||||||
@ -208,7 +265,8 @@ function extract(
|
|||||||
throw Error(`unexpected parse errors: ${parseResult.errors.join('\n')}`);
|
throw Error(`unexpected parse errors: ${parseResult.errors.join('\n')}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const messages = extractI18nMessages(parseResult.rootNodes, implicitTags, implicitAttrs);
|
const messages = extractI18nMessages(
|
||||||
|
parseResult.rootNodes, DEFAULT_INTERPOLATION_CONFIG, implicitTags, implicitAttrs);
|
||||||
|
|
||||||
// clang-format off
|
// clang-format off
|
||||||
// https://github.com/angular/clang-format/issues/35
|
// https://github.com/angular/clang-format/issues/35
|
||||||
@ -216,5 +274,3 @@ function extract(
|
|||||||
message => [serializeAst(message.nodes), message.meaning, message.description, ]) as [string[], string, string][];
|
message => [serializeAst(message.nodes), message.meaning, message.description, ]) as [string[], string, string][];
|
||||||
// clang-format on
|
// clang-format on
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
94
modules/@angular/compiler/test/i18n/serializers/util_spec.ts
Normal file
94
modules/@angular/compiler/test/i18n/serializers/util_spec.ts
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
/**
|
||||||
|
* @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 {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||||
|
|
||||||
|
import {PlaceholderRegistry} from '../../../src/i18n/serializers/util';
|
||||||
|
|
||||||
|
export function main(): void {
|
||||||
|
ddescribe('PlaceholderRegistry', () => {
|
||||||
|
let reg: PlaceholderRegistry;
|
||||||
|
|
||||||
|
beforeEach(() => { reg = new PlaceholderRegistry(); });
|
||||||
|
|
||||||
|
describe('tag placeholder', () => {
|
||||||
|
it('should generate names for well known tags', () => {
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {}, false)).toEqual('START_PARAGRAPH');
|
||||||
|
expect(reg.getCloseTagPlaceholderName('p')).toEqual('CLOSE_PARAGRAPH');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should generate names for custom tags', () => {
|
||||||
|
expect(reg.getStartTagPlaceholderName('my-cmp', {}, false)).toEqual('START_TAG_MY-CMP');
|
||||||
|
expect(reg.getCloseTagPlaceholderName('my-cmp')).toEqual('CLOSE_TAG_MY-CMP');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should generate the same name for the same tag', () => {
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {}, false)).toEqual('START_PARAGRAPH');
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {}, false)).toEqual('START_PARAGRAPH');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be case insensitive for tag name', () => {
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {}, false)).toEqual('START_PARAGRAPH');
|
||||||
|
expect(reg.getStartTagPlaceholderName('P', {}, false)).toEqual('START_PARAGRAPH');
|
||||||
|
expect(reg.getCloseTagPlaceholderName('p')).toEqual('CLOSE_PARAGRAPH');
|
||||||
|
expect(reg.getCloseTagPlaceholderName('P')).toEqual('CLOSE_PARAGRAPH');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should generate the same name for the same tag with the same attributes', () => {
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {foo: 'a', bar: 'b'}, false))
|
||||||
|
.toEqual('START_PARAGRAPH');
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {foo: 'a', bar: 'b'}, false))
|
||||||
|
.toEqual('START_PARAGRAPH');
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {bar: 'b', foo: 'a'}, false))
|
||||||
|
.toEqual('START_PARAGRAPH');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should generate different names for the same tag with different attributes', () => {
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {foo: 'a', bar: 'b'}, false))
|
||||||
|
.toEqual('START_PARAGRAPH');
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {foo: 'a'}, false)).toEqual('START_PARAGRAPH_1');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be case sensitive for attributes', () => {
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {foo: 'a', bar: 'b'}, false))
|
||||||
|
.toEqual('START_PARAGRAPH');
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {fOo: 'a', bar: 'b'}, false))
|
||||||
|
.toEqual('START_PARAGRAPH_1');
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {fOo: 'a', bAr: 'b'}, false))
|
||||||
|
.toEqual('START_PARAGRAPH_2');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should support void tags', () => {
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {}, true)).toEqual('PARAGRAPH');
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {}, true)).toEqual('PARAGRAPH');
|
||||||
|
expect(reg.getStartTagPlaceholderName('p', {other: 'true'}, true)).toEqual('PARAGRAPH_1');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('arbitrary placeholders', () => {
|
||||||
|
it('should generate the same name given the same name and content', () => {
|
||||||
|
expect(reg.getPlaceholderName('name', 'content')).toEqual('NAME');
|
||||||
|
expect(reg.getPlaceholderName('name', 'content')).toEqual('NAME');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should generate a different name given different content', () => {
|
||||||
|
expect(reg.getPlaceholderName('name', 'content1')).toEqual('NAME');
|
||||||
|
expect(reg.getPlaceholderName('name', 'content2')).toEqual('NAME_1');
|
||||||
|
expect(reg.getPlaceholderName('name', 'content3')).toEqual('NAME_2');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should generate a different name given different names', () => {
|
||||||
|
expect(reg.getPlaceholderName('name1', 'content')).toEqual('NAME1');
|
||||||
|
expect(reg.getPlaceholderName('name2', 'content')).toEqual('NAME2');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
68
modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts
Normal file
68
modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/**
|
||||||
|
* @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 {HtmlParser} from '@angular/compiler/src/html_parser';
|
||||||
|
import {Catalog} from '@angular/compiler/src/i18n/catalog';
|
||||||
|
import {XmbSerializer} from '@angular/compiler/src/i18n/serializers/xmb';
|
||||||
|
import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/interpolation_config';
|
||||||
|
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||||
|
|
||||||
|
|
||||||
|
export function main(): void {
|
||||||
|
ddescribe('XMB serializer', () => {
|
||||||
|
const HTML = `
|
||||||
|
<p>not translatable</p>
|
||||||
|
<p i18n>translatable element <b>with placeholders</b> {{ interpolation}}</p>
|
||||||
|
<!-- i18n -->{ count, plural, =0 {<p>test</p>}}<!-- /i18n -->
|
||||||
|
<p i18n="m|d">foo</p>
|
||||||
|
<p i18n>{ count, plural, =0 { { sex, gender, other {<p>deeply nested</p>}} }}</p>`;
|
||||||
|
|
||||||
|
const XMB = `<? xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<messagebundle>
|
||||||
|
<msg id="834fa53b">translatable element <ph name="START_BOLD_TEXT"><ex><b></ex></ph>with placeholders<ph name="CLOSE_BOLD_TEXT"><ex></b></ex></ph> <ph name="INTERPOLATION"/></msg>
|
||||||
|
<msg id="7a2843db">{ count, plural, =0 {<ph name="START_PARAGRAPH"><ex><p></ex></ph>test<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}}</msg>
|
||||||
|
<msg id="b45e58a5" desc="d" meaning="m">foo</msg>
|
||||||
|
<msg id="18ea85bc">{ count, plural, =0 {{ sex, gender, other {<ph name="START_PARAGRAPH"><ex><p></ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}} }}</msg>
|
||||||
|
</messagebundle>`;
|
||||||
|
|
||||||
|
it('should write a valid xmb file', () => { expect(toXmb(HTML)).toEqual(XMB); });
|
||||||
|
|
||||||
|
it('should throw when trying to load an xmb file', () => {
|
||||||
|
expect(() => {
|
||||||
|
const serializer = new XmbSerializer();
|
||||||
|
serializer.load(XMB);
|
||||||
|
}).toThrow();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function toXmb(html: string): string {
|
||||||
|
let catalog = new Catalog(new HtmlParser, [], {});
|
||||||
|
const serializer = new XmbSerializer();
|
||||||
|
|
||||||
|
catalog.updateFromTemplate(html, '', DEFAULT_INTERPOLATION_CONFIG);
|
||||||
|
|
||||||
|
return catalog.write(serializer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// <? xml version="1.0" encoding="UTF-8" ?><messagebundle><message id="834fa53b">translatable
|
||||||
|
// element <ph name="START_BOLD_TEXT"><ex><b></ex></ph>with placeholders<ph
|
||||||
|
// name="CLOSE_BOLD_TEXT"><ex></b></ex></ph> <ph name="INTERPOLATION"/></message><message
|
||||||
|
// id="7a2843db">{ count, plural, =0 {<ph name="START_PARAGRAPH"><ex><p></ex></ph>test<ph
|
||||||
|
// name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}}</message><message id="b45e58a5" description="d"
|
||||||
|
// meaning="m">foo</message><message id="18ea85bc">{ count, plural, =0 {{ sex, gender, other {<ph
|
||||||
|
// name="START_PARAGRAPH"><ex><p></ex></ph>deeply nested<ph
|
||||||
|
// name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}} }}</message></messagebundle>
|
||||||
|
// <? xml version="1.0" encoding="UTF-8" ?><messagebundle><message id="834fa53b">translatable
|
||||||
|
// element <ph name="START_BOLD_TEXT"><ex><b></ex></ph>with placeholders<ph
|
||||||
|
// name="CLOSE_BOLD_TEXT"><ex></b></ex></ph> <ph name="INTERPOLATION"/></message><message
|
||||||
|
// id="7a2843db">{ count, plural, =0 {<ph name="START_PARAGRAPH"><ex><p></ex></ph>test<ph
|
||||||
|
// name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}}</message><message id="18ea85bc">{ count,
|
||||||
|
// plural, =0 {{ sex, gender, other {<ph name="START_PARAGRAPH"><ex><p></ex></ph>deeply
|
||||||
|
// nested<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>}} }}</message><message id="b45e58a5"
|
||||||
|
// description="d" meaning="m">foo</message></messagebundle>
|
@ -0,0 +1,49 @@
|
|||||||
|
/**
|
||||||
|
* @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 {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
|
||||||
|
|
||||||
|
import * as xml from '../../../src/i18n/serializers/xml_helper';
|
||||||
|
|
||||||
|
export function main(): void {
|
||||||
|
ddescribe('XML helper', () => {
|
||||||
|
it('should serialize XML declaration', () => {
|
||||||
|
expect(xml.serialize([new xml.Declaration({version: '1.0'})]))
|
||||||
|
.toEqual('<? xml version="1.0" ?>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should serialize text node',
|
||||||
|
() => { expect(xml.serialize([new xml.Text('foo bar')])).toEqual('foo bar'); });
|
||||||
|
|
||||||
|
it('should escape text nodes',
|
||||||
|
() => { expect(xml.serialize([new xml.Text('<>')])).toEqual('<>'); });
|
||||||
|
|
||||||
|
it('should serialize xml nodes without children', () => {
|
||||||
|
expect(xml.serialize([new xml.Tag('el', {foo: 'bar'}, [])])).toEqual('<el foo="bar"/>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should serialize xml nodes with children', () => {
|
||||||
|
expect(xml.serialize([
|
||||||
|
new xml.Tag('parent', {}, [new xml.Tag('child', {}, [new xml.Text('content')])])
|
||||||
|
])).toEqual('<parent><child>content</child></parent>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should serialize node lists', () => {
|
||||||
|
expect(xml.serialize([
|
||||||
|
new xml.Tag('el', {order: '0'}, []),
|
||||||
|
new xml.Tag('el', {order: '1'}, []),
|
||||||
|
])).toEqual('<el order="0"/><el order="1"/>');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should escape attribute values', () => {
|
||||||
|
expect(xml.serialize([new xml.Tag('el', {foo: '<">'}, [])]))
|
||||||
|
.toEqual('<el foo="<">"/>');
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user