@@ -6,7 +6,7 @@ import {TranslateCompiler} from "./translate.compiler";
6
6
import { TranslateLoader } from "./translate.loader" ;
7
7
import { InterpolateFunction , TranslateParser } from "./translate.parser" ;
8
8
import { TranslateStore } from "./translate.store" ;
9
- import { getValue , isDefined , isArray , isString , mergeDeep , setValue , isDict } from "./util" ;
9
+ import { getValue , isDefined , isArray , isString , setValue , isDict , insertValue } from "./util" ;
10
10
11
11
export const ISOLATE_TRANSLATE_SERVICE = new InjectionToken < string > ( 'ISOLATE_TRANSLATE_SERVICE' ) ;
12
12
export const USE_DEFAULT_LANG = new InjectionToken < string > ( 'USE_DEFAULT_LANG' ) ;
@@ -91,8 +91,8 @@ const makeObservable = <T>(value: T | Observable<T>): Observable<T> => {
91
91
export class TranslateService {
92
92
private loadingTranslations ! : Observable < InterpolatableTranslationObject > ;
93
93
private pending = false ;
94
- private _translationRequests : Record < string , Observable < TranslationObject > > = { } ;
95
- private lastUseLanguage : string | null = null ;
94
+ private _translationRequests : Record < Language , Observable < TranslationObject > > = { } ;
95
+ private lastUseLanguage : Language | null = null ;
96
96
97
97
98
98
/**
@@ -128,45 +128,22 @@ export class TranslateService {
128
128
/**
129
129
* The default lang to fallback when translations are missing on the current lang
130
130
*/
131
- get defaultLang ( ) : string {
132
- return this . store . defaultLang ;
133
- }
134
-
135
- set defaultLang ( defaultLang : string ) {
136
- this . store . defaultLang = defaultLang ;
131
+ get defaultLang ( ) : Language {
132
+ return this . store . getDefaultLanguage ( ) ;
137
133
}
138
134
139
135
/**
140
136
* The lang currently used
141
137
*/
142
- get currentLang ( ) : string {
143
- return this . store . currentLang ;
144
- }
145
-
146
- set currentLang ( currentLang : string ) {
147
- this . store . currentLang = currentLang ;
138
+ get currentLang ( ) : Language {
139
+ return this . store . getCurrentLanguage ( ) ;
148
140
}
149
141
150
142
/**
151
143
* an array of langs
152
144
*/
153
- get langs ( ) : string [ ] {
154
- return this . store . langs ;
155
- }
156
-
157
- set langs ( langs : string [ ] ) {
158
- this . store . langs = langs ;
159
- }
160
-
161
- /**
162
- * a list of translations per lang
163
- */
164
- get translations ( ) : Record < string , InterpolatableTranslationObject > {
165
- return this . store . translations ;
166
- }
167
-
168
- set translations ( translations : Record < string , InterpolatableTranslationObject > ) {
169
- this . store . translations = translations ;
145
+ get langs ( ) : readonly Language [ ] {
146
+ return this . store . getLanguages ( ) ;
170
147
}
171
148
172
149
/**
@@ -213,17 +190,21 @@ export class TranslateService {
213
190
const pending = this . retrieveTranslations ( lang ) ;
214
191
215
192
if ( typeof pending !== "undefined" ) {
216
- // on init set the defaultLang immediately
193
+
217
194
if ( this . defaultLang == null ) {
218
- this . defaultLang = lang ;
195
+ // on init set the defaultLang immediately
196
+ // but do not emit the change yet
197
+ this . store . setDefaultLang ( lang , false ) ;
219
198
}
220
199
221
200
pending . pipe ( take ( 1 ) )
222
201
. subscribe ( ( ) => {
223
- this . changeDefaultLang ( lang ) ;
202
+ this . store . setDefaultLang ( lang ) ;
224
203
} ) ;
225
- } else { // we already have this language
226
- this . changeDefaultLang ( lang ) ;
204
+ }
205
+ else {
206
+ // we already have this language
207
+ this . store . setDefaultLang ( lang ) ;
227
208
}
228
209
}
229
210
@@ -246,12 +227,12 @@ export class TranslateService {
246
227
247
228
// don't change the language if the language given is already selected
248
229
if ( lang === this . currentLang ) {
249
- return of ( this . translations [ lang ] ) ;
230
+ return of ( this . store . getTranslations ( lang ) ) ;
250
231
}
251
232
252
233
// on init set the currentLang immediately
253
234
if ( ! this . currentLang ) {
254
- this . currentLang = lang ;
235
+ this . store . setCurrentLang ( lang , false ) ;
255
236
}
256
237
257
238
const pending = this . retrieveTranslations ( lang ) ;
@@ -268,7 +249,7 @@ export class TranslateService {
268
249
else {
269
250
// we have this language, return an Observable
270
251
this . changeLang ( lang ) ;
271
- return of ( this . translations [ lang ] ) ;
252
+ return of ( this . store . getTranslations ( lang ) ) ;
272
253
}
273
254
}
274
255
@@ -278,20 +259,18 @@ export class TranslateService {
278
259
*/
279
260
private changeLang ( lang : string ) : void {
280
261
281
- // received a new language file
282
- // but this was not the one requested last
283
262
if ( lang !== this . lastUseLanguage )
284
263
{
264
+ // received new language data,
265
+ // but this was not the one requested last
285
266
return ;
286
267
}
287
268
288
- this . currentLang = lang ;
289
-
290
- this . store . emitLangChange ( { lang : lang , translations : this . translations [ lang ] } ) ;
269
+ this . store . setCurrentLang ( lang ) ;
291
270
292
- // if there is no default lang, use the one that we just set
293
271
if ( this . defaultLang == null ) {
294
- this . changeDefaultLang ( lang ) ;
272
+ // if there is no default lang, use the one that we just set
273
+ this . store . setDefaultLang ( lang ) ;
295
274
}
296
275
}
297
276
@@ -302,7 +281,7 @@ export class TranslateService {
302
281
private retrieveTranslations ( lang : string ) : Observable < TranslationObject > | undefined {
303
282
304
283
// if this language is unavailable or extend is true, ask for it
305
- if ( typeof this . translations [ lang ] === "undefined" || this . extend ) {
284
+ if ( ! this . store . hasTranslationFor ( lang ) || this . extend ) {
306
285
this . _translationRequests [ lang ] = this . _translationRequests [ lang ] || this . loadAndCompileTranslations ( lang ) ;
307
286
return this . _translationRequests [ lang ] ;
308
287
}
@@ -344,8 +323,7 @@ export class TranslateService {
344
323
this . loadingTranslations
345
324
. subscribe ( {
346
325
next : ( res : InterpolatableTranslationObject ) => {
347
- this . translations [ lang ] = ( this . extend && this . translations [ lang ] ) ? { ...res , ...this . translations [ lang ] } : res ;
348
- this . updateLangs ( ) ;
326
+ this . store . setTranslations ( lang , res , this . extend ) ;
349
327
this . pending = false ;
350
328
} ,
351
329
error : ( err ) => {
@@ -361,41 +339,24 @@ export class TranslateService {
361
339
* Manually sets an object of translations for a given language
362
340
* after passing it through the compiler
363
341
*/
364
- public setTranslation ( lang : string , translations : InterpolatableTranslationObject , shouldMerge = false ) : void {
342
+ public setTranslation ( lang : Language , translations : InterpolatableTranslationObject , shouldMerge = false ) : void {
365
343
const interpolatableTranslations = this . compiler . compileTranslations ( translations , lang ) ;
366
- if ( ( shouldMerge || this . extend ) && this . translations [ lang ] ) {
367
- this . translations [ lang ] = mergeDeep ( this . translations [ lang ] , interpolatableTranslations ) ;
368
- } else {
369
- this . translations [ lang ] = interpolatableTranslations ;
370
- }
371
- this . updateLangs ( ) ;
372
- this . store . emitTranslationChange ( { lang : lang , translations : this . translations [ lang ] } ) ;
344
+ this . store . setTranslations ( lang , interpolatableTranslations , ( shouldMerge || this . extend ) ) ;
373
345
}
374
346
375
- /**
376
- * Returns an array of currently available langs
377
- */
378
- public getLangs ( ) : string [ ] {
379
- return this . langs ;
347
+
348
+ public getLangs ( ) : readonly Language [ ] {
349
+ return this . store . getLanguages ( ) ;
380
350
}
381
351
382
352
/**
383
353
* Add available languages
384
354
*/
385
- public addLangs ( langs : string [ ] ) : void
355
+ public addLangs ( languages : Language [ ] ) : void
386
356
{
387
- const newLangs = langs . filter ( lang => ! this . langs . includes ( lang ) ) ;
388
- if ( newLangs . length > 0 ) {
389
- this . langs = [ ...this . langs , ...newLangs ] ;
390
- }
357
+ this . store . addLanguages ( languages ) ;
391
358
}
392
359
393
- /**
394
- * Update the list of available languages
395
- */
396
- private updateLangs ( ) : void {
397
- this . addLangs ( Object . keys ( this . translations ) ) ;
398
- }
399
360
400
361
private getParsedResultForKey ( translations : InterpolatableTranslation , key : string , interpolateParams ?: InterpolationParameters ) : Translation | Observable < Translation >
401
362
{
@@ -424,7 +385,7 @@ export class TranslateService {
424
385
let text = getValue ( translations , key ) ;
425
386
if ( text === undefined && this . defaultLang != null && this . defaultLang !== this . currentLang && this . useDefaultLang )
426
387
{
427
- text = getValue ( this . translations [ this . defaultLang ] , key ) ;
388
+ text = getValue ( this . store . getTranslations ( this . defaultLang ) , key ) ;
428
389
}
429
390
return text ;
430
391
}
@@ -504,7 +465,7 @@ export class TranslateService {
504
465
) ;
505
466
}
506
467
507
- return makeObservable ( this . getParsedResult ( this . translations [ this . currentLang ] , key , interpolateParams ) ) ;
468
+ return makeObservable ( this . getParsedResult ( this . store . getTranslations ( this . currentLang ) , key , interpolateParams ) ) ;
508
469
}
509
470
510
471
/**
@@ -559,7 +520,7 @@ export class TranslateService {
559
520
throw new Error ( 'Parameter "key" is required and cannot be empty' ) ;
560
521
}
561
522
562
- const result = this . getParsedResult ( this . translations [ this . currentLang ] , key , interpolateParams ) ;
523
+ const result = this . getParsedResult ( this . store . getTranslations ( this . currentLang ) , key , interpolateParams ) ;
563
524
564
525
if ( isObservable ( result ) ) {
565
526
if ( Array . isArray ( key ) ) {
@@ -577,23 +538,19 @@ export class TranslateService {
577
538
/**
578
539
* Sets the translated value of a key, after compiling it
579
540
*/
580
- public set ( key : string , translation : Translation , lang : string = this . currentLang ) : void {
581
- setValue ( this . translations [ lang ] , key ,
582
- isString ( translation )
583
- ? this . compiler . compile ( translation , lang )
584
- : this . compiler . compileTranslations ( translation , lang )
541
+ public set ( key : string , translation : Translation , lang : Language = this . currentLang ) : void {
542
+
543
+ this . store . setTranslations (
544
+ lang ,
545
+ insertValue ( this . store . getTranslations ( lang ) , key ,
546
+ isString ( translation )
547
+ ? this . compiler . compile ( translation , lang )
548
+ : this . compiler . compileTranslations ( translation , lang )
549
+ ) ,
550
+ false
585
551
) ;
586
- this . updateLangs ( ) ;
587
- this . store . emitTranslationChange ( { lang : lang , translations : this . translations [ lang ] } ) ;
588
552
}
589
553
590
- /**
591
- * Changes the default lang
592
- */
593
- private changeDefaultLang ( lang : string ) : void {
594
- this . defaultLang = lang ;
595
- this . store . emitDefaultLangChange ( { lang : lang , translations : this . translations [ lang ] } ) ;
596
- }
597
554
598
555
/**
599
556
* Allows to reload the lang file from the file
@@ -608,7 +565,7 @@ export class TranslateService {
608
565
*/
609
566
public resetLang ( lang : string ) : void {
610
567
delete this . _translationRequests [ lang ] ;
611
- delete this . translations [ lang ] ;
568
+ this . store . deleteTranslations ( lang ) ;
612
569
}
613
570
614
571
/**
0 commit comments