I'm using Hogan.js, which is compatible with the Mustache spec. And im having trouble implementing a solid way of doing pluralization. I would like to keep using Hogan and use http://i18next.com/ for i18n handling
doing something like this works for the simple cases
tpl:
{{#plural(count)}}
I have {{count}} apples!
{{/plural(count)}}
data:
{
count: 2,
'plural(count)': function () {
return function () {
return _t[arguments[0].trim()][this['count']]
}
}
}
this requires parsing/scanning/rendering in seperate steps to be able to generate all of the required plural methods (plural(key.val) etc.) but thats fine, it only needs to be done once, at server boot.
this breaks on things like
{{#plural(key.nested)}}
that would match if the data looked like
{
'plural(key': {
'val)': ...
}
}
this also requires me to manually lookup the values from the context, not a major problem but there are some cases with lambda's/partials that might be impossible to resolve
for the default translation mappings, thing are a lot less complex, and thats easy to handle
Ok found the way I think is best to handle this problem:
alter the tree, replacing all
tag
keys toi18n
where then
matches your pattern/i18n .+/
I use
{{#i18n {count: count, text: 'I have <%count%> apples!'} }}
and the like to add the options for i18next so i match alln
's starting withi18n
add the
i18n
to Hogan.codegenadd the
i18n
method to the prototype of Hogan.Templatenote that inside the
Hogan.Template.prototype.i18n
you can access all of the template's methods