Search code examples
angularjsmongodbinternationalizationangular-translate

How to translate the content of an attribute with angular-translate


in my mongo schema i have an enum with predefined types:

let MeterSchema = new Schema({
[...]
type: {
    type: String,
    enum: ['Prepayment', 'TimeOfDay', 'PowerExport']
},
[...]
}

What i want is to display this value internationalized with i18n files on my angular view. I looked at the Variale replacement of angular translate (https://angular-translate.github.io/docs/#/guide/06_variable-replacement), but could not figure it out how do it properly with this.

At the moment i translate it that way:

View

<div class="md-summary">{{vm.getMeterType(meter) | translate}}</div>

Controller

public getMeterType(meter): String {
    return 'app.masterData.meters.type.' + meter.type;
}

But i think there has to be a better way to this.


Solution

  • The answer is actually pretty simple:

    <div class="md-summary">{{'app.masterData.meters.type.' + meter.type | translate}}</div>
    

    I don't know why i didn't use string concatenation in the first place.