I have an express static site app. I calling my site's translation with a cookie in app.js:
// i18n
app.get('/hu', function (req, res) { // http://127.0.0.1:3000/hu
res.cookie('locale', 'hu', { maxAge: 900000, httpOnly: true });
res.redirect('back');
});
app.get('/en', function (req, res) { // http://127.0.0.1:3000/en
res.cookie('locale', 'en', { maxAge: 900000, httpOnly: true });
res.redirect('back');
});
If someone visiting http://127.0.0.1:3000/en
URL, than it will store a cookie which calling the translation. hu
is the default language, when someone visiting my site at the first time, there's no any cookie stored.
But how can I add a CSS class to my site when the english translation is active? I have a navigation bar with my logo in the middle which is centered horizontally. On english language, the words length different, causing even the centered flexbox elements left-nav | logo | right-nav
slightly not be in the center.
Somehow I want to add a CSS class into my handlebar template from app.js (which is on the server side) when the specific cookie is presented. Is it possible?
Is it possible to solve this from app.js globally, not from the routers?
Final solution, thank you for @t.niese!
In app.js:
app.use(function(req, res, next) {
var defaultLang = 'hu';
var activeLang = req.cookies.locale || defaultLang;
res.locals.langClass = activeLang + '-' + activeLang.toUpperCase();
next();
});
In my handlebars template:
<!doctype html>
<html class="no-js lang-{{langClass}}" lang="{{langClass}}">