简体   繁体   English

如何用Mustache.js做高级i18n?

[英]How to do advanced i18n with Mustache.js?

It seems Twitter is using a fork of Mustache.js to provide i18n to its templates? 看来Twitter正在使用Mustache.js分支为其模板提供i18n?

Could someone give a brief example of how this is done and perhaps also outline what semantics is necessary to crowdsource these translations? 有人可以给出一个简单的例子说明这是如何完成的,也许还概述了为这些翻译众包所需的语义?

There is of course this simple example: 当然有这个简单的例子:

var template = "{{_i}}{{name}} is using mustache.js!{{/i}}"

var view = {
  name: "Matt"
};

var translationTable = {
  // Welsh, according to Google Translate
  "{{name}} is using mustache.js!": "Mae {{name}} yn defnyddio mustache.js!"
};

function _(text) {
  return translationTable[text] || text;
}

alert(Mustache.to_html(template, view));
// alerts "Mae Matt yn defnyddio mustache.js!"

But I'd like some more insight on how to structure the _(text) function and translationTable to provide conditionals, singular, plural etc. Examples of solving more advanced use cases would be much appreciated. 但我想更深入地了解如何构造_(文本)函数和translationTable以提供条件,单数,复数等。非常感谢解决更高级用例的示例。

I know i'm not answering your question really, but unless you are planning on spending a lot of time on this project I would seriously consider just leaving this as a data issue. 我知道我并没有真正回答你的问题,但除非你计划在这个项目上花费大量时间,否则我会认真考虑将此作为数据问题。

{
    title : {
        key: 'título',
        value: 'bienvenida'
    }
}

And: 和:

{
    title : {
        key: 'لقب',
        value: 'ترحيب'
    }
}

Then just make the template generic: 然后只需使模板通用:

<h1>{{title.key}}: {{title.value}}</h1>

And: 和:

<h1>{{title.value}} {{title.key}}</h1>

All you need to maintain is a 1:1 mapping between templates and data. 您需要维护的只是模板和数据之间的1:1映射。

Mustache.render(data[language], template[language]);

Keep it simple :) 把事情简单化 :)

Structuring the more advanced cases including conditionals, loops and so on are done in the exact same way as with the regular Mustache library. 构建包括条件,循环等在内的更高级的案例的方式与常规的Mustache库完全相同。 You can use the new I18N {{_i}} start and {{/i}} end tags to wrap parts of your template for translation purposes. 您可以使用新的I18N {{_ i}}开始和{{/ i}}结束标记来包装模板的部分内容以进行翻译。

If you template is: 如果您的模板是:

<h1>Title: {{title}}</h1>
<ul>
   {{#a_list}}
      <li>{{label}}</li>
   {{/a_list}}
</ul>

you can just wrap the first line 你可以包装第一行

<h1>{{_i}}Title: {{title}}{{/i}}</h1>

and include the inner part in the translation map. 并在翻译地图中包含内部部分。

See http://jsfiddle.net/ZsqYG/2/ for a complete example. 有关完整示例,请参见http://jsfiddle.net/ZsqYG/2/

I believe what you want to do is to use i18n features with Mustache. 我相信你想要做的是使用Mustache的i18n功能。 This can be achieved by overloading the Mustache.render method as follow: 这可以通过重载Mustache.render方法来实现,如下所示:

var lang = {
    'is_using_pre': 'Mae ',
    'is_using': 'yn defnyddio'
};

var Mustache = (function (Mustache) {
    var _render = Mustache.render;

    Mustache.render = function (template, view, partials) {
        view['lang'] = lang;
        return _render (template, view, partials);
    };

    return Mustache;
}(Mustache));

var template = "{{_i}}{{lang.is_using_pre}}{{name}} {{lang.is_using}} mustache.js!{{/i}}";
var view = {
  name: "Matt"
};

alert(Mustache.to_html(template, view));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM