1- import { HttpBackend , HttpClient } from "@angular/common/http" ;
1+ import { HttpBackend , HttpClient , HttpErrorResponse } from "@angular/common/http" ;
22import { inject , Injectable , InjectionToken , Provider } from "@angular/core" ;
33import { mergeDeep , TranslateLoader , TranslationObject } from "@ngx-translate/core" ;
44import { catchError , forkJoin , map , Observable , of } from "rxjs" ;
55
6+ export interface TranslateHttpLoaderConfig {
7+ prefix ?: string ;
8+ suffix ?: string ;
9+ showLog ?: boolean ;
10+ enforceLoading : boolean ;
11+ useHttpBackend : boolean ;
12+ }
13+
614export interface TranslateHttpLoaderResource {
715 prefix : string ;
816 suffix ?: string ;
917 showLog ?: boolean ;
1018}
1119
12- export interface TranslateHttpLoaderConfig {
13- /**
14- * @deprecated Use `resources` instead. `prefix` and `suffix` will be removed in a future version.
15- */
16- prefix ?: string ;
17- /**
18- * @deprecated Use `resources` instead. `prefix` and `suffix` will be removed in a future version.
19- */
20- suffix ?: string ;
20+ export interface TranslateMultiHttpLoaderConfig {
2121 showLog ?: boolean ;
2222 resources : ( string | TranslateHttpLoaderResource ) [ ] ;
2323 enforceLoading : boolean ;
2424 useHttpBackend : boolean ;
2525}
2626
27- export const TRANSLATE_HTTP_LOADER_CONFIG = new InjectionToken < Partial < TranslateHttpLoaderConfig > > (
28- "TRANSLATE_HTTP_LOADER_CONFIG" ,
29- ) ;
27+ export const TRANSLATE_HTTP_LOADER_CONFIG = new InjectionToken <
28+ Partial < TranslateMultiHttpLoaderConfig >
29+ > ( "TRANSLATE_HTTP_LOADER_CONFIG" ) ;
3030
3131@Injectable ( )
3232export class TranslateHttpLoader implements TranslateLoader {
3333 private http : HttpClient ;
34- private config : TranslateHttpLoaderConfig ;
34+ private config : TranslateMultiHttpLoaderConfig ;
3535
3636 constructor ( ) {
3737 this . config = {
38- prefix : "/assets/i18n/" ,
39- suffix : ".json" ,
4038 resources : [ ] ,
4139 enforceLoading : false ,
4240 useHttpBackend : false ,
41+ showLog : false ,
4342 ...inject ( TRANSLATE_HTTP_LOADER_CONFIG ) ,
4443 } ;
4544
@@ -54,18 +53,15 @@ export class TranslateHttpLoader implements TranslateLoader {
5453 public getTranslation ( lang : string ) : Observable < TranslationObject > {
5554 const cacheBuster = this . config . enforceLoading ? `?enforceLoading=${ Date . now ( ) } ` : "" ;
5655
57- if ( ( ! this . config . resources || this . config . resources . length <= 0 ) && this . config . prefix )
58- this . config . resources = [ { prefix : this . config . prefix , suffix : this . config . suffix } ] ;
59-
6056 const requests = this . config . resources . map ( ( resource ) => {
6157 let path : string ;
6258
6359 if ( typeof resource === "string" ) path = `${ resource } ${ lang } .json` ;
6460 else path = `${ resource . prefix } ${ lang } ${ resource . suffix ?? ".json" } ` ;
6561
6662 return this . http . get < TranslationObject > ( `${ path } ${ cacheBuster } ` ) . pipe (
67- catchError ( ( err ) => {
68- if ( this . config . showLog || ( resource as TranslateHttpLoaderResource ) . showLog ) {
63+ catchError ( ( err : HttpErrorResponse ) => {
64+ if ( this . config . showLog ) {
6965 console . error ( `Error loading translation for ${ lang } :` , err ) ;
7066 }
7167 return of ( { } ) ;
@@ -80,7 +76,32 @@ export class TranslateHttpLoader implements TranslateLoader {
8076}
8177
8278export function provideTranslateHttpLoader (
83- config : Partial < TranslateHttpLoaderConfig > = { } ,
79+ config : Partial < TranslateHttpLoaderConfig | TranslateMultiHttpLoaderConfig > = { } ,
80+ ) : Provider [ ] {
81+ // If config already has resources, it's a multi-config, pass it through
82+ if ( "resources" in config && config . resources ) {
83+ return provideTranslateMultiHttpLoader ( config as Partial < TranslateMultiHttpLoaderConfig > ) ;
84+ }
85+
86+ // Otherwise, convert single config to multi-config
87+ const singleConfig = config as Partial < TranslateHttpLoaderConfig > ;
88+ const multiConfig : Partial < TranslateMultiHttpLoaderConfig > = {
89+ showLog : singleConfig . showLog ?? false ,
90+ enforceLoading : singleConfig . enforceLoading ?? false ,
91+ useHttpBackend : singleConfig . useHttpBackend ?? false ,
92+ resources : [
93+ {
94+ prefix : singleConfig . prefix ?? "/assets/i18n/" ,
95+ suffix : singleConfig . suffix ?? ".json" ,
96+ } ,
97+ ] ,
98+ } ;
99+
100+ return provideTranslateMultiHttpLoader ( multiConfig ) ;
101+ }
102+
103+ export function provideTranslateMultiHttpLoader (
104+ config : Partial < TranslateMultiHttpLoaderConfig > = { } ,
84105) : Provider [ ] {
85106 const useBackend = config . useHttpBackend ?? false ;
86107
0 commit comments