feat(i18n): update I18nHtmlParser to accept parsed messages

This commit is contained in:
vsavkin
2016-03-27 18:31:02 -07:00
committed by Victor Savkin
parent d7e1175df0
commit 756121acc1
4 changed files with 84 additions and 84 deletions

View File

@ -16,6 +16,7 @@ import {Message, id} from 'angular2/src/i18n/message';
import {Parser} from 'angular2/src/core/change_detection/parser/parser';
import {Lexer} from 'angular2/src/core/change_detection/parser/lexer';
import {StringMapWrapper} from 'angular2/src/facade/collection';
import {HtmlParser, HtmlParseTreeResult} from 'angular2/src/compiler/html_parser';
import {
HtmlAst,
@ -26,6 +27,7 @@ import {
HtmlCommentAst,
htmlVisitAll
} from 'angular2/src/compiler/html_ast';
import {serializeXmb, deserializeXmb} from 'angular2/src/i18n/xmb_serializer';
import {ParseError, ParseLocation} from 'angular2/src/compiler/parse_util';
import {humanizeDom} from '../../test/compiler/html_ast_spec_utils';
@ -34,7 +36,13 @@ export function main() {
function parse(template: string, messages: {[key: string]: string}): HtmlParseTreeResult {
var parser = new Parser(new Lexer());
let htmlParser = new HtmlParser();
return new I18nHtmlParser(htmlParser, parser, messages).parse(template, "someurl");
let msgs = '';
StringMapWrapper.forEach(messages, (v, k) => msgs += `<msg id="${k}">${v}</msg>`);
let res = deserializeXmb(`<message-bundle>${msgs}</message-bundle>`, 'someUrl');
return new I18nHtmlParser(htmlParser, parser, res.content, res.messages)
.parse(template, "someurl");
}
it("should delegate to the provided parser when no i18n", () => {
@ -112,6 +120,18 @@ export function main() {
]);
});
it("should preserve non-i18n attributes", () => {
let translations: {[key: string]: string} = {};
translations[id(new Message('message', null, null))] = 'another message';
expect(humanizeDom(parse('<div i18n value="b">message</div>', translations)))
.toEqual([
[HtmlElementAst, 'div', 0],
[HtmlAttrAst, 'value', "b"],
[HtmlTextAst, 'another message', 1]
]);
});
it('should extract from partitions', () => {
let translations: {[key: string]: string} = {};
translations[id(new Message('message1', 'meaning1', null))] = 'another message1';
@ -156,14 +176,6 @@ export function main() {
.toEqual([`Cannot find message for id '${mid}'`]);
});
it("should error when message cannot be parsed", () => {
let translations: {[key: string]: string} = {};
translations[id(new Message("some message", null, null))] = "<a>a</b>";
expect(humanizeErrors(parse("<div i18n>some message</div>", translations).errors))
.toEqual([`Unexpected closing tag "b"`]);
});
it("should error when a non-placeholder element appears in translation", () => {
let translations: {[key: string]: string} = {};
translations[id(new Message("some message", null, null))] = "<a>a</a>";
@ -180,11 +192,6 @@ export function main() {
.toEqual([`Missing "name" attribute.`]);
});
it("should error when no matching attribute", () => {
expect(humanizeErrors(parse("<div i18n-value></div>", {}).errors))
.toEqual([`Missing attribute 'value'.`]);
});
it("should error when the translation refers to an invalid expression", () => {
let translations: {[key: string]: string} = {};
translations[id(new Message('hi <ph name="0"/>', null, null))] = 'hi <ph name="99"/>';