/**
* @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 {escapeRegExp} from '@angular/compiler/src/util';
import {serializeNodes} from '../../../src/i18n/digest';
import {MessageBundle} from '../../../src/i18n/message_bundle';
import {Xliff2} from '../../../src/i18n/serializers/xliff2';
import {HtmlParser} from '../../../src/ml_parser/html_parser';
import {DEFAULT_INTERPOLATION_CONFIG} from '../../../src/ml_parser/interpolation_config';
const HTML = `
not translatable
translatable element with placeholders {{ interpolation}}
{ count, plural, =0 {
test
}}
foo
{{interpolation}} Text
hello
{ count, plural, =0 { { sex, select, other {
deeply nested
}} }}
{ count, plural, =0 { { sex, select, other {
deeply nested
}} }}
multi
lines
`;
const WRITE_XLIFF = `
file.ts:2file.ts:3file.ts:4dmfile.ts:5nestedfile.ts:6ph namesfile.ts:7empty elementfile.ts:8file.ts:9file.ts:10file.ts:11,12
`;
const LOAD_XLIFF = `
file.ts:2etubirtta elbatalsnartfile.ts:3sredlohecalp htiw tnemele elbatalsnartfile.ts:4{VAR_PLURAL, plural, =0 {TEST} }dmfile.ts:5oofnestedfile.ts:6txeT ph namesfile.ts:7empty elementfile.ts:8 ollehfile.ts:9{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {profondément imbriqué} } } }file.ts:10{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {profondément imbriqué} } } }file.ts:11,12multi
lignes
`;
export function main(): void {
const serializer = new Xliff2();
function toXliff(html: string, locale: string | null = null): string {
const catalog = new MessageBundle(new HtmlParser, [], {}, locale);
catalog.updateFromTemplate(html, 'file.ts', DEFAULT_INTERPOLATION_CONFIG);
return catalog.write(serializer);
}
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(''));
return msgMap;
}
describe('XLIFF 2.0 serializer', () => {
describe('write', () => {
it('should write a valid xliff 2.0 file',
() => { expect(toXliff(HTML)).toEqual(WRITE_XLIFF); });
it('should write a valid xliff 2.0 file with a source language',
() => { expect(toXliff(HTML, 'fr')).toContain('srcLang="fr"'); });
});
describe('load', () => {
it('should load XLIFF files', () => {
expect(loadAsMap(LOAD_XLIFF)).toEqual({
'1933478729560469763': 'etubirtta elbatalsnart',
'7056919470098446707':
'sredlohecalp htiw tnemele elbatalsnart',
'2981514368455622387':
'{VAR_PLURAL, plural, =0 {[, TEST, ]}}',
'i': 'oof',
'6440235004920703622':
'txeT ',
'8779402634269838862':
'',
'6536355551500405293': ' olleh',
'baz':
'{VAR_PLURAL, plural, =0 {[{VAR_SELECT, select, other {[, profondément imbriqué, ]}}, ]}}',
'2015957479576096115': '{VAR_PLURAL, plural, =0 {[{VAR_SELECT, select, other {[, profondément imbriqué, ]}}, ]}}',
'2340165783990709777': `multi
lignes`
});
});
it('should return the target locale',
() => { expect(serializer.load(LOAD_XLIFF, 'url').locale).toEqual('fr'); });
});
describe('structure errors', () => {
it('should throw when a wrong xliff version is used', () => {
const XLIFF = `
`;
expect(() => {
loadAsMap(XLIFF);
}).toThrowError(/The XLIFF file version 1.2 is not compatible with XLIFF 2.0 serializer/);
});
it('should throw when an unit has no translation', () => {
const XLIFF = `
`;
expect(() => {
loadAsMap(XLIFF);
}).toThrowError(/Message missingtarget misses a translation/);
});
it('should throw when an unit has no id attribute', () => {
const XLIFF = `
`;
expect(() => { loadAsMap(XLIFF); }).toThrowError(/ misses the "id" attribute/);
});
it('should throw on duplicate unit id', () => {
const XLIFF = `
`;
expect(() => {
loadAsMap(XLIFF);
}).toThrowError(/Duplicated translations for msg deadbeef/);
});
});
describe('message errors', () => {
it('should throw on unknown message tags', () => {
const XLIFF = `
msg should contain only ph and pc tags`;
expect(() => { loadAsMap(XLIFF); })
.toThrowError(new RegExp(
escapeRegExp(`[ERROR ->]msg should contain only ph and pc tags`)));
});
it('should throw when a placeholder misses an id attribute', () => {
const XLIFF = `
`;
expect(() => {
loadAsMap(XLIFF);
}).toThrowError(new RegExp(escapeRegExp(` misses the "equiv" attribute`)));
});
});
});
}