Naigel Naigel - 11 days ago 5
AngularJS Question

angular-translate translation object with some equal translations

I'm using

angular-translate
to make a web application multilanguage. Everything works fine, and by now I have structured my translations so that I have:

$translateProvider.translations("en", {
"LANGUAGE": {
"ENGLISH": "english",
"GERMAN": "german",
"ITALIAN": "italian",
"FRENCH": "french",
}
});


Let's say I want to add
BUTTONS.ENGLISH
with the same definition of
LANGUAGE.ENGLISH
. Is it possible to reference it in some way like
BUTTONS.ENGLISH = this.LANGUAGE.ENGLISH
?

$translateProvider.translations("en", {
"LANGUAGE": {
"ENGLISH": "english",
"GERMAN": "german",
"ITALIAN": "italian",
"FRENCH": "french",
},
"BUTTONS": {
"ENGLISH": this["LANGUAGE"]["ENGLISH"] // ???
}
});

Answer

The way you are trying to do is not possible because of javascript scope issues but you can always use interpolation inside a translation to call another translation like the following example.

 "ENGLISH": "{{ 'LANGUAGE.ENGLISH' | translate }}"

This way you can guarantee that your key will correspond to another key value which maybe can make it easier to maintain.

Full code:

$translateProvider.translations("en", {
    "LANGUAGE": {
        "ENGLISH": "english",
        "GERMAN": "german",
        "ITALIAN": "italian",
        "FRENCH": "french",
    },
    "BUTTONS": {
        "ENGLISH": "{{ 'LANGUAGE.ENGLISH' | translate }}"
    }
});
Comments