How to translate controller member arrays in Ember-i18n?

496 Views Asked by At

In my customer controller I have defined some constant arrays that are used for populating select input (dropdown) options.

import Ember from 'ember';

export default Ember.Controller.extend({
    occupations: [
        {code: 'student', name: "Student"},
        {code: 'worker', name: "Worker"},
        {code: 'retired', name: "Retired"},
        {code: 'other', name: "Other"}
    ]
});

Normal solution would be using translationMacro function t() or this.get('i18n').t() around translation key, but they can't be used in such situation as "this" inside object or array will not refer to controller.

What is best practice for solving such situation?

1

There are 1 best solutions below

1
dwickern On BEST ANSWER

You can make occupations a property:

import Ember from 'ember';

export default Ember.Controller.extend({
    i18n: Ember.inject.service(),

    occupations: function() {
        var i18n = this.get('i18n');
        return [
            {code: 'student', name: i18n.t('occupations.student') },
            {code: 'worker', name: "Worker"},
            {code: 'retired', name: "Retired"},
            {code: 'other', name: "Other"}
        ];
    }.property()
});