【问题标题】:i18n with Marionette.jsi18n 与 Marionette.js
【发布时间】:2016-03-07 22:35:24
【问题描述】:
我已经下载了 require.js i18n 插件来对视图进行一些翻译,它们使用把手模板,但 html 标记之间的字符串没有用花括号括起来。
模板是使用 hbs 加载的!
我是一个初学者,经过深思熟虑,我的任何尝试都取得了丰硕的成果。
我的视图如何呈现翻译后的字符串?
非常感谢。
// my View
View = Marionette.ItemView.extend({
template: theTmpl,
//some events and logic
initialize: function() {
}
});
【问题讨论】:
标签:
backbone.js
marionette
【解决方案1】:
要获得翻译后的字符串,您可以使用custom Handlebar helpers like
Handlebars.registerHelper('$', function() {
var args = Array.prototype.slice.apply(arguments),
options = args.pop();
// If multiple params are present then use sprintf
if (args.length > 1) {
return i18next.translate.apply(null, args);
}
// Otherwise pass in any hash params
return i18next.translate(args[0], options.hash);
});
在 hbs 模板中你可以这样写
{{$ 'Hello world'}}
如果您已将Hello World 映射到Bonjour le monde,那么您将在浏览器中看到Bonjour le monde。