@@ -7,24 +7,32 @@ import {TranslateModule, TranslateService} from '../src/public_api';
7
7
selector : 'hmx-app' ,
8
8
changeDetection : ChangeDetectionStrategy . OnPush ,
9
9
template : `
10
- <div #noKey translate>
11
- TEST
12
- </div>
13
- <div #withKey [translate]="'TEST'">Some init content</div>
14
- <div #noContent [translate]="'TEST'"></div>
15
- <div #withOtherElements translate>TEST1 <span>Hey</span> TEST2</div>
16
- <div #withParams [translate]="'TEST'" [translateParams]="value">Some init content</div>
17
- <div #withParamsNoKey translate [translateParams]="value">TEST</div>
10
+ <div #noKey translate>TEST</div>
11
+ <div #contentAsKey translate>TEST.VALUE</div>
12
+ <div #withKey [translate]="'TEST'">Some init content</div>
13
+ <div #noContent [translate]="'TEST'"></div>
14
+ <div #withOtherElements translate>TEST1 <span>Hey</span> TEST2</div>
15
+ <div #withParams [translate]="'TEST'" [translateParams]="value">Some init content</div>
16
+ <div #withParamsNoKey translate [translateParams]="value">TEST</div>
17
+ <div #leadingSpaceNoKeyNoParams translate> TEST</div>
18
+ <div #trailingSpaceNoKeyNoParams translate>TEST </div>
19
+ <div #withSpaceAndLineBreakNoKeyNoParams translate>
20
+ TEST
21
+ </div>
18
22
`
19
23
} )
20
24
class App {
21
25
viewContainerRef : ViewContainerRef ;
22
26
@ViewChild ( 'noKey' , { static : true } ) noKey : ElementRef ;
27
+ @ViewChild ( 'contentAsKey' , { static : true } ) contentAsKey : ElementRef ;
23
28
@ViewChild ( 'withKey' , { static : true } ) withKey : ElementRef ;
24
29
@ViewChild ( 'withOtherElements' , { static : true } ) withOtherElements : ElementRef ;
25
30
@ViewChild ( 'withParams' , { static : true } ) withParams : ElementRef ;
26
31
@ViewChild ( 'withParamsNoKey' , { static : true } ) withParamsNoKey : ElementRef ;
27
32
@ViewChild ( 'noContent' , { static : true } ) noContent : ElementRef ;
33
+ @ViewChild ( 'leadingSpaceNoKeyNoParams' ) leadingSpaceNoKeyNoParams : ElementRef ;
34
+ @ViewChild ( 'trailingSpaceNoKeyNoParams' ) trailingSpaceNoKeyNoParams : ElementRef ;
35
+ @ViewChild ( 'withSpaceAndLineBreakNoKeyNoParams' ) withSpaceAndLineBreakNoKeyNoParams : ElementRef ;
28
36
value = { value : 'ok' } ;
29
37
30
38
constructor ( viewContainerRef : ViewContainerRef ) {
@@ -43,7 +51,7 @@ describe('TranslateDirective', () => {
43
51
] ,
44
52
declarations : [ App ]
45
53
} ) ;
46
- translate = TestBed . get ( TranslateService ) ;
54
+ translate = TestBed . inject ( TranslateService ) ;
47
55
48
56
fixture = ( < any > TestBed ) . createComponent ( App ) ;
49
57
fixture . detectChanges ( ) ;
@@ -55,12 +63,21 @@ describe('TranslateDirective', () => {
55
63
} ) ;
56
64
57
65
it ( 'should translate a string using the container value' , ( ) => {
58
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( ' TEST ' ) ;
66
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( 'TEST' ) ;
59
67
60
68
translate . setTranslation ( 'en' , { "TEST" : "This is a test" } ) ;
61
69
translate . use ( 'en' ) ;
62
70
63
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( ' This is a test ' ) ;
71
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( 'This is a test' ) ;
72
+ } ) ;
73
+
74
+ it ( 'should translate a string using the container value as a key' , ( ) => {
75
+ expect ( fixture . componentInstance . contentAsKey . nativeElement . innerHTML ) . toEqual ( 'TEST.VALUE' ) ;
76
+
77
+ translate . setTranslation ( 'en' , { "TEST" : { "VALUE" : "This is a test" } } ) ;
78
+ translate . use ( 'en' ) ;
79
+
80
+ expect ( fixture . componentInstance . contentAsKey . nativeElement . innerHTML ) . toEqual ( 'This is a test' ) ;
64
81
} ) ;
65
82
66
83
it ( 'should translate a string using the key value' , ( ) => {
@@ -130,57 +147,105 @@ describe('TranslateDirective', () => {
130
147
expect ( fixture . componentInstance . withParamsNoKey . nativeElement . innerHTML ) . toEqual ( 'It is changed' ) ;
131
148
} ) ;
132
149
150
+ it ( 'should update the DOM when the lang changes and the translation key starts with space' , ( ) => {
151
+ expect ( fixture . componentInstance . leadingSpaceNoKeyNoParams . nativeElement . innerHTML ) . toEqual ( ' TEST' ) ;
152
+
153
+ const en = "This is a test - with leading spaces in translation key" ;
154
+ const fr = "C'est un test - avec un espace de tête dans la clé de traduction" ;
155
+ const leadingSpaceFromKey = ' ' ;
156
+ translate . setTranslation ( 'en' , { "TEST" : en } ) ;
157
+ translate . setTranslation ( 'fr' , { "TEST" : fr } ) ;
158
+
159
+ translate . use ( 'en' ) ;
160
+ expect ( fixture . componentInstance . leadingSpaceNoKeyNoParams . nativeElement . innerHTML ) . toEqual ( leadingSpaceFromKey + en ) ;
161
+
162
+ translate . use ( 'fr' ) ;
163
+ expect ( fixture . componentInstance . leadingSpaceNoKeyNoParams . nativeElement . innerHTML ) . toEqual ( leadingSpaceFromKey + fr ) ;
164
+ } ) ;
165
+
166
+ it ( 'should update the DOM when the lang changes and the translation key has line breaks and spaces' , ( ) => {
167
+ expect ( fixture . componentInstance . withSpaceAndLineBreakNoKeyNoParams . nativeElement . innerHTML ) . toEqual ( ' TEST ' ) ;
168
+
169
+ const en = "This is a test - with trailing spaces in translation key" ;
170
+ const fr = "C'est un test - avec un espace de fuite dans la clé de traduction" ;
171
+ const whiteSpaceFromKey = ' ' ;
172
+ translate . setTranslation ( 'en' , { "TEST" : en } ) ;
173
+ translate . setTranslation ( 'fr' , { "TEST" : fr } ) ;
174
+
175
+ translate . use ( 'en' ) ;
176
+ expect ( fixture . componentInstance . withSpaceAndLineBreakNoKeyNoParams . nativeElement . innerHTML ) . toEqual ( whiteSpaceFromKey + en + whiteSpaceFromKey ) ;
177
+
178
+ translate . use ( 'fr' ) ;
179
+ expect ( fixture . componentInstance . withSpaceAndLineBreakNoKeyNoParams . nativeElement . innerHTML ) . toEqual ( whiteSpaceFromKey + fr + whiteSpaceFromKey ) ;
180
+ } ) ;
181
+
182
+ it ( 'should update the DOM when the lang changes and the translation key ends with space' , ( ) => {
183
+ expect ( fixture . componentInstance . trailingSpaceNoKeyNoParams . nativeElement . innerHTML ) . toEqual ( 'TEST ' ) ;
184
+
185
+ const en = "This is a test - with spaces and line breaks in translation key" ;
186
+ const fr = "C'est un test - avec des espaces et sauts de lignes dans la clé de traduction" ;
187
+ const trailingSpaceFromKey = ' ' ;
188
+ translate . setTranslation ( 'en' , { "TEST" : en } ) ;
189
+ translate . setTranslation ( 'fr' , { "TEST" : fr } ) ;
190
+
191
+ translate . use ( 'en' ) ;
192
+ expect ( fixture . componentInstance . trailingSpaceNoKeyNoParams . nativeElement . innerHTML ) . toEqual ( en + trailingSpaceFromKey ) ;
193
+
194
+ translate . use ( 'fr' ) ;
195
+ expect ( fixture . componentInstance . trailingSpaceNoKeyNoParams . nativeElement . innerHTML ) . toEqual ( fr + trailingSpaceFromKey ) ;
196
+ } ) ;
197
+
133
198
it ( 'should update the DOM when the lang changes' , ( ) => {
134
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( ' TEST ' ) ;
199
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( 'TEST' ) ;
135
200
expect ( fixture . componentInstance . withParams . nativeElement . innerHTML ) . toEqual ( 'TEST' ) ;
136
201
expect ( fixture . componentInstance . noContent . nativeElement . innerHTML ) . toEqual ( 'TEST' ) ;
137
202
138
203
translate . setTranslation ( 'en' , { "TEST" : "This is a test" } ) ;
139
204
translate . setTranslation ( 'fr' , { "TEST" : "C'est un test" } ) ;
140
205
141
206
translate . use ( 'en' ) ;
142
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( ' This is a test ' ) ;
207
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( 'This is a test' ) ;
143
208
expect ( fixture . componentInstance . withParams . nativeElement . innerHTML ) . toEqual ( 'This is a test' ) ;
144
209
expect ( fixture . componentInstance . noContent . nativeElement . innerHTML ) . toEqual ( 'This is a test' ) ;
145
210
146
211
translate . use ( 'fr' ) ;
147
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( " C'est un test " ) ;
212
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( "C'est un test" ) ;
148
213
expect ( fixture . componentInstance . withParams . nativeElement . innerHTML ) . toEqual ( "C'est un test" ) ;
149
214
expect ( fixture . componentInstance . noContent . nativeElement . innerHTML ) . toEqual ( "C'est un test" ) ;
150
215
} ) ;
151
216
152
217
it ( 'should update the DOM when the lang changes and the translation ends with space' , ( ) => {
153
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( ' TEST ' ) ;
218
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( 'TEST' ) ;
154
219
expect ( fixture . componentInstance . withParams . nativeElement . innerHTML ) . toEqual ( 'TEST' ) ;
155
220
expect ( fixture . componentInstance . noContent . nativeElement . innerHTML ) . toEqual ( 'TEST' ) ;
156
221
157
- const en = " This is a test - with spaces " ;
158
- const fr = " C'est un test - pardon, je ne parle pas francais :) " ;
222
+ const en = " This is a test - with spaces " ;
223
+ const fr = " C'est un test - avec espaces " ;
159
224
160
225
translate . setTranslation ( 'en' , { "TEST" : en } ) ;
161
226
translate . setTranslation ( 'fr' , { "TEST" : fr } ) ;
162
227
163
228
translate . use ( 'en' ) ;
164
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( ` ${ en } ` ) ;
229
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( `${ en } ` ) ;
165
230
expect ( fixture . componentInstance . withParams . nativeElement . innerHTML ) . toEqual ( en ) ;
166
231
expect ( fixture . componentInstance . noContent . nativeElement . innerHTML ) . toEqual ( en ) ;
167
232
168
233
translate . use ( 'fr' ) ;
169
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( ` ${ fr } ` ) ;
234
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( `${ fr } ` ) ;
170
235
expect ( fixture . componentInstance . withParams . nativeElement . innerHTML ) . toEqual ( fr ) ;
171
236
expect ( fixture . componentInstance . noContent . nativeElement . innerHTML ) . toEqual ( fr ) ;
172
237
} ) ;
173
238
174
239
it ( 'should update the DOM when the default lang changes' , ( ) => {
175
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( ' TEST ' ) ;
240
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( 'TEST' ) ;
176
241
177
242
translate . setTranslation ( 'en' , { "TEST" : "This is a test" } ) ;
178
243
translate . setTranslation ( 'fr' , { "TEST" : "C'est un test" } ) ;
179
244
translate . setDefaultLang ( 'en' ) ;
180
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( ' This is a test ' ) ;
245
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( 'This is a test' ) ;
181
246
182
247
translate . setDefaultLang ( 'fr' ) ;
183
- expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( " C'est un test " ) ;
248
+ expect ( fixture . componentInstance . noKey . nativeElement . innerHTML ) . toEqual ( "C'est un test" ) ;
184
249
} ) ;
185
250
186
251
it ( 'should unsubscribe from lang change subscription on destroy' , ( ) => {
0 commit comments