如何处理霍根的多元化
How to handle pluralization in Hogan
我使用的是Hogan.js,它与Mustache规范兼容。我在实现多元化的坚实道路上遇到了困难。我想继续使用霍根http://i18next.com/用于i18n处理
这样做适用于的简单情况
tpl:
{{#plural(count)}}
I have {{count}} apples!
{{/plural(count)}}
数据:
{
count: 2,
'plural(count)': function () {
return function () {
return _t[arguments[0].trim()][this['count']]
}
}
}
这需要在单独的步骤中解析/扫描/呈现,以便能够生成所有所需的多个方法(复数(key.val)等),但没关系,只需要在服务器启动时完成一次。
这会破坏之类的东西
{{#multiple(key.nested)}}
如果数据看起来像
{
'plural(key': {
'val)': ...
}
}
这也需要我从上下文手动查找值,这不是一个大问题,但在某些情况下,lambda的/partials可能无法解决
对于默认的转换映射,事情要复杂得多,而且处理
Ok找到了我认为最好的方法来处理这个问题:
var tpl_data = fs.readFileSync('./tpl/test.hjs', 'utf8');
var scan = Hogan.scan(tpl_data);
var tree = Hogan.parse(scan);
var gen = Hogan.generate(tree, tpl_data, {asString:false});
var out = gen.render(data);
更改树,将所有tag
密钥替换为i18n
其中n
与您的图案/i18n .+/
匹配
我使用{{#i18n {count: count, text: 'I have <%count%> apples!'} }}
等为i18next添加选项所以我匹配所有从i18n
开始的n
将i18n
添加到Hogan.codegen
Hogan.codegen.i18n = function (node, context) {
context.code += 't.b(t.v(t.i18n("' + esc(node.n) + '",c,p,0)));';
}
将i18n
方法添加到Hogan.Template 的原型中
Hogan.Template.prototype.i18n = function (key, ctx, partials, returnFound) {
//here the ctx is an array with from right to left the render scopes
// most right is the most inner scope, most left is the full render data
//1. get the config from the key,
//2. get the values out of the scope
//3. get the values for the translation
//4. lookup the translation, and repleace the values in the translation
//5. return the translated string
};
注意,在Hogan.Template.prototype.i18n
中,您可以访问模板的所有方法
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- keyup事件处理程序更改焦点不适用于快速键入
- 如何使用jquery处理php循环通过元素
- angular.js没有'无法在PhoneGap中处理视图标记
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- 提示使用服务器端事件处理程序激活JavaScript
- javascript:如何在antlr生成的Lexer中进行错误处理
- 如何编写一个具有公共标头的批处理
- 在同一个服务工作者中处理service-worker.js有任何影响吗
- 如何处理node.js节点mongodb中的连接和查询队列
- 通过命令行/批处理文件打开页面时,将javascript代码注入Google Chrome
- 如何处理10页以上的静态页眉/页脚
- 将事件处理程序绑定到任何可能的事件
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- python到“;流“;字典处理
- 使用javascript进行实时图像处理
- 导入jQuery脚本获胜'我不处理html文件
- Javascript无法处理表单提交
- 在承诺链中处理早期回报的最佳方式
- 如何处理霍根的多元化