/** * @license * Copyright Google LLC 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 {MessageBundle} from '@angular/compiler/src/i18n/message_bundle'; import {Xmb} from '@angular/compiler/src/i18n/serializers/xmb'; import {HtmlParser} from '@angular/compiler/src/ml_parser/html_parser'; import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/ml_parser/interpolation_config'; { describe('XMB serializer', () => { const HTML = `

not translatable

translatable element with placeholders {{ interpolation}}

{ count, plural, =0 {

test

}}

foo

foo

foo

{ count, plural, =0 { { sex, select, other {

deeply nested

}} }}

Test: { count, plural, =0 { { sex, select, other {

deeply nested

}} } =other {a lot}}

multi lines

`; const XMB = ` ]> file.ts:3translatable element <b><b>with placeholders</b></b> {{ interpolation}}{{ interpolation}} file.ts:4{VAR_PLURAL, plural, =0 {<p><p>test</p></p>} } file.ts:5foo file.ts:6foo file.ts:7foo file.ts:8{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {<p><p>deeply nested</p></p>} } } } file.ts:9Test: { count, plural, =0 {...} =other {...}}{ count, plural, =0 {...} =other {...}} file.ts:9{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {<p><p>deeply nested</p></p>} } } =other {a lot} } file.ts:10,11multi lines `; it('should write a valid xmb file', () => { expect(toXmb(HTML, 'file.ts')).toEqual(XMB); // the locale is not specified in the xmb file expect(toXmb(HTML, 'file.ts', 'fr')).toEqual(XMB); }); it('should throw when trying to load an xmb file', () => { expect(() => { const serializer = new Xmb(); serializer.load(XMB, 'url'); }).toThrowError(/Unsupported/); }); }); } function toXmb(html: string, url: string, locale: string|null = null): string { const catalog = new MessageBundle(new HtmlParser, [], {}, locale); const serializer = new Xmb(); catalog.updateFromTemplate(html, url, DEFAULT_INTERPOLATION_CONFIG); return catalog.write(serializer); }