feat(i18n): xmb serializer
This commit is contained in:
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="<">"/>');
|
||||
});
|
||||
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user