1
- import { inject , Injectable , InjectionToken } from "@angular/core" ;
1
+ import { inject , Injectable , InjectionToken , OnDestroy } from "@angular/core" ;
2
2
import { concat , defer , forkJoin , isObservable , Observable , of } from "rxjs" ;
3
3
import { concatMap , map , shareReplay , switchMap , take } from "rxjs/operators" ;
4
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 { insertValue , isArray , isDefinedAndNotNull , isDict , isString } from "./util" ;
9
+ import { insertValue , isArray , isDefinedAndNotNull , isDict , isString , mergeDeep } from "./util" ;
10
10
11
11
/**
12
12
* Configuration object for the translation service.
@@ -173,7 +173,7 @@ export abstract class ITranslateService {
173
173
}
174
174
175
175
@Injectable ( )
176
- export class TranslateService implements ITranslateService {
176
+ export class TranslateService implements ITranslateService , OnDestroy {
177
177
private loadingTranslations ! : Observable < InterpolatableTranslationObject > ;
178
178
private pending = false ;
179
179
private _translationRequests : Record < Language , Observable < TranslationObject > > = { } ;
@@ -245,6 +245,12 @@ export class TranslateService implements ITranslateService {
245
245
if ( config . extend ) {
246
246
this . extend = true ;
247
247
}
248
+
249
+ this . store . addLoader ( this . currentLoader ) ;
250
+ }
251
+
252
+ ngOnDestroy ( ) : void {
253
+ this . store . removeLoader ( this . currentLoader ) ;
248
254
}
249
255
250
256
/**
@@ -341,11 +347,25 @@ export class TranslateService implements ITranslateService {
341
347
) : Observable < InterpolatableTranslationObject > {
342
348
this . pending = true ;
343
349
344
- const loadingTranslations = this . currentLoader
345
- . getTranslation ( lang )
346
- . pipe ( shareReplay ( 1 ) , take ( 1 ) ) ;
350
+ const loaders = this . store . getLoaders ( ) ;
351
+ let loadAndMerge : Observable < TranslationObject > ;
352
+
353
+ if ( loaders . length === 0 ) {
354
+ return of ( { } as InterpolatableTranslationObject ) ;
355
+ } else if ( loaders . length === 1 ) {
356
+ loadAndMerge = loaders [ 0 ] . getTranslation ( lang ) ;
357
+ } else {
358
+ const requests : Observable < TranslationObject > [ ] = loaders . map ( ( loader ) =>
359
+ loader . getTranslation ( lang ) . pipe ( take ( 1 ) ) ,
360
+ ) ;
361
+ loadAndMerge = forkJoin ( requests ) . pipe (
362
+ map ( ( results : TranslationObject [ ] ) =>
363
+ results . reduce ( ( acc , curr ) => mergeDeep ( acc , curr ) , { } as TranslationObject ) ,
364
+ ) ,
365
+ ) ;
366
+ }
347
367
348
- this . loadingTranslations = loadingTranslations . pipe (
368
+ this . loadingTranslations = loadAndMerge . pipe ( shareReplay ( 1 ) , take ( 1 ) ) . pipe (
349
369
map ( ( res : TranslationObject ) => this . compiler . compileTranslations ( res , lang ) ) ,
350
370
shareReplay ( 1 ) ,
351
371
take ( 1 ) ,
@@ -362,7 +382,7 @@ export class TranslateService implements ITranslateService {
362
382
} ,
363
383
} ) ;
364
384
365
- return loadingTranslations ;
385
+ return this . loadingTranslations ;
366
386
}
367
387
368
388
/**
0 commit comments