Using ngx-translate to translate data from json files
I'm using json server to get my translation files. I get them in my home.ts like this:
export class HomePage {
colors: any; // <-- THIS IS IMPORTANT
constructor(public navCtrl: NavController, public restProvider: RestProvider) {
this.getEnglish();
this.getGerman();
}
getGerman() {
this.restProvider.getGermanFromApi()
.then(data => {
this.colors= data;
console.log(this.colors);
});
}
getEnglish() {
this.restProvider.getEnglishFromApi()
.then(data => {
this.colors= data;
console.log(this.colors);
});
}
My rest.ts
getGermanFromApi() {
return new Promise(resolve => {
this.http.get(this.apiUrl+'/german').subscribe(data => {
resolve(data);
}, err => {
console.log(err);
});
});
}
//SAME FUNCTION FOR ENGLISH
My json files de.json
[
{
"id": 1,
"colorname ":"blau"
},
{
"id": 2,
"colorname ":"gelb"
}
]
//SAME FOR ENGLISH
Now I want to loop through the colors like this:
<li *ngFor="let color of colors">
<a>{{color.colorname | translate}}</a>
</li>
Output: blau gelb
When I change the language to English, the output is still "blau gelb". The reason for this is because I save the data I get from the api in "colors" which you can see in my home.ts. In the constructor I first use the getEnglish() function, which saves the data from the api in colors. Right after this function I call getGerman() which also saves the data in colors, so I override it and only have german data in colors.
Now to my question: I guess I shouldn't save both data I get from German and English in the colors array because I override the english data like this. What would be a better solution? I basically want to get data from both languages and change the language.