| 
									
										
										
										
											2016-07-15 09:42:33 -07: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-09-30 14:52:12 -07:00
										 |  |  | import {MessageBundle} from '@angular/compiler/src/i18n/message_bundle'; | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  | import {Xmb} from '@angular/compiler/src/i18n/serializers/xmb'; | 
					
						
							| 
									
										
										
										
											2016-09-30 14:52:12 -07:00
										 |  |  | import {HtmlParser} from '@angular/compiler/src/ml_parser/html_parser'; | 
					
						
							|  |  |  | import {DEFAULT_INTERPOLATION_CONFIG} from '@angular/compiler/src/ml_parser/interpolation_config'; | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 14:42:55 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |   describe('XMB serializer', () => { | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |     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> | 
					
						
							| 
									
										
										
										
											2016-12-06 15:04:59 +01:00
										 |  |  | <p i18n="m|d@@i">foo</p> | 
					
						
							|  |  |  | <p i18n="@@bar">foo</p> | 
					
						
							|  |  |  | <p i18n="@@baz">{ count, plural, =0 { { sex, select, other {<p>deeply nested</p>}} }}</p> | 
					
						
							| 
									
										
										
										
											2017-07-12 16:27:53 +02:00
										 |  |  | <p i18n>Test: { count, plural, =0 { { sex, select, other {<p>deeply nested</p>}} } =other {a lot}}</p> | 
					
						
							| 
									
										
										
										
											2017-04-14 18:06:25 +02:00
										 |  |  | <p i18n>multi | 
					
						
							|  |  |  | lines</p>`;
 | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 21:26:03 -07:00
										 |  |  |     const XMB = `<?xml version="1.0" encoding="UTF-8" ?>
 | 
					
						
							| 
									
										
										
										
											2016-07-29 13:07:01 -07:00
										 |  |  | <!DOCTYPE messagebundle [ | 
					
						
							|  |  |  | <!ELEMENT messagebundle (msg)*> | 
					
						
							|  |  |  | <!ATTLIST messagebundle class CDATA #IMPLIED> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!ELEMENT msg (#PCDATA|ph|source)*> | 
					
						
							|  |  |  | <!ATTLIST msg id CDATA #IMPLIED> | 
					
						
							|  |  |  | <!ATTLIST msg seq CDATA #IMPLIED> | 
					
						
							|  |  |  | <!ATTLIST msg name CDATA #IMPLIED> | 
					
						
							|  |  |  | <!ATTLIST msg desc CDATA #IMPLIED> | 
					
						
							|  |  |  | <!ATTLIST msg meaning CDATA #IMPLIED> | 
					
						
							|  |  |  | <!ATTLIST msg obsolete (obsolete) #IMPLIED> | 
					
						
							|  |  |  | <!ATTLIST msg xml:space (default|preserve) "default"> | 
					
						
							|  |  |  | <!ATTLIST msg is_hidden CDATA #IMPLIED> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!ELEMENT source (#PCDATA)> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!ELEMENT ph (#PCDATA|ex)*> | 
					
						
							|  |  |  | <!ATTLIST ph name CDATA #REQUIRED> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <!ELEMENT ex (#PCDATA)> | 
					
						
							|  |  |  | ]> | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | <messagebundle> | 
					
						
							| 
									
										
										
										
											2017-07-12 16:27:53 +02:00
										 |  |  |   <msg id="7056919470098446707"><source>file.ts:3</source>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"><ex>{{ interpolation}}</ex></ph></msg> | 
					
						
							| 
									
										
										
										
											2017-04-14 18:06:25 +02:00
										 |  |  |   <msg id="2981514368455622387"><source>file.ts:4</source>{VAR_PLURAL, plural, =0 {<ph name="START_PARAGRAPH"><ex><p></ex></ph>test<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>} }</msg> | 
					
						
							|  |  |  |   <msg id="7999024498831672133" desc="d" meaning="m"><source>file.ts:5</source>foo</msg> | 
					
						
							|  |  |  |   <msg id="i" desc="d" meaning="m"><source>file.ts:6</source>foo</msg> | 
					
						
							|  |  |  |   <msg id="bar"><source>file.ts:7</source>foo</msg> | 
					
						
							|  |  |  |   <msg id="baz"><source>file.ts:8</source>{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {<ph name="START_PARAGRAPH"><ex><p></ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>} } } }</msg> | 
					
						
							| 
									
										
										
										
											2017-07-12 16:27:53 +02:00
										 |  |  |   <msg id="6997386649824869937"><source>file.ts:9</source>Test: <ph name="ICU"><ex>{ count, plural, =0 {...} =other {...}}</ex></ph></msg> | 
					
						
							|  |  |  |   <msg id="5229984852258993423"><source>file.ts:9</source>{VAR_PLURAL, plural, =0 {{VAR_SELECT, select, other {<ph name="START_PARAGRAPH"><ex><p></ex></ph>deeply nested<ph name="CLOSE_PARAGRAPH"><ex></p></ex></ph>} } } =other {a lot} }</msg> | 
					
						
							| 
									
										
										
										
											2017-04-14 18:06:25 +02:00
										 |  |  |   <msg id="2340165783990709777"><source>file.ts:10,11</source>multi | 
					
						
							|  |  |  | lines</msg> | 
					
						
							| 
									
										
										
										
											2016-09-30 14:52:12 -07:00
										 |  |  | </messagebundle> | 
					
						
							|  |  |  | `;
 | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-16 17:03:18 +01:00
										 |  |  |     it('should write a valid xmb file', () => { | 
					
						
							| 
									
										
										
										
											2017-04-14 18:06:25 +02:00
										 |  |  |       expect(toXmb(HTML, 'file.ts')).toEqual(XMB); | 
					
						
							| 
									
										
										
										
											2017-02-16 17:03:18 +01:00
										 |  |  |       // the locale is not specified in the xmb file
 | 
					
						
							| 
									
										
										
										
											2017-04-14 18:06:25 +02:00
										 |  |  |       expect(toXmb(HTML, 'file.ts', 'fr')).toEqual(XMB); | 
					
						
							| 
									
										
										
										
											2017-02-16 17:03:18 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should throw when trying to load an xmb file', () => { | 
					
						
							|  |  |  |       expect(() => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         const serializer = new Xmb(); | 
					
						
							| 
									
										
										
										
											2016-11-02 17:40:15 -07:00
										 |  |  |         serializer.load(XMB, 'url'); | 
					
						
							| 
									
										
										
										
											2016-08-09 21:05:04 -07:00
										 |  |  |       }).toThrowError(/Unsupported/); | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-14 18:06:25 +02:00
										 |  |  | function toXmb(html: string, url: string, locale: string | null = null): string { | 
					
						
							| 
									
										
										
										
											2017-02-16 17:03:18 +01:00
										 |  |  |   const catalog = new MessageBundle(new HtmlParser, [], {}, locale); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |   const serializer = new Xmb(); | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-14 18:06:25 +02:00
										 |  |  |   catalog.updateFromTemplate(html, url, DEFAULT_INTERPOLATION_CONFIG); | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return catalog.write(serializer); | 
					
						
							| 
									
										
										
										
											2016-08-04 19:35:41 +02:00
										 |  |  | } |