Add a middleware for i18n

parent 195da958
Pipeline #1474 passed with stage
in 41 seconds
......@@ -7,12 +7,19 @@ const Canvas = require('canvas');
const Image = Canvas.Image;
const app = express();
const config = require('./config.json');
const i18n = require('./i18n.js')();
const i18n = require('./i18n.js');
app.set('x-powered-by', false);
app.set('view engine', 'pug');
app.use(express.static('./static'));
app.use(function(req, res, next) {
let locale = req.headers['accept-language'] || '';
req.i18n = new i18n();
req.i18n.setLocale(locale.indexOf('en') !== -1 && locale.indexOf('en') < locale.indexOf('fr') ? 'en' : 'fr');
next();
});
// {name: filepath}
let fonts = {
'FiraSans': 'FiraSans-Regular.otf',
......@@ -65,9 +72,7 @@ function randomize(req, _) {
}
app.get('/generate.png', function (req, res) {
let locale = req.headers['accept-language'] || '';
i18n.setLocale(locale.indexOf('en') !== -1 && locale.indexOf('en') < locale.indexOf('fr') ? 'en' : 'fr');
_: i18n._;
let _ = req.i18n._;
if (req.query.target === 'facebook') {
return res.redirect('https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(buildUrl(req)));
......@@ -155,13 +160,11 @@ app.get('/generate.png', function (req, res) {
});
app.get('/', function(req, res) {
let locale = req.headers['accept-language'] || '';
i18n.setLocale(locale.indexOf('en') !== -1 && locale.indexOf('en') < locale.indexOf('fr') ? 'en' : 'fr');
res.render('index', {protocol: req.protocol,
host: req.headers.host,
imageParams: req._parsedUrl.query,
subdir: config.subdir,
_: i18n._});
_: req.i18n._});
})
app.listen(process.env.PORT || config.port || 80);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment