ngx-translate

get nested json value using translateService.get() of ngx-translate


I have a translation file with nested translations :

...
"LANG": {
    "Dutch": "Néerlandais",
    "English": "Anglais",
    "French": "Français"
},
...

I'm trying to retreive my translations using TranslateService :

this.translate.get(['LANG.French', 'LANG.English', 'LANG.Dutch'])
  .subscribe((translations) => {
    console.log(translations.LANG.French);
});

I thought the translations object would look like this :

translations : {
    LANG : {
        "Dutch": "Néerlandais",
        "English": "Anglais",
        "French": "Français"
    }
}

But in fact it looks like this :

translations : {
    LANG.Dutch : "Néerlandais",
    LANG.English: "Anglais",
    LANG.French: "Français"
}

And console.log(translations.LANG.French) fails because translations.LANG is undefined.

Is there a way to achieve this ?


Solution

  • In case someone comes here :

    
    this.translate.get(['LANG'])
         .subscribe((translations) => {
        console.log(translations.LANG.French);
    });
    

    It will work but will retrieve the whole object LANG. Thus it may contain some useless properties.