mustache/hogan i18n and taking word-order into account

Go To StackoverFlow.com

1

Found a couple of questions (and answers) on this: How is internationalization configured for Hogan.js? ,etc.

but non in particular that take word order into account. I need the ability to:

  • step 1. given a key -> lookup a sentence in a particular language.

  • step 2. this sentence may contain {{var}} , which need to be
    substituted by json-values.

step 2. alone is general mustache-templating.

step 1. alone could be done with several techniques, but I prefer techniques that don't involve any specialized code outside of the Mustache/Hogan engine (in combination with a i18n-resource bundle of course) . Hogan seems to support this with something like: (from url above)

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

However to combine 1. and 2. in this example I would want translatedStrings[i18nKey] to return a string which potentially contains {{<some expansion>}} as well.

Someone knows of an elegant way to do this?

Rationale: Often languages differ a lot in word order, etc. which makes for complex templates without this ability.

2012-04-04 19:50
by Geert-Jan


4

The latest version of Hogan.js will handle Mustache tags inside the result returned from a lambda. One minor change to the code in your question however, is that the result of the lambda should be a function in order to modify the string:

var translatedStrings = { name: "Nom {{rank}}" };

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

document.write(Hogan.compile(template).render(context));​ // Nom Captain: Jean Luc

I created a jsfiddle that demonstrates this with the latest version.

2012-04-18 22:13
by Phuong LeCong
Ads