如何使用Handlebars.js(小胡子模板)制作i18n? [英] How to make i18n with Handlebars.js (mustache templates)?

查看:93
本文介绍了如何使用Handlebars.js(小胡子模板)制作i18n?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Handlebars.js(与Backbone和jQuery相关)来制作几乎完全呈现客户端的网络应用程序,并且我遇到了此应用程序国际化的问题。



我该如何做这项工作?



有没有插件?

解决方案

我知道这已被回答,但我想分享我的简单解决方案。为了在Gazler的解决方案上使用I18n.js(我们在工作中使用该工具),我只使用了一个非常简单的Handlebars帮助程序,以方便进行本地化:



Handler

  Handlebars.registerHelper('I18n',
function (str){
return(I18n!= undefined?I18n.t(str):str);
}
);

模板

 < script id =my_templatetype =x-handlebars-template> 
< div> {{I18n myVar}}< / div>
< / script>

这样做的主要优点是对整个json对象没有昂贵的前/后处理。更不用说如果传入的json嵌套了对象/数组,那么寻找并解析它们所花费的时间可能会变得非常昂贵,如果对象很大的话。



干杯! p>

I'm currently using Handlebars.js (associated with Backbone and jQuery) to make a web app almost totally client side rendered, and I'm having issues with the internationalisation of this app.

How can I make this work?

Are there any plugins?

解决方案

I know this has been answered, but I'd like to share my simple solution. To build on Gazler's solution using I18n.js (which we use with our project at work), I just used a very simple Handlebars helper to facilitate the process to do the localization on the fly:

Handler

Handlebars.registerHelper('I18n',
  function(str){
    return (I18n != undefined ? I18n.t(str) : str);
  }
);

Template

<script id="my_template" type="x-handlebars-template">
    <div>{{I18n myVar}}</div>
</script>

The primary advantage of this is that there's no expensive pre/post processing on the entire json object. Not to mention if the incoming json has nested objects/arrays, the time spent looking for and parsing for them might get expensive if the object is huge.

Cheers!

这篇关于如何使用Handlebars.js(小胡子模板)制作i18n?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆