在我的客户控制器中,我定义了一些用于填充选择输入(下拉)选项的常量数组。
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"}
]
});
普通的解决方案是使用translationMacro函数t()或this.get('i18n')。t()围绕翻译键,但是它们不能用于这种情况,因为“this”里面的对象或数组不会指控制器。
解决此类情况的最佳做法是什么?
答案 0 :(得分:6)
您可以将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()
});