1
1
import { Inject , Injectable , InjectionToken } from "@angular/core" ;
2
2
import { concat , forkJoin , isObservable , Observable , of , defer } from "rxjs" ;
3
3
import { concatMap , map , shareReplay , switchMap , take } from "rxjs/operators" ;
4
- import { MissingTranslationHandler , MissingTranslationHandlerParams } from "./missing-translation-handler" ;
4
+ import { MissingTranslationHandler } from "./missing-translation-handler" ;
5
5
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 { isDefined , isArray , isString , isDict , insertValue } from "./util" ;
9
+ import { isDefinedAndNotNull , isArray , isString , 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' ) ;
@@ -189,7 +189,7 @@ export class TranslateService {
189
189
// on init set the defaultLang immediately, but do not emit a change yet
190
190
this . store . setDefaultLang ( lang , false ) ;
191
191
}
192
-
192
+
193
193
const pending = this . loadOrExtendLanguage ( lang ) ;
194
194
if ( isObservable ( pending ) )
195
195
{
@@ -331,21 +331,17 @@ export class TranslateService {
331
331
{
332
332
const textToInterpolate = this . getTextToInterpolate ( key ) ;
333
333
334
- let res : Translation | undefined ;
335
-
336
- if ( textToInterpolate !== undefined )
334
+ if ( isDefinedAndNotNull ( textToInterpolate ) )
337
335
{
338
- res = this . runInterpolation ( textToInterpolate , interpolateParams ) ;
339
- }
340
- else
341
- {
342
- const params : MissingTranslationHandlerParams = { key, translateService : this } ;
343
- if ( typeof interpolateParams !== 'undefined' ) {
344
- params . interpolateParams = interpolateParams ;
345
- }
346
- res = this . missingTranslationHandler . handle ( params ) ;
336
+ return this . runInterpolation ( textToInterpolate , interpolateParams ) ;
347
337
}
348
338
339
+ const res = this . missingTranslationHandler . handle ( {
340
+ key,
341
+ translateService : this ,
342
+ ...( interpolateParams !== undefined && { interpolateParams } )
343
+ } ) ;
344
+
349
345
return res !== undefined ? res : key ;
350
346
}
351
347
@@ -358,66 +354,81 @@ export class TranslateService {
358
354
{
359
355
if ( isArray ( translations ) )
360
356
{
361
- return ( translations as Translation [ ] ) . map ( ( translation ) => this . runInterpolation ( translation , interpolateParams ) ) ;
357
+ return this . runInterpolationOnArray ( translations , interpolateParams ) ;
362
358
}
363
359
else if ( isDict ( translations ) )
364
360
{
365
- const result : TranslationObject = { } ;
366
- for ( const key in translations ) {
367
- const res = this . runInterpolation ( translations [ key ] , interpolateParams ) ;
368
- if ( res !== undefined )
369
- {
370
- result [ key ] = res ;
371
- }
372
- }
373
- return result ;
361
+ return this . runInterpolationOnDict ( translations , interpolateParams ) ;
374
362
}
375
363
else
376
364
{
377
365
return this . parser . interpolate ( translations , interpolateParams ) ;
378
366
}
379
367
}
380
368
369
+ private runInterpolationOnArray ( translations : InterpolatableTranslation , interpolateParams : InterpolationParameters | undefined )
370
+ {
371
+ return ( translations as Translation [ ] ) . map ( ( translation ) => this . runInterpolation ( translation , interpolateParams ) ) ;
372
+ }
373
+
374
+ private runInterpolationOnDict ( translations : InterpolatableTranslation , interpolateParams : InterpolationParameters | undefined )
375
+ {
376
+ const result : TranslationObject = { } ;
377
+ for ( const key in translations )
378
+ {
379
+ const res = this . runInterpolation ( translations [ key ] , interpolateParams ) ;
380
+ if ( res !== undefined )
381
+ {
382
+ result [ key ] = res ;
383
+ }
384
+ }
385
+ return result ;
386
+ }
387
+
381
388
/**
382
389
* Returns the parsed result of the translations
383
390
*/
384
- public getParsedResult ( key : string | string [ ] , interpolateParams ?: InterpolationParameters ) : Translation | TranslationObject | Observable < Translation | TranslationObject > {
385
-
386
- // handle a bunch of keys
387
- if ( key instanceof Array ) {
388
- const result : Record < string , Translation | Observable < Translation > > = { } ;
391
+ public getParsedResult ( key : string | string [ ] , interpolateParams ?: InterpolationParameters ) : Translation | TranslationObject | Observable < Translation | TranslationObject >
392
+ {
393
+ return ( key instanceof Array ) ? this . getParsedResultForArray ( key , interpolateParams ) : this . getParsedResultForKey ( key , interpolateParams ) ;
394
+ }
389
395
390
- let observables = false ;
391
- for ( const k of key ) {
392
- result [ k ] = this . getParsedResultForKey ( k , interpolateParams ) ;
393
- observables = observables || isObservable ( result [ k ] ) ;
394
- }
396
+ private getParsedResultForArray ( key : string [ ] , interpolateParams : InterpolationParameters | undefined )
397
+ {
398
+ const result : Record < string , Translation | Observable < Translation > > = { } ;
395
399
396
- if ( ! observables ) {
397
- return result as TranslationObject ;
398
- }
400
+ let observables = false ;
401
+ for ( const k of key )
402
+ {
403
+ result [ k ] = this . getParsedResultForKey ( k , interpolateParams ) ;
404
+ observables = observables || isObservable ( result [ k ] ) ;
405
+ }
399
406
400
- const sources : Observable < Translation > [ ] = key . map ( k => makeObservable ( result [ k ] ) ) ;
401
- return forkJoin ( sources ) . pipe (
402
- map ( ( arr : ( Translation ) [ ] ) => {
403
- const obj : TranslationObject = { } ;
404
- arr . forEach ( ( value :Translation , index : number ) => {
405
- obj [ key [ index ] ] = value ;
406
- } ) ;
407
- return obj ;
408
- } )
409
- ) ;
407
+ if ( ! observables )
408
+ {
409
+ return result as TranslationObject ;
410
410
}
411
411
412
- return this . getParsedResultForKey ( key , interpolateParams ) ;
412
+ const sources : Observable < Translation > [ ] = key . map ( k => makeObservable ( result [ k ] ) ) ;
413
+ return forkJoin ( sources ) . pipe (
414
+ map ( ( arr : ( Translation ) [ ] ) =>
415
+ {
416
+ const obj : TranslationObject = { } ;
417
+ arr . forEach ( ( value : Translation , index : number ) =>
418
+ {
419
+ obj [ key [ index ] ] = value ;
420
+ } ) ;
421
+ return obj ;
422
+ } )
423
+ ) ;
413
424
}
414
425
415
426
/**
416
427
* Gets the translated value of a key (or an array of keys)
417
428
* @returns the translated key, or an object of translated keys
418
429
*/
419
430
public get ( key : string | string [ ] , interpolateParams ?: InterpolationParameters ) : Observable < Translation | TranslationObject > {
420
- if ( ! isDefined ( key ) || ! key . length ) {
431
+ if ( ! isDefinedAndNotNull ( key ) || ! key . length ) {
421
432
throw new Error ( `Parameter "key" is required and cannot be empty` ) ;
422
433
}
423
434
// check if we are loading a new translation to use
@@ -438,7 +449,7 @@ export class TranslateService {
438
449
* @returns A stream of the translated key, or an object of translated keys
439
450
*/
440
451
public getStreamOnTranslationChange ( key : string | string [ ] , interpolateParams ?: InterpolationParameters ) : Observable < Translation | TranslationObject > {
441
- if ( ! isDefined ( key ) || ! key . length ) {
452
+ if ( ! isDefinedAndNotNull ( key ) || ! key . length ) {
442
453
throw new Error ( `Parameter "key" is required and cannot be empty` ) ;
443
454
}
444
455
@@ -459,7 +470,7 @@ export class TranslateService {
459
470
* @returns A stream of the translated key, or an object of translated keys
460
471
*/
461
472
public stream ( key : string | string [ ] , interpolateParams ?: InterpolationParameters ) : Observable < Translation | TranslationObject > {
462
- if ( ! isDefined ( key ) || ! key . length ) {
473
+ if ( ! isDefinedAndNotNull ( key ) || ! key . length ) {
463
474
throw new Error ( `Parameter "key" required` ) ;
464
475
}
465
476
@@ -480,7 +491,7 @@ export class TranslateService {
480
491
*/
481
492
public instant ( key : string | string [ ] , interpolateParams ?: InterpolationParameters ) : Translation | TranslationObject
482
493
{
483
- if ( ! isDefined ( key ) || key . length === 0 ) {
494
+ if ( ! isDefinedAndNotNull ( key ) || key . length === 0 ) {
484
495
throw new Error ( 'Parameter "key" is required and cannot be empty' ) ;
485
496
}
486
497
0 commit comments