3 var env = require('gitter-web-env');
4 var errorReporter = env.errorReporter;
5 var config = env.config;
6 var I18n = require('i18n-2');
7 var i18nFactory = require('gitter-web-i18n');
9 var homepagePath = config.get('web:homeurl');
11 function getI18n(req) {
12 if (req.path === homepagePath) {
13 return i18nFactory.getHomePage(req);
16 return i18nFactory.get(req);
18 module.exports = function(req, res, next) {
19 const i18n = (req.i18n = getI18n(req));
20 const lang = (i18n && i18n.locale) || 'en';
22 I18n.registerMethods(res.locals, req);
23 // Wrap the i18n function so that when it fails,
24 // the page is still rendered with some text even if it may be templated
25 res.locals['__'] = function(...args) {
27 return i18n.__(...args);
29 errorReporter(err, { lang }, { module: 'i18n-middleware' });
35 if (i18n && i18n.prefLocale) {
36 req.i18n.setLocale(i18n.prefLocale);
38 i18n.setLocaleFromQuery(req);
41 res.locals.locale = i18n;
42 res.locals.lang = lang;