javascript - Express.js multilanguage with i18n-node -
how call controller express.js routing?
app.get('*', function(req, res, next) { var regexp = /^\/([a-z]{2})(\/|$)/gi, exec = regexp.exec(req.url); exec = exec != null ? exec[1] : undefined; if(exec == undefined) { // add language prefix link } else { i18n.setlocale(exec); // add language prefix link } next(); });
if open page /about need url this: http://example.com/en/about. how can , how display call 'about' controller?
app.get('/about', function(req, res) { console.log('here about'); res.send('hello, world'); });
not suitable: app.get('(en|de|ru)/about', ...)
thanks in advance.
you like
app.get('/:language/about', function(req, res) { var language = req.params.language; i18n.setlocale(language); });
for multiple routes maybe this
controllers.js
module.exports = { about: function(req, res) { res.send('about'); } }
app.js
var controllers = require('./controllers'); app.all('/:language/:controller', function(req, res) { i18n.setlocale(req.params.language); controllers[req.params.controller](req, res); });
Comments
Post a Comment