把手帮助i18n

时间:2014-05-12 03:44:00

标签: ember.js internationalization ember-i18n

如果我使用Ember.js内置助手input

{{input value=query class="form-input" placeholder="Search"}}

我想替换占位符字符串"搜索"用该字符串的翻译版本。

通常,如果我还没有使用input帮助器,我会像这样访问翻译后的字符串:

{{ t "home.search" }}

3 个答案:

答案 0 :(得分:8)

使用Subexpressions计算出来:http://handlebarsjs.com/expressions.html

{{input value=query class="form-input" placeholder=(t "home.search") }}

答案 1 :(得分:2)

我无法让dylanjha的解决方案起作用,因为Ember.I18n的助手会返回一个包裹翻译文本的跨度,而不是翻译文本本身。我通过编写自己的帮助程序直接调用翻译来解决这个问题:

Ember.Handlebars.registerHelper('ts', function (key) {
  return Em.I18n.t(key);
});

然后我能够像这样使用我的助手:

{{input value=login placeholder=(ts 'signin.login') }}

答案 2 :(得分:0)

我在我的应用中使用它:

// This is a custom text field that allows i18n placeholders
App.TextField = Ember.TextField.extend({
    attributeBindings: ['autofocus', 'z-index', 'autocomplete'],

    placeholder: function () {
        if (this.get('placeholderKey')) {
            return I18n.t(this.get('placeholderKey'));
        } else {
            return ''; //or this.get('placeholder')
        }
    }.property('placeholderKey')
});
相关问题