I am trying to create an i18n module for my application. Since I use swig, I would prefer to create a custom "trans" tag so that the translation result can be cached.
I use Express.js 4 as the base for my application.
How can I read a variable from a request inside a user tag or filter? I have no idea how to read them (especially inside filters). I can’t even access the variables that I pass into the template. For instance:
swig template index.swig
-------------------------------
{% trans %}this is sparta{% endtrans %}
{% trans "fr" %}this is sparta{% endtrans %}
-------------------------------
I can do the work of line 2. I pass in the locale for "trans" and in my code will replace the text passed by the user from the French translation.
But I do not want to pass the language standard to each tag. I would like to do something like this:
function (req, res, next) {
res.locals.locale = req.session.user.getLocale();
}
{% trans %}this is sparta{% endtrans %}
? swig . , , lang , swig.