Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 843
Abel Avatar asked Nov 30 '25 07:11

Abel


1 Answers

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.

like image 104
Abel Avatar answered Dec 02 '25 03:12

Abel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!