Forked from BorisWechselberger/MultiTranslateHttpLoader.ts
Last active
March 5, 2018 08:31
-
-
Save jarodsmk/e1bf766225bdbcdec07edbcc060d9558 to your computer and use it in GitHub Desktop.
TranslateLoader for multiple JSON files
This file contains hidden or 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