www: Add i18n /en|fr/ urls

This commit is contained in:
Samuel Ortion 2022-08-28 09:48:24 +02:00
parent 585e578e48
commit 4bfa6cfe16
3 changed files with 35 additions and 18 deletions

44
app.js
View File

@ -24,14 +24,14 @@ app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public')));
const sess = { const sess = {
secret: 'keyboard cat', secret: 'keyboard cat',
resave: true, resave: true,
saveUninitialized: true saveUninitialized: true
} }
if (app.get('env') === 'production') { if (app.get('env') === 'production') {
app.set('trust proxy', 1); // trust first proxy app.set('trust proxy', 1); // trust first proxy
sess.cookie.secure = true; // serve secure cookies sess.cookie.secure = true; // serve secure cookies
} }
app.use(session(sess)); app.use(session(sess));
@ -42,17 +42,31 @@ i18n.expressBind(app, {
cookieName: 'locale' cookieName: 'locale'
}); });
app.use('/dist/leaflet', express.static('node_modules/leaflet/dist')); app.use(function (req, res, next) {
app.use('/dist/feather', express.static('node_modules/feather-icons/dist'));
app.use('/dist/axios', express.static('node_modules/axios/dist'));
app.use(function(req, res, next) {
req.i18n.setLocaleFromQuery(); req.i18n.setLocaleFromQuery();
req.i18n.setLocaleFromCookie(); req.i18n.setLocaleFromCookie();
next(); next();
}); });
app.use(function(req, res, next) { app.all('*', function(req, res, next) {
// set locale
var rxLocale = /^\/(fr|en)/i;
if(rxLocale.test(req.url)){
const arr = rxLocale.exec(req.url);
const locale=arr[1];
req.i18n.setLocale(locale);
} else {
req.i18n.setLocale('en');
}
// add extra logic
next();
});
app.use('/dist/leaflet', express.static('node_modules/leaflet/dist'));
app.use('/dist/feather', express.static('node_modules/feather-icons/dist'));
app.use('/dist/axios', express.static('node_modules/axios/dist'));
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', 'http://localhost:3000'); res.header('Access-Control-Allow-Origin', 'http://localhost:3000');
res.header( res.header(
'Access-Control-Allow-Headers', 'Access-Control-Allow-Headers',
@ -65,16 +79,16 @@ app.use('/api/0', apiRouter);
const csrfProtection = csrf({ cookie: true }); const csrfProtection = csrf({ cookie: true });
app.use(csrfProtection); app.use(csrfProtection);
app.use('/', indexRouter); app.use(/\/(fr|en)/i, indexRouter);
app.use('/auth', authRouter); app.use(/\/(fr|en)\/auth/i, authRouter);
// catch 404 and forward to error handler // catch 404 and forward to error handler
app.use(function(req, res, next) { app.use(function (req, res, next) {
next(createError(404)); next(createError(404));
}); });
// error handler // error handler
app.use(function(err, req, res, next) { app.use(function (err, req, res, next) {
// set locals, only providing error in development // set locals, only providing error in development
res.locals.message = err.message; res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {}; res.locals.error = req.app.get('env') === 'development' ? err : {};

View File

@ -1 +1,4 @@
{} {
"Game": "Game",
"About": "About"
}

View File

@ -1,4 +1,4 @@
{ {
"Home": "Home", "Home": "Inicio",
"About": "About" "About": "Acerca de",
} }