Forked from BorisWechselberger/MultiTranslateHttpLoader.ts
Created
August 24, 2018 10:49
-
-
Save shameed/aee37119c8202b712b729c2eed417a1c to your computer and use it in GitHub Desktop.
TranslateLoader for multiple JSON files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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