2016-08-12 23:14:52 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2016-11-02 20:40:15 -04:00
|
|
|
import {escapeRegExp} from '@angular/core/src/facade/lang';
|
|
|
|
|
|
|
|
import {serializeNodes} from '../../../src/i18n/digest';
|
2016-08-12 23:14:52 -04:00
|
|
|
import {MessageBundle} from '../../../src/i18n/message_bundle';
|
2016-11-02 20:40:15 -04:00
|
|
|
import {Xliff} from '../../../src/i18n/serializers/xliff';
|
2016-08-12 23:14:52 -04:00
|
|
|
import {HtmlParser} from '../../../src/ml_parser/html_parser';
|
|
|
|
import {DEFAULT_INTERPOLATION_CONFIG} from '../../../src/ml_parser/interpolation_config';
|
|
|
|
|
|
|
|
const HTML = `
|
|
|
|
<p i18n-title title="translatable attribute">not translatable</p>
|
|
|
|
<p i18n>translatable element <b>with placeholders</b> {{ interpolation}}</p>
|
|
|
|
<p i18n="m|d">foo</p>
|
2016-12-06 09:04:59 -05:00
|
|
|
<p i18n="m|d@@i">foo</p>
|
|
|
|
<p i18n="@@bar">foo</p>
|
2016-09-30 18:39:46 -04:00
|
|
|
<p i18n="ph names"><br><img><div></div></p>
|
2016-08-12 23:14:52 -04:00
|
|
|
`;
|
|
|
|
|
|
|
|
const WRITE_XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
|
|
|
|
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
|
2016-09-30 17:52:12 -04:00
|
|
|
<file source-language="en" datatype="plaintext" original="ng2.template">
|
|
|
|
<body>
|
|
|
|
<trans-unit id="983775b9a51ce14b036be72d4cfd65d68d64e231" datatype="html">
|
|
|
|
<source>translatable attribute</source>
|
|
|
|
<target/>
|
|
|
|
</trans-unit>
|
|
|
|
<trans-unit id="ec1d033f2436133c14ab038286c4f5df4697484a" datatype="html">
|
2016-09-30 18:39:46 -04:00
|
|
|
<source>translatable element <x id="START_BOLD_TEXT" ctype="x-b"/>with placeholders<x id="CLOSE_BOLD_TEXT" ctype="x-b"/> <x id="INTERPOLATION"/></source>
|
2016-09-30 17:52:12 -04:00
|
|
|
<target/>
|
|
|
|
</trans-unit>
|
|
|
|
<trans-unit id="db3e0a6a5a96481f60aec61d98c3eecddef5ac23" datatype="html">
|
|
|
|
<source>foo</source>
|
|
|
|
<target/>
|
|
|
|
<note priority="1" from="description">d</note>
|
|
|
|
<note priority="1" from="meaning">m</note>
|
|
|
|
</trans-unit>
|
2016-12-06 09:04:59 -05:00
|
|
|
<trans-unit id="i" datatype="html">
|
|
|
|
<source>foo</source>
|
|
|
|
<target/>
|
|
|
|
<note priority="1" from="description">d</note>
|
|
|
|
<note priority="1" from="meaning">m</note>
|
|
|
|
</trans-unit>
|
|
|
|
<trans-unit id="bar" datatype="html">
|
|
|
|
<source>foo</source>
|
|
|
|
<target/>
|
|
|
|
</trans-unit>
|
2016-09-30 18:39:46 -04:00
|
|
|
<trans-unit id="d7fa2d59aaedcaa5309f13028c59af8c85b8c49d" datatype="html">
|
|
|
|
<source><x id="LINE_BREAK" ctype="lb"/><x id="TAG_IMG" ctype="image"/><x id="START_TAG_DIV" ctype="x-div"/><x id="CLOSE_TAG_DIV" ctype="x-div"/></source>
|
|
|
|
<target/>
|
|
|
|
<note priority="1" from="description">ph names</note>
|
2016-09-30 18:50:59 -04:00
|
|
|
</trans-unit>
|
2016-09-30 17:52:12 -04:00
|
|
|
</body>
|
|
|
|
</file>
|
|
|
|
</xliff>
|
|
|
|
`;
|
2016-08-12 23:14:52 -04:00
|
|
|
|
|
|
|
const LOAD_XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
|
|
|
|
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
|
2016-09-30 17:52:12 -04:00
|
|
|
<file source-language="en" datatype="plaintext" original="ng2.template">
|
|
|
|
<body>
|
|
|
|
<trans-unit id="983775b9a51ce14b036be72d4cfd65d68d64e231" datatype="html">
|
|
|
|
<source>translatable attribute</source>
|
|
|
|
<target>etubirtta elbatalsnart</target>
|
|
|
|
</trans-unit>
|
|
|
|
<trans-unit id="ec1d033f2436133c14ab038286c4f5df4697484a" datatype="html">
|
|
|
|
<source>translatable element <x id="START_BOLD_TEXT" ctype="b"/>with placeholders<x id="CLOSE_BOLD_TEXT" ctype="b"/> <x id="INTERPOLATION"/></source>
|
2016-09-30 18:39:46 -04:00
|
|
|
<target><x id="INTERPOLATION"/> footnemele elbatalsnart <x id="START_BOLD_TEXT" ctype="x-b"/>sredlohecalp htiw<x id="CLOSE_BOLD_TEXT" ctype="x-b"/></target>
|
2016-09-30 17:52:12 -04:00
|
|
|
</trans-unit>
|
|
|
|
<trans-unit id="db3e0a6a5a96481f60aec61d98c3eecddef5ac23" datatype="html">
|
|
|
|
<source>foo</source>
|
|
|
|
<target>oof</target>
|
|
|
|
<note priority="1" from="description">d</note>
|
|
|
|
<note priority="1" from="meaning">m</note>
|
|
|
|
</trans-unit>
|
2016-12-06 09:04:59 -05:00
|
|
|
<trans-unit id="i" datatype="html">
|
|
|
|
<source>foo</source>
|
|
|
|
<target>toto</target>
|
|
|
|
<note priority="1" from="description">d</note>
|
|
|
|
<note priority="1" from="meaning">m</note>
|
|
|
|
</trans-unit>
|
|
|
|
<trans-unit id="bar" datatype="html">
|
|
|
|
<source>foo</source>
|
|
|
|
<target>tata</target>
|
|
|
|
</trans-unit>
|
2016-09-30 18:39:46 -04:00
|
|
|
<trans-unit id="d7fa2d59aaedcaa5309f13028c59af8c85b8c49d" datatype="html">
|
|
|
|
<source><x id="LINE_BREAK" ctype="lb"/><x id="TAG_IMG" ctype="image"/><x id="START_TAG_DIV" ctype="x-div"/><x id="CLOSE_TAG_DIV" ctype="x-div"/></source>
|
|
|
|
<target><x id="START_TAG_DIV" ctype="x-div"/><x id="CLOSE_TAG_DIV" ctype="x-div"/><x id="TAG_IMG" ctype="image"/><x id="LINE_BREAK" ctype="lb"/></target>
|
|
|
|
<note priority="1" from="description">ph names</note>
|
|
|
|
</trans-unit>
|
2016-09-30 17:52:12 -04:00
|
|
|
</body>
|
|
|
|
</file>
|
|
|
|
</xliff>
|
|
|
|
`;
|
2016-08-12 23:14:52 -04:00
|
|
|
|
|
|
|
export function main(): void {
|
2016-11-02 20:40:15 -04:00
|
|
|
const serializer = new Xliff();
|
2016-08-12 23:14:52 -04:00
|
|
|
|
|
|
|
function toXliff(html: string): string {
|
2016-11-12 08:08:58 -05:00
|
|
|
const catalog = new MessageBundle(new HtmlParser, [], {});
|
2016-08-12 23:14:52 -04:00
|
|
|
catalog.updateFromTemplate(html, '', DEFAULT_INTERPOLATION_CONFIG);
|
|
|
|
return catalog.write(serializer);
|
|
|
|
}
|
|
|
|
|
2016-11-02 20:40:15 -04:00
|
|
|
function loadAsMap(xliff: string): {[id: string]: string} {
|
|
|
|
const i18nNodesByMsgId = serializer.load(xliff, 'url');
|
|
|
|
const msgMap: {[id: string]: string} = {};
|
|
|
|
Object.keys(i18nNodesByMsgId)
|
|
|
|
.forEach(id => msgMap[id] = serializeNodes(i18nNodesByMsgId[id]).join(''));
|
2016-08-12 23:14:52 -04:00
|
|
|
|
2016-11-02 20:40:15 -04:00
|
|
|
return msgMap;
|
2016-08-12 23:14:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
describe('XLIFF serializer', () => {
|
|
|
|
describe('write', () => {
|
|
|
|
it('should write a valid xliff file', () => { expect(toXliff(HTML)).toEqual(WRITE_XLIFF); });
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('load', () => {
|
|
|
|
it('should load XLIFF files', () => {
|
2016-11-02 20:40:15 -04:00
|
|
|
expect(loadAsMap(LOAD_XLIFF)).toEqual({
|
2016-08-12 23:14:52 -04:00
|
|
|
'983775b9a51ce14b036be72d4cfd65d68d64e231': 'etubirtta elbatalsnart',
|
|
|
|
'ec1d033f2436133c14ab038286c4f5df4697484a':
|
2016-11-02 20:40:15 -04:00
|
|
|
'<ph name="INTERPOLATION"/> footnemele elbatalsnart <ph name="START_BOLD_TEXT"/>sredlohecalp htiw<ph name="CLOSE_BOLD_TEXT"/>',
|
2016-08-12 23:14:52 -04:00
|
|
|
'db3e0a6a5a96481f60aec61d98c3eecddef5ac23': 'oof',
|
2016-12-06 09:04:59 -05:00
|
|
|
'i': 'toto',
|
|
|
|
'bar': 'tata',
|
2016-11-02 20:40:15 -04:00
|
|
|
'd7fa2d59aaedcaa5309f13028c59af8c85b8c49d':
|
|
|
|
'<ph name="START_TAG_DIV"/><ph name="CLOSE_TAG_DIV"/><ph name="TAG_IMG"/><ph name="LINE_BREAK"/>',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-11-14 13:41:46 -05:00
|
|
|
describe('structure errors', () => {
|
2016-11-14 14:22:58 -05:00
|
|
|
it('should throw when a trans-unit has no translation', () => {
|
|
|
|
const XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
|
|
|
|
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
|
|
|
|
<file source-language="en" datatype="plaintext" original="ng2.template">
|
|
|
|
<body>
|
|
|
|
<trans-unit id="missingtarget">
|
|
|
|
<source/>
|
|
|
|
</trans-unit>
|
|
|
|
</body>
|
|
|
|
</file>
|
|
|
|
</xliff>`;
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
loadAsMap(XLIFF);
|
|
|
|
}).toThrowError(/Message missingtarget misses a translation/);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-11-14 13:41:46 -05:00
|
|
|
it('should throw when a trans-unit has no id attribute', () => {
|
2016-11-02 20:40:15 -04:00
|
|
|
const XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
|
|
|
|
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
|
|
|
|
<file source-language="en" datatype="plaintext" original="ng2.template">
|
|
|
|
<body>
|
|
|
|
<trans-unit datatype="html">
|
|
|
|
<source/>
|
|
|
|
<target/>
|
|
|
|
</trans-unit>
|
|
|
|
</body>
|
|
|
|
</file>
|
|
|
|
</xliff>`;
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
loadAsMap(XLIFF);
|
|
|
|
}).toThrowError(/<trans-unit> misses the "id" attribute/);
|
|
|
|
});
|
|
|
|
|
2016-11-14 13:41:46 -05:00
|
|
|
it('should throw on duplicate trans-unit id', () => {
|
|
|
|
const XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
|
|
|
|
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
|
|
|
|
<file source-language="en" datatype="plaintext" original="ng2.template">
|
|
|
|
<body>
|
|
|
|
<trans-unit id="deadbeef">
|
|
|
|
<source/>
|
|
|
|
<target/>
|
|
|
|
</trans-unit>
|
|
|
|
<trans-unit id="deadbeef">
|
|
|
|
<source/>
|
|
|
|
<target/>
|
|
|
|
</trans-unit>
|
|
|
|
</body>
|
|
|
|
</file>
|
|
|
|
</xliff>`;
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
loadAsMap(XLIFF);
|
|
|
|
}).toThrowError(/Duplicated translations for msg deadbeef/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('message errors', () => {
|
2016-11-02 20:40:15 -04:00
|
|
|
it('should throw on unknown message tags', () => {
|
|
|
|
const XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
|
|
|
|
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
|
|
|
|
<file source-language="en" datatype="plaintext" original="ng2.template">
|
|
|
|
<body>
|
|
|
|
<trans-unit id="deadbeef" datatype="html">
|
|
|
|
<source/>
|
|
|
|
<target><b>msg should contain only ph tags</b></target>
|
|
|
|
</trans-unit>
|
|
|
|
</body>
|
|
|
|
</file>
|
|
|
|
</xliff>`;
|
|
|
|
|
|
|
|
expect(() => { loadAsMap(XLIFF); })
|
|
|
|
.toThrowError(
|
|
|
|
new RegExp(escapeRegExp(`[ERROR ->]<b>msg should contain only ph tags</b>`)));
|
|
|
|
});
|
|
|
|
|
2016-11-14 13:41:46 -05:00
|
|
|
it('should throw when a placeholder misses an id attribute', () => {
|
2016-11-02 20:40:15 -04:00
|
|
|
const XLIFF = `<?xml version="1.0" encoding="UTF-8" ?>
|
|
|
|
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
|
|
|
|
<file source-language="en" datatype="plaintext" original="ng2.template">
|
|
|
|
<body>
|
2016-11-14 13:41:46 -05:00
|
|
|
<trans-unit id="deadbeef" datatype="html">
|
2016-11-02 20:40:15 -04:00
|
|
|
<source/>
|
2016-11-14 13:41:46 -05:00
|
|
|
<target><x/></target>
|
2016-11-02 20:40:15 -04:00
|
|
|
</trans-unit>
|
|
|
|
</body>
|
|
|
|
</file>
|
|
|
|
</xliff>`;
|
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
loadAsMap(XLIFF);
|
2016-11-14 13:41:46 -05:00
|
|
|
}).toThrowError(new RegExp(escapeRegExp(`<x> misses the "id" attribute`)));
|
2016-08-12 23:14:52 -04:00
|
|
|
});
|
2016-11-14 13:41:46 -05:00
|
|
|
|
2016-08-12 23:14:52 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|