refactor(i18n): misc updates

This commit is contained in:
Victor Berchet
2016-08-01 14:43:20 -07:00
parent df44e3e425
commit e811a5d97f
10 changed files with 115 additions and 59 deletions

View File

@ -10,7 +10,7 @@ import {ExtractionResult, extractAstMessages} from '@angular/compiler/src/i18n/e
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
import {HtmlParser} from '../../src/ml_parser/html_parser';
import {serializeAst} from '../html_parser/ast_serializer_spec';
import {serializeNodes} from '../html_parser/ast_serializer_spec';
export function main() {
describe('MessageExtractor', () => {
@ -21,6 +21,24 @@ export function main() {
]);
});
it('should extract from elements', () => {
expect(
extract(
'<div i18n="m|d"><span i18n-title="m|d" title="single child">nested</span></div>'))
.toEqual([
[
['<span i18n-title="m|d" title="single child">nested</span>'],
'm',
'd',
],
[
['title="single child"'],
'm',
'd',
],
]);
});
it('should extract from elements', () => {
expect(
extract(
@ -87,6 +105,11 @@ export function main() {
[['{count, plural, =0 {text}}'], 'm', 'd'],
]);
// single message when ICU is the only (implicit) children
expect(extract('<div>{count, plural, =0 {text}}</div>', ['div'])).toEqual([
[['{count, plural, =0 {text}}'], '', ''],
]);
// one message for the element content and one message for the ICU
expect(extract('<div i18n="m|d">before{count, plural, =0 {text}}after</div>')).toEqual([
[['before', '{count, plural, =0 {text}}', 'after'], 'm', 'd'],
@ -184,18 +207,24 @@ export function main() {
it('should report nested translatable elements', () => {
expect(extractErrors(`<p i18n><b i18n></b></p>`)).toEqual([
['Could not mark an element as translatable inside a translatable section', '<b i18n>'],
['Unexpected section start', '<b i18n>'],
['Unexpected section end', '<p i18n>'],
]);
});
it('should report translatable elements in implicit elements', () => {
expect(extractErrors(`<p><b i18n></b></p>`, ['p'])).toEqual([
['Could not mark an element as translatable inside a translatable section', '<b i18n>'],
['Unexpected section start', '<b i18n>'],
['Unexpected section end', '<p>'],
]);
});
it('should report translatable elements in translatable blocks', () => {
expect(extractErrors(`<!-- i18n --><b i18n></b><!-- /i18n -->`)).toEqual([
['Could not mark an element as translatable inside a translatable section', '<b i18n>'],
['Unexpected section start', '<b i18n>'],
['Unexpected section end', '<!--'],
]);
});
});
@ -246,18 +275,24 @@ export function main() {
it('should report nested implicit elements', () => {
expect(extractErrors(`<p><b></b></p>`, ['p', 'b'])).toEqual([
['Could not mark an element as translatable inside a translatable section', '<b>'],
['Unexpected section start', '<b>'],
['Unexpected section end', '<p>'],
]);
});
it('should report implicit element in translatable element', () => {
expect(extractErrors(`<p i18n><b></b></p>`, ['b'])).toEqual([
['Could not mark an element as translatable inside a translatable section', '<b>'],
['Unexpected section start', '<b>'],
['Unexpected section end', '<p i18n>'],
]);
});
it('should report implicit element in translatable blocks', () => {
expect(extractErrors(`<!-- i18n --><b></b><!-- /i18n -->`, ['b'])).toEqual([
['Could not mark an element as translatable inside a translatable section', '<b>'],
['Unexpected section start', '<b>'],
['Unexpected section end', '<!--'],
]);
});
});
@ -285,7 +320,7 @@ function extract(
// clang-format off
// https://github.com/angular/clang-format/issues/35
return messages.map(
message => [serializeAst(message.nodes), message.meaning, message.description, ]) as [string[], string, string][];
message => [serializeNodes(message.nodes), message.meaning, message.description, ]) as [string[], string, string][];
// clang-format on
}

View File

@ -8,9 +8,9 @@
import {Message} from '@angular/compiler/src/i18n/i18n_ast';
import {extractI18nMessages} from '@angular/compiler/src/i18n/i18n_parser';
import {ddescribe, describe, expect, it} from '@angular/core/testing/testing_internal';
import {ddescribe, describe, expect, iit, it} from '@angular/core/testing/testing_internal';
import {serializeAst} from '../../src/i18n/message_bundle';
import {serializeNodes} from '../../src/i18n/message_bundle';
import {HtmlParser} from '../../src/ml_parser/html_parser';
import {DEFAULT_INTERPOLATION_CONFIG} from '../../src/ml_parser/interpolation_config';
@ -289,7 +289,7 @@ function _humanizeMessages(
// clang-format off
// https://github.com/angular/clang-format/issues/35
return _extractMessages(html, implicitTags, implicitAttrs).map(
message => [serializeAst(message.nodes), message.meaning, message.description, ]) as [string[], string, string][];
message => [serializeNodes(message.nodes), message.meaning, message.description, ]) as [string[], string, string][];
// clang-format on
}

View File

@ -10,7 +10,7 @@ import * as i18n from '@angular/compiler/src/i18n/i18n_ast';
import {Serializer} from '@angular/compiler/src/i18n/serializers/serializer';
import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit} from '@angular/core/testing/testing_internal';
import {MessageBundle, serializeAst, strHash} from '../../src/i18n/message_bundle';
import {MessageBundle, serializeNodes, strHash} from '../../src/i18n/message_bundle';
import {HtmlParser} from '../../src/ml_parser/html_parser';
import {DEFAULT_INTERPOLATION_CONFIG} from '../../src/ml_parser/interpolation_config';
@ -59,7 +59,7 @@ export function main(): void {
class _TestSerializer implements Serializer {
write(messageMap: {[id: string]: i18n.Message}): string {
return Object.keys(messageMap)
.map(id => `${id}=${serializeAst(messageMap[id].nodes)}`)
.map(id => `${id}=${serializeNodes(messageMap[id].nodes)}`)
.join('//');
}

View File

@ -12,7 +12,7 @@ import {beforeEach, ddescribe, describe, expect, iit, inject, it, xdescribe, xit
import {HtmlParser} from '../../../src/ml_parser/html_parser';
import {DEFAULT_INTERPOLATION_CONFIG} from '../../../src/ml_parser/interpolation_config';
import {serializeAst} from '../../ml_parser/ast_serializer_spec';
import {serializeNodes} from '../../ml_parser/ast_serializer_spec';
export function main(): void {
describe('XTB serializer', () => {
@ -22,7 +22,7 @@ export function main(): void {
{[id: string]: string} {
const asAst = serializer.load(content, 'url', placeholders);
let asText: {[id: string]: string} = {};
Object.keys(asAst).forEach(id => { asText[id] = serializeAst(asAst[id]).join(''); });
Object.keys(asAst).forEach(id => { asText[id] = serializeNodes(asAst[id]).join(''); });
return asText;
}

View File

@ -11,7 +11,7 @@ import * as html from '../../src/ml_parser/ast';
import {HtmlParser} from '../../src/ml_parser/html_parser';
export function main() {
describe('Node serilaizer', () => {
describe('Node serializer', () => {
var parser: HtmlParser;
beforeEach(() => { parser = new HtmlParser(); });
@ -19,31 +19,31 @@ export function main() {
it('should support element', () => {
const html = '<p></p>';
const ast = parser.parse(html, 'url');
expect(serializeAst(ast.rootNodes)).toEqual([html]);
expect(serializeNodes(ast.rootNodes)).toEqual([html]);
});
it('should support attributes', () => {
const html = '<p k="value"></p>';
const ast = parser.parse(html, 'url');
expect(serializeAst(ast.rootNodes)).toEqual([html]);
expect(serializeNodes(ast.rootNodes)).toEqual([html]);
});
it('should support text', () => {
const html = 'some text';
const ast = parser.parse(html, 'url');
expect(serializeAst(ast.rootNodes)).toEqual([html]);
expect(serializeNodes(ast.rootNodes)).toEqual([html]);
});
it('should support expansion', () => {
const html = '{number, plural, =0 {none} =1 {one} other {many}}';
const ast = parser.parse(html, 'url', true);
expect(serializeAst(ast.rootNodes)).toEqual([html]);
expect(serializeNodes(ast.rootNodes)).toEqual([html]);
});
it('should support comment', () => {
const html = '<!--comment-->';
const ast = parser.parse(html, 'url', true);
expect(serializeAst(ast.rootNodes)).toEqual([html]);
expect(serializeNodes(ast.rootNodes)).toEqual([html]);
});
it('should support nesting', () => {
@ -55,7 +55,7 @@ export function main() {
</p>
</div>`;
const ast = parser.parse(html, 'url', true);
expect(serializeAst(ast.rootNodes)).toEqual([html]);
expect(serializeNodes(ast.rootNodes)).toEqual([html]);
});
});
}
@ -91,6 +91,6 @@ class _SerializerVisitor implements html.Visitor {
const serializerVisitor = new _SerializerVisitor();
export function serializeAst(nodes: html.Node[]): string[] {
export function serializeNodes(nodes: html.Node[]): string[] {
return nodes.map(node => node.visit(serializerVisitor, null));
}