简体   繁体   English

如何在Ember-i18n中转换控制器成员数组?

[英]How to translate controller member arrays in Ember-i18n?

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. 正常的解决方案是在翻译键周围使用translationMacro函数t()或this.get('i18n')。t(),但不能在对象或数组中的“ this”不引用控制器的情况下使用它们。

What is best practice for solving such situation? 解决这种情况的最佳实践是什么?

You can make occupations a property: 您可以使occupations成为财产:

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()
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM