Reputation: 944
I'm building my first multi-language application. Successfully detecting the language via Middelware and attaching it to req.lang. As a next step, is there any way to also localize the route-paths? This would be very useful for SEO.
What is the ususal approach here?
const paths = {
en: {
index: 'home',
imprint: 'imprint'
},
de: {
index: 'start',
imprint: 'impressum'
},
nl: {
index: 'stchartje',
imprint: 'imprintjn'
}
}
router.get('/'+paths[req.lang].imprint, function(req, res, next) {
res.render('index', { title: content[req.lang].IMPRINT.HEADLINE });
})
Thats what I came up with for now, it is not working though, because 'req' is not evailable in the route-definition.
Upvotes: 0
Views: 47
Reputation: 29172
You can use params
:
router.get('/:slug', function(req, res, next) {
if (req.params.slug === paths[req.lang].imprint) {
res.render('index', { title: content[req.lang].IMPRINT.HEADLINE });
} else {
next();
}
})
Upvotes: 1