Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save shameed/aee37119c8202b712b729c2eed417a1c to your computer and use it in GitHub Desktop.
Save shameed/aee37119c8202b712b729c2eed417a1c to your computer and use it in GitHub Desktop.
TranslateLoader for multiple JSON files
import {HttpClient} from '@angular/common/http';
import {TranslateLoader} from '@ngx-translate/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/forkJoin';
export function translateLoader(http: HttpClient) {
return new MultiTranslateHttpLoader(http, [
{prefix: './assets/i18n/', suffix: '.json'},
{prefix: './assets/i18n/countries-', suffix: '.json'}
]);
}
export class MultiTranslateHttpLoader implements TranslateLoader {
constructor(private http: HttpClient,
public resources: { prefix: string, suffix: string }[] = [{
prefix: '/assets/i18n/',
suffix: '.json'
}]) {}
/**
* Gets the translations from the server
* @param lang
* @returns {any}
*/
public getTranslation(lang: string): any {
return Observable.forkJoin(this.resources.map(config => {
return this.http.get(`${config.prefix}${lang}${config.suffix}`);
})).map(response => {
return response.reduce((a, b) => {
return Object.assign(a, b);
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment