11import { HttpBackend , HttpClient } from "@angular/common/http" ;
22import { inject , Injectable , InjectionToken , Provider } from "@angular/core" ;
3- import { TranslateLoader , TranslationObject } from "@ngx-translate/core" ;
4- import { Observable } from "rxjs" ;
3+ import { mergeDeep , TranslateLoader , TranslationObject } from "@ngx-translate/core" ;
4+ import { forkJoin , map , Observable } from "rxjs" ;
5+
6+ export interface TranslateHttpLoaderResource {
7+ prefix : string ;
8+ suffix ?: string ;
9+ }
510
611export interface TranslateHttpLoaderConfig {
712 prefix : string ;
813 suffix : string ;
14+ ressources : ( string | TranslateHttpLoaderResource ) [ ] ;
915 enforceLoading : boolean ;
1016 useHttpBackend : boolean ;
1117}
@@ -23,6 +29,7 @@ export class TranslateHttpLoader implements TranslateLoader {
2329 this . config = {
2430 prefix : "/assets/i18n/" ,
2531 suffix : ".json" ,
32+ ressources : [ ] ,
2633 enforceLoading : false ,
2734 useHttpBackend : false ,
2835 ...inject ( TRANSLATE_HTTP_LOADER_CONFIG ) ,
@@ -39,9 +46,24 @@ export class TranslateHttpLoader implements TranslateLoader {
3946 public getTranslation ( lang : string ) : Observable < TranslationObject > {
4047 const cacheBuster = this . config . enforceLoading ? `?enforceLoading=${ Date . now ( ) } ` : "" ;
4148
42- return this . http . get (
49+ if ( this . config . ressources . length > 0 ) {
50+ const requests = this . config . ressources . map ( ( resource ) => {
51+ let path : string ;
52+
53+ if ( typeof resource === "string" ) path = `${ resource } ${ lang } .json` ;
54+ else path = `${ resource . prefix } ${ lang } ${ resource . suffix ?? ".json" } ` ;
55+
56+ return this . http . get ( `${ path } ${ cacheBuster } ` ) ;
57+ } ) as Observable < TranslationObject > [ ] ;
58+
59+ return forkJoin ( requests ) . pipe (
60+ map ( ( response ) => response . reduce ( ( acc , curr ) => mergeDeep ( acc , curr ) , { } ) ) ,
61+ ) as Observable < TranslationObject > ;
62+ }
63+
64+ return this . http . get < TranslationObject > (
4365 `${ this . config . prefix } ${ lang } ${ this . config . suffix } ${ cacheBuster } ` ,
44- ) as Observable < TranslationObject > ;
66+ ) ;
4567 }
4668}
4769
0 commit comments