本文介绍了如何为Hogan.js配置国际化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望使用在浏览器中创建html格式的模板。我读过hogan支持i18n,但我找不到一个如何工作的例子。你如何将翻译过的文本传递给hogan以及你在模板中添加了什么标记,我看到了{{_i}}和{{i18n}}?

I'm looking to use hogan.js to create html form a template in the browser. I've read that hogan supports i18n, but I can't find an example of how this works. How do you pass the translated text to hogan and what tag do you put in the template, I have seen both {{_i}} and {{i18n}}?

推荐答案

似乎我混淆了来自Twitter的href =https://github.com/janl/mustache.js =nofollow> Mustache.js ,来自twitter的独立胡子编译器。 fork确实支持 {{_ i}} 标记以进行国际化。然后,它将调用一个名为 _ 的全局函数,在该函数中,您可以使用自己的方法查找已翻译的值。例如,

It would seem I was confusing an older fork of Mustache.js from Twitter, with Hogan a separate mustache compiler from also from twitter. The fork does support an {{_i}} tag for internationalization. This will then call a global function with the name _ in which you provide you're own method for looking up the translated value. E.g.

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国际化是通过普通的小胡子lambdas实现的,例如:

Would return "Nom: Jean Luc". Whereas with Hogan internationalization is achieved with normal mustache lambdas, e.g.:

translatedStrings = {
   name: "Nom";
}

var template = "{{#i18n}}Name{{/i18n}}: {{username}}",
    context = {
       username: "Jean Luc",
       i18n: function (i18nKey) {return translatedStrings[i18nKey];}
    };

Hogan.compile(template).render(context);

参见了解有关提供lambda的更多信息。因此,主要的区别在于,在使用Hogan进行渲染时,必须始终在上下文中提供查找翻译的函数,而小胡子叉将查找全局方法。

See http://mustache.github.com/mustache.5.html for more on providing lambdas. So the main distinction is that the function to look up translations must always be provided on the context when rendering with Hogan, whereas the mustache fork will look up a global method.

这篇关于如何为Hogan.js配置国际化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 17:39