| 
									
										
										
										
											2019-07-30 18:02:17 +01: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
 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  | // Ensure that `$localize` is loaded to the global scope.
 | 
					
						
							| 
									
										
										
										
											2019-08-10 12:51:30 +01:00
										 |  |  | import '@angular/localize/init'; | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-08 14:46:28 +01:00
										 |  |  | import {clearTranslations, loadTranslations} from '../localize'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | import {computeMsgId, MessageId, TargetMessage} from '../src/utils'; | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('$localize tag with translations', () => { | 
					
						
							|  |  |  |   describe('identities', () => { | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  |       loadTranslations(computeIds({ | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |         'abc': 'abc', | 
					
						
							| 
									
										
										
										
											2019-09-05 15:11:31 +02:00
										 |  |  |         'abc{$PH}': 'abc{$PH}', | 
					
						
							|  |  |  |         'abc{$PH}def': 'abc{$PH}def', | 
					
						
							|  |  |  |         'abc{$PH}def{$PH_1}': 'abc{$PH}def{$PH_1}', | 
					
						
							|  |  |  |         'Hello, {$PH}!': 'Hello, {$PH}!', | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     afterEach(() => { | 
					
						
							|  |  |  |       clearTranslations(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should render template literals as-is', () => { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       expect($localize`abc`).toEqual('abc'); | 
					
						
							|  |  |  |       expect($localize`abc${1 + 2 + 3}`).toEqual('abc6'); | 
					
						
							|  |  |  |       expect($localize`abc${1 + 2 + 3}def`).toEqual('abc6def'); | 
					
						
							|  |  |  |       expect($localize`abc${1 + 2 + 3}def${4 + 5 + 6}`).toEqual('abc6def15'); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |       const getName = () => 'World'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       expect($localize`Hello, ${getName()}!`).toEqual('Hello, World!'); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('to upper-case messageParts', () => { | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  |       loadTranslations(computeIds({ | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |         'abc': 'ABC', | 
					
						
							| 
									
										
										
										
											2019-09-05 15:11:31 +02:00
										 |  |  |         'abc{$PH}': 'ABC{$PH}', | 
					
						
							|  |  |  |         'abc{$PH}def': 'ABC{$PH}DEF', | 
					
						
							|  |  |  |         'abc{$PH}def{$PH_1}': 'ABC{$PH}DEF{$PH_1}', | 
					
						
							|  |  |  |         'Hello, {$PH}!': 'HELLO, {$PH}!', | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     afterEach(() => { | 
					
						
							|  |  |  |       clearTranslations(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should render template literals with messages upper-cased', () => { | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       expect($localize`abc`).toEqual('ABC'); | 
					
						
							|  |  |  |       expect($localize`abc${1 + 2 + 3}`).toEqual('ABC6'); | 
					
						
							|  |  |  |       expect($localize`abc${1 + 2 + 3}def`).toEqual('ABC6DEF'); | 
					
						
							|  |  |  |       expect($localize`abc${1 + 2 + 3}def${4 + 5 + 6}`).toEqual('ABC6DEF15'); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |       const getName = () => 'World'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       expect($localize`Hello, ${getName()}!`).toEqual('HELLO, World!'); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('to reverse expressions', () => { | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  |       loadTranslations(computeIds({ | 
					
						
							| 
									
										
										
										
											2019-09-05 15:11:31 +02:00
										 |  |  |         'abc{$PH}def{$PH_1} - Hello, {$PH_2}!': 'abc{$PH_2}def{$PH_1} - Hello, {$PH}!', | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     afterEach(() => { | 
					
						
							|  |  |  |       clearTranslations(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should render template literals with expressions reversed', () => { | 
					
						
							|  |  |  |       const getName = () => 'World'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       expect($localize`abc${1 + 2 + 3}def${4 + 5 + 6} - Hello, ${getName()}!`) | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |           .toEqual('abcWorlddef15 - Hello, 6!'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('to remove expressions', () => { | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  |       loadTranslations(computeIds({ | 
					
						
							| 
									
										
										
										
											2019-09-05 15:11:31 +02:00
										 |  |  |         'abc{$PH}def{$PH_1} - Hello, {$PH_2}!': 'abc{$PH} - Hello, {$PH_2}!', | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  |       })); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |     afterEach(() => { | 
					
						
							|  |  |  |       clearTranslations(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should render template literals with expressions removed', () => { | 
					
						
							|  |  |  |       const getName = () => 'World'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |       expect($localize`abc${1 + 2 + 3}def${4 + 5 + 6} - Hello, ${getName()}!`) | 
					
						
							| 
									
										
										
										
											2019-07-30 18:02:17 +01:00
										 |  |  |           .toEqual('abc6 - Hello, World!'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2019-09-13 12:46:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function computeIds(translations: Record<MessageId, TargetMessage>): | 
					
						
							|  |  |  |     Record<MessageId, TargetMessage> { | 
					
						
							|  |  |  |   const processed: Record<MessageId, TargetMessage> = {}; | 
					
						
							|  |  |  |   Object.keys(translations).forEach(key => processed[computeMsgId(key, '')] = translations[key]); | 
					
						
							|  |  |  |   return processed; | 
					
						
							|  |  |  | } |