While utilizing multipleTranslateHttpLoader
, I encountered an issue.
To resolve it, I had to develop my own httpLoader.
import { HttpBackend, HttpClient } from '@angular/common/http'
import { TranslateLoader } from '@ngx-translate/core'
import * as merge from 'deepmerge'
import { forkJoin, Observable, of } from 'rxjs'
import { catchError, map } from 'rxjs/operators'
export class MegaMultiTranslateHttpLoader implements TranslateLoader {
constructor(private _handler: HttpBackend, private _resourcesPrefix: string[]) {}
public getTranslation(lang: string): Observable<any> {
const requests = this._resourcesPrefix.map((prefix) => {
const path = `${prefix}${lang}.json`
return new HttpClient(this._handler).get(path).pipe(
catchError((res) => {
console.group('MegaMultiTranslateHttpLoader')
console.error('Something went wrong for the following translation file:', path)
console.error('Content of the error: ', res)
console.groupEnd()
return of({})
}),
)
})
return forkJoin(requests).pipe(map((response) => merge.all(response)))
}
}
In your app.module.ts
file, include:
export function HttpLoaderFactory(_httpBackend: HttpBackend) {
return new MegaMultiTranslateHttpLoader(_httpBackend, ['/assets/i18n/', '/assets/vendors/i18n/'])
}
@NgModule({
imports: [
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: HttpLoaderFactory,
deps: [HttpBackend],
},
}),
// ...
]
// ...
})
Points to Note
- The
suffix
has been omitted assuming usage of .json files only
- Here we utilize
httpBackend
instead of httpClient
- Make sure to install
npm i deepmerge
which is automatically installed by the ngx-translate-multi-http-loader library
Update
Updating to the latest version of ngx-translate-multi-http-loader should resolve this issue