/** * @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

}} }}

`; const WRITE_XLIFF = ` translatable attribute translatable element with placeholders {VAR_PLURAL, plural, =0 {test} } d m foo nested Text ph names empty element hello {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {deeply nested} } } } {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {deeply nested} } } } `; const LOAD_XLIFF = ` translatable attribute etubirtta elbatalsnart translatable element with placeholders sredlohecalp htiw tnemele elbatalsnart {VAR_PLURAL, plural, =0 {test} } {VAR_PLURAL, plural, =0 {TEST} } d m foo oof nested Text txeT ph names empty element hello olleh {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {deeply nested} } } } {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {profondément imbriqué} } } } {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {deeply nested} } } } {VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {profondément imbriqué} } } } `; 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, '', 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é, ]}}, ]}}' }); }); 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`))); }); }); }); }