Last active
July 6, 2022 15:59
-
-
Save BorisWechselberger/08e2424e1267ed27f9b4a046cc3357c8 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