gsf / whiskers.js

Whiskers templating library for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

i18n

marmarosi opened this issue · comments

It would be nice if whiskers would support multilingual templates in some way, in conjunction with i18next or i18n modules. These modules tipically register a helper function on res.locals property. However, whiskers cannot call a function.

That could be resolved introducing a new tag, that would call a fixed name function on the context. E.g.

{i18n key}

context.i18n = function(key) {
return fn(key);
}

Jozsef Marmarosi

Whiskers doesn't call functions because the templates it compiles should never throw an error. By constraining tag types, templates are also kept more readable.

Instead of sticking that i18n function into the template, pass your values into it as you prepare the context object.

I accept your arguments as a conception. Still I think that translatable texts should be placed on views. For my purposes I created a fork, and added the possibility of calling external functions: https://github.com/marmarosi/whiskers.js. Its syntax is simple:

<p>{fn(arguments)}</p>
<p>{obj_A.method_B(arg1, arg2, ...)}</p>

E.g. using i18next to translate texts:

<p>{t('engine', { name: "Whiskers", lng: "hu" })}</p>

On errors it simply returns an empty string.

Looks great, but I probably won't roll it into whiskers. As long as you're extending things to meet your needs, you might consider packaging it up as your own templating library. I've had fun working on this one.