Skip to content

Instantly share code, notes, and snippets.

@ukcoderj
Created May 26, 2021 10:39
Show Gist options
  • Save ukcoderj/135c5b8f33e952d073b39b2b72218719 to your computer and use it in GitHub Desktop.
Save ukcoderj/135c5b8f33e952d073b39b2b72218719 to your computer and use it in GitHub Desktop.
// backup copy of https://github.com/denniske/ngx-translate-multi-http-loader
import {HttpClient} from "@angular/common/http";
import {TranslateLoader} from "@ngx-translate/core";
import {Observable, forkJoin, of} from "rxjs";
import {catchError, map} from "rxjs/operators";
import merge from 'deepmerge';
export interface ITranslationResource {
prefix: string;
suffix: string;
}
export class MultiTranslateHttpLoader implements TranslateLoader {
constructor(
private http: HttpClient,
private resources: ITranslationResource[],
) {}
public getTranslation(lang: string): Observable<any> {
const requests = this.resources.map(resource => {
const path = resource.prefix + lang + resource.suffix;
return this.http.get(path).pipe(catchError(res => {
console.error("Something went wrong for the following translation file:", path);
console.error(res.message);
return of({});
}));
});
return forkJoin(requests).pipe(map(response => merge.all(response)));
}
}
// export function HttpLoaderFactory(http: HttpClient) {
// return new MultiTranslateHttpLoader(http, [
// /* { prefix: "./assets/translate/core/", suffix: ".json" },*/
// { prefix: "assets/i18n/participation/", suffix: ".json" },
// { prefix: "assets/i18n/shared/", suffix: ".json" },
// ]);
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment