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 > > = { } ;
@@ -185,6 +185,8 @@ export class TranslateService implements ITranslateService {
185
185
private missingTranslationHandler = inject ( MissingTranslationHandler ) ;
186
186
private store : TranslateStore = inject ( TranslateStore ) ;
187
187
188
+ private _loaderIndex ! : number ;
189
+
188
190
private readonly extend : boolean = false ;
189
191
190
192
/**
@@ -225,6 +227,8 @@ export class TranslateService implements ITranslateService {
225
227
}
226
228
227
229
constructor ( ) {
230
+ this . _addLoader ( ) ;
231
+
228
232
const config : TranslateServiceConfig = {
229
233
extend : false ,
230
234
fallbackLang : null ,
@@ -247,6 +251,17 @@ export class TranslateService implements ITranslateService {
247
251
}
248
252
}
249
253
254
+ private _addLoader ( ) {
255
+ while ( this . store . loaders . has ( this . _loaderIndex ) ) {
256
+ this . _loaderIndex ++ ;
257
+ }
258
+ this . store . loaders . set ( this . _loaderIndex , this . currentLoader ) ;
259
+ }
260
+
261
+ ngOnDestroy ( ) {
262
+ this . store . loaders . delete ( this . _loaderIndex ) ;
263
+ }
264
+
250
265
/**
251
266
* Sets the fallback language to use if a translation is not found in the
252
267
* current language
@@ -341,9 +356,22 @@ export class TranslateService implements ITranslateService {
341
356
) : Observable < InterpolatableTranslationObject > {
342
357
this . pending = true ;
343
358
344
- const loadingTranslations = this . currentLoader
345
- . getTranslation ( lang )
346
- . pipe ( shareReplay ( 1 ) , take ( 1 ) ) ;
359
+ const loaders = Array . from ( this . store . loaders . values ( ) ) ;
360
+ const requests = loaders . map ( ( loader ) => loader . getTranslation ( lang ) . pipe ( take ( 1 ) ) ) ;
361
+
362
+ // Merge all translation objects
363
+ const loadingTranslations = (
364
+ requests . length > 1
365
+ ? forkJoin ( requests ) . pipe (
366
+ map ( ( results : TranslationObject [ ] ) =>
367
+ results . reduce (
368
+ ( acc , curr ) => mergeDeep ( acc , curr ) ,
369
+ { } as TranslationObject ,
370
+ ) ,
371
+ ) ,
372
+ )
373
+ : requests [ 0 ]
374
+ ) . pipe ( shareReplay ( 1 ) , take ( 1 ) ) ;
347
375
348
376
this . loadingTranslations = loadingTranslations . pipe (
349
377
map ( ( res : TranslationObject ) => this . compiler . compileTranslations ( res , lang ) ) ,
0 commit comments