我想在客户端使用 Handlebars 和 Ruby 的 i18n-js gem(在 Rails 3 应用程序上)翻译我的 i18n 键。在您看来,这样的 Handlebars 助手可能是什么?
根据当前的 Handlebars 版本,默认帮助器如下所示:
Handlebars.registerHelper('if', function(context, options) {
var type = toString.call(context);
if(type === functionType) { context = context.call(this); }
if(!context || Handlebars.Utils.isEmpty(context)) {
return options.inverse(this);
} else {
return options.fn(this);
}
});
Handlebars.registerHelper('unless', function(context, options) {
var fn = options.fn, inverse = options.inverse;
options.fn = inverse;
options.inverse = fn;
return Handlebars.helpers['if'].call(this, context, options);
});
Handlebars.registerHelper('with', function(context, options) {
return options.fn(context);
});
Handlebars.registerHelper('log', function(context) {
Handlebars.log(context);
});
关于i18n-js gem,这似乎是一个很好的组合。例如,这个库在 Ember.js 中使用(如 ember-i18n)。是否已经有关于 Handlebars.js 和 i18n 的最佳实践?
感谢您的任何建议。
最佳答案
我用 i18n-js 写了一个 handlebars 助手。这应该非常适合您的需求: https://stackoverflow.com/a/8796912/499700
关于ruby-on-rails - Handlebars.js & i18n-js,新助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8756256/