我正在寻找使用hogan.js在浏览器中创建 html 表单模板。我读到 hogan 支持 i18n,但我找不到其工作原理的示例。你如何将翻译后的文本传递给 hogan 以及你在模板中放置什么标签,我已经看到了 {{_i}} 和 {{i18n}}?
看来我很困惑旧叉子 of 小胡子.js来自推特,与Hogan来自 Twitter 的单独的 Mustache 编译器。该叉子确实支持{{_i}}
国际化标签。然后,这将调用一个名为的全局函数_
您在其中提供自己的方法来查找翻译值。例如。
translatedStrings = {
name: "Nom";
}
function _(i18nKey) {
return translatedStrings[i18nKey];
}
var template = "{{_i}}Name{{/i}}: {{username}}",
context = {username: "Jean Luc"};
Mustache.to_html(template, context);
将返回“Nom:Jean Luc”。而 Hogan 国际化是通过普通的 Mustache lambda 实现的,例如:
translatedStrings = {
name: "Nom";
}
var template = "{{#i18n}}Name{{/i18n}}: {{username}}",
context = {
username: "Jean Luc",
i18n: function (i18nKey) {return translatedStrings[i18nKey];}
};
Hogan.compile(template).render(context);
See http://mustache.github.com/mustache.5.html有关提供 lambda 的更多信息。因此,主要区别在于,在使用 Hogan 渲染时,必须始终在上下文中提供查找翻译的函数,而 Mustache fork 将查找全局方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)