I am building an Express app for personal use but I want it to support multiple languages. I'm using the i18-node for english (en) and spanish (es) and had written two routes, as follow:
function setLanguage (req, res, next) {
const locales = i18n.getLocales()
const { lang } = req.params
if (locales.includes(lang)) {
req.setLocale(lang)
return next()
}
return next(new Error('Page not found'))
}
app.get('/', setLanguage, (req, res) => {
// Renders the home with 'es' as default locale
res.render('index')
})
app.get('/:lang', setLanguage, (req, res) => {
// Renders the same view with 'es' or 'en' locale
res.render('index')
})
If I visit localhost:3000/es
or localhost:3000/en
it works. If I visit localhost:3000/thisdoesnotexist
it shows me the not found page, but if i visit localhost:3000/
it also shows me the not found.
So the question is, how can I set a multilanguage app with a custom not found page?