| 
									
										
										
										
											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-08-05 12:08:43 -07:00
										 |  |  | import {serializeNodes} from '../../src/i18n/digest'; | 
					
						
							| 
									
										
										
										
											2016-11-02 17:40:15 -07:00
										 |  |  | import * as i18n from '../../src/i18n/i18n_ast'; | 
					
						
							| 
									
										
										
										
											2016-08-05 12:08:43 -07:00
										 |  |  | import {MessageBundle} from '../../src/i18n/message_bundle'; | 
					
						
							| 
									
										
										
										
											2016-11-02 17:40:15 -07:00
										 |  |  | import {Serializer} from '../../src/i18n/serializers/serializer'; | 
					
						
							| 
									
										
										
										
											2016-08-01 12:19:09 -07:00
										 |  |  | import {HtmlParser} from '../../src/ml_parser/html_parser'; | 
					
						
							|  |  |  | import {DEFAULT_INTERPOLATION_CONFIG} from '../../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('MessageBundle', () => { | 
					
						
							|  |  |  |     describe('Messages', () => { | 
					
						
							|  |  |  |       let messages: MessageBundle; | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |       beforeEach(() => { messages = new MessageBundle(new HtmlParser, [], {}); }); | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should extract the message to the catalog', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         messages.updateFromTemplate( | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |             '<p i18n="m|d">Translate Me</p>', 'url', DEFAULT_INTERPOLATION_CONFIG); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(humanizeMessages(messages)).toEqual([ | 
					
						
							| 
									
										
										
										
											2016-10-28 19:53:42 -07:00
										 |  |  |           'Translate Me (m|d)', | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-19 14:42:25 -08:00
										 |  |  |       it('should extract and dedup messages', () => { | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         messages.updateFromTemplate( | 
					
						
							| 
									
										
										
										
											2017-01-19 14:42:25 -08:00
										 |  |  |             '<p i18n="m|d@@1">Translate Me</p><p i18n="@@2">Translate Me</p><p i18n="@@2">Translate Me</p>', | 
					
						
							|  |  |  |             'url', DEFAULT_INTERPOLATION_CONFIG); | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  |         expect(humanizeMessages(messages)).toEqual([ | 
					
						
							| 
									
										
										
										
											2016-10-28 19:53:42 -07:00
										 |  |  |           'Translate Me (m|d)', | 
					
						
							|  |  |  |           'Translate Me (|)', | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |         ]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-17 17:36:16 -08:00
										 |  |  | class _TestSerializer extends Serializer { | 
					
						
							| 
									
										
										
										
											2016-10-28 19:53:42 -07:00
										 |  |  |   write(messages: i18n.Message[]): string { | 
					
						
							|  |  |  |     return messages.map(msg => `${serializeNodes(msg.nodes)} (${msg.meaning}|${msg.description})`) | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |         .join('//'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-03 14:29:28 -08:00
										 |  |  |   load(content: string, url: string): | 
					
						
							|  |  |  |       {locale: string | null, i18nNodesByMsgId: {[id: string]: i18n.Node[]}} { | 
					
						
							|  |  |  |     return {locale: null, i18nNodesByMsgId: {}}; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2016-10-28 19:53:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-19 14:42:25 -08:00
										 |  |  |   digest(msg: i18n.Message): string { return msg.id || `default`; } | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-21 13:56:58 -07:00
										 |  |  | function humanizeMessages(catalog: MessageBundle): string[] { | 
					
						
							| 
									
										
										
										
											2016-07-15 09:42:33 -07:00
										 |  |  |   return catalog.write(new _TestSerializer()).split('//'); | 
					
						
							| 
									
										
										
										
											2017-02-16 17:03:18 +01:00
										 |  |  | } |