1- import { inject , Injectable , InjectionToken } from "@angular/core" ;
1+ import { inject , Injectable , InjectionToken , OnDestroy } from "@angular/core" ;
22import { concat , defer , forkJoin , isObservable , Observable , of } from "rxjs" ;
33import { concatMap , map , shareReplay , switchMap , take } from "rxjs/operators" ;
44import { MissingTranslationHandler } from "./missing-translation-handler" ;
55import { TranslateCompiler } from "./translate.compiler" ;
66import { TranslateLoader } from "./translate.loader" ;
77import { InterpolateFunction , TranslateParser } from "./translate.parser" ;
88import { TranslateStore } from "./translate.store" ;
9- import { insertValue , isArray , isDefinedAndNotNull , isDict , isString } from "./util" ;
9+ import { insertValue , isArray , isDefinedAndNotNull , isDict , isString , mergeDeep } from "./util" ;
1010
1111/**
1212 * Configuration object for the translation service.
@@ -173,7 +173,7 @@ export abstract class ITranslateService {
173173}
174174
175175@Injectable ( )
176- export class TranslateService implements ITranslateService {
176+ export class TranslateService implements ITranslateService , OnDestroy {
177177 private loadingTranslations ! : Observable < InterpolatableTranslationObject > ;
178178 private pending = false ;
179179 private _translationRequests : Record < Language , Observable < TranslationObject > > = { } ;
@@ -245,6 +245,12 @@ export class TranslateService implements ITranslateService {
245245 if ( config . extend ) {
246246 this . extend = true ;
247247 }
248+
249+ this . store . addLoader ( this . currentLoader ) ;
250+ }
251+
252+ ngOnDestroy ( ) : void {
253+ this . store . removeLoader ( this . currentLoader ) ;
248254 }
249255
250256 /**
@@ -341,11 +347,25 @@ export class TranslateService implements ITranslateService {
341347 ) : Observable < InterpolatableTranslationObject > {
342348 this . pending = true ;
343349
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+ }
347367
348- this . loadingTranslations = loadingTranslations . pipe (
368+ this . loadingTranslations = loadAndMerge . pipe ( shareReplay ( 1 ) , take ( 1 ) ) . pipe (
349369 map ( ( res : TranslationObject ) => this . compiler . compileTranslations ( res , lang ) ) ,
350370 shareReplay ( 1 ) ,
351371 take ( 1 ) ,
@@ -362,7 +382,7 @@ export class TranslateService implements ITranslateService {
362382 } ,
363383 } ) ;
364384
365- return loadingTranslations ;
385+ return this . loadingTranslations ;
366386 }
367387
368388 /**
0 commit comments