Change i18n settings per request

17 views
Skip to first unread message

Arnau Vazquez

unread,
Jun 3, 2015, 1:20:47 PM6/3/15
to apostr...@googlegroups.com
Hi there,

first of all thanks for building this great, easy-to-use CMS. I have picked it for the blogging and CMS needs at my company and I've got the basic setup working.
I tried to use an individual installation (via docker) for each of the 3 languages I need to support, but this used quite a lot of memory, so I am now back to a single install with different content trees per language (say /es/blog, /it/blog…).
I have not succeed then in setting the i18n locale depending on the path which is requested. I tried with a middleware function but I don't seem to have access to setLocale or getLocale as in the npm module docs.
I would like something similar to what John Resig describes here

app.use(function(req, res, next) {
        req
.i18n = new i18n(/* options... */);
       
next();
});

And that this is applied in the nunjucks templates.

I don't want to rely on cookie only because first-time visitors will not have it yet.

Does anyone have an idea on how to achieve this?

Thanks,
Arnau

Arnau Vazquez

unread,
Jun 4, 2015, 11:48:45 AM6/4/15
to apostr...@googlegroups.com
Well, I implemented a (probably dirty) workaround, based on Fotis' post.

In app.js afterInit I tie a new instance of i18n to what I guess is express app:

       var i18n = require('i18n');
       i18n
.configure(site.options.i18n);
       site
.apos.app.use(i18n.init);

this gives me res.getLocale( ) and res.setLocale( ) that I was looking for.

And then in the middleware array:

function (site, req, res, next) {
            var pathLocale = false;

            // extract pathLocale from req.path if it starts with a lang code

            if (pathLocale) {
                res.setLocale(pathLocale);
            }

            next();
        }

Thanks!
Arnau
Reply all
Reply to author
Forward
0 new messages