Skip to content

Instantly share code, notes, and snippets.

@jamuhl
Created January 13, 2016 07:35
Show Gist options
  • Save jamuhl/f8020960024b4d09f721 to your computer and use it in GitHub Desktop.
Save jamuhl/f8020960024b4d09f721 to your computer and use it in GitHub Desktop.
i18next - multiple express apps
var i18next = require('i18next');
var middleware = require('i18next-express-middleware');
var Backend = require('i18next-node-fs-backend');
var express = require('express');
i18next
.use(Backend)
.init({
// use correct configuration options...look up docs!
backend: {
loadPath: __dirname + '/locales/{{lng}}/{{ns}}.json'
}
});
// pseudo virtual hosts
var vhosts = [
{ lng: 'de', port: 3000 },
{ lng: 'en', port: 3001 }
];
vhosts.forEach(function(host) {
var app = express();
// set req.lng to defined lng in vhost
// see for details:
// https://github.com/i18next/i18next-express-middleware/blob/master/src/index.js#L14
app.use(function(req, res, next) {
req.lng = host.lng;
next();
});
// use the middleware to do the magic
// create a fixed t function for req.lng
// no clones needed as they just would do the same (sharing all but lng)
app.use(middleware.handle(i18next));
// in your request handler
app.get('/', function(req, res) {
var lng = req.language;
var lngs = req.languages;
console.log('language is: ', lng, lngs);
res.send(req.t('key1'));
});
app.listen(host.port);
});
// go to localhost:3000 ---> de
// go to localhost:3001 ---> en
// unclear to me...why you need an array of express apps to archive this.
// one app with custom language detector function detection lng from domainName
// would be enought to detect lng and translate content appropriate
@uroblesmellin
Copy link

Hi Jan,

I get this error just by placing this line before as you mentioned:

// change the order - first the middleware to get req.t
app.use(middleware.handle(i18next));

If I do that before :
app.use(function(req, res, next) {
// ...
});

I get:

TypeError: Cannot read property 'indexOf' of undefined
at e.a.value (/Users/uroblesmellin/projects/bolt20-frontend/bolt-2dot0-frontend/node_modules/i18next/bin/index.js:1:13811)
at t.u.value (/Users/uroblesmellin/projects/bolt20-frontend/bolt-2dot0-frontend/node_modules/i18next/bin/index.js:2:8308)
at /Users/uroblesmellin/projects/bolt20-frontend/bolt-2dot0-frontend/node_modules/i18next-express-middleware/lib/index.js:82:40
at Layer.handle as handle_request
at trim_prefix (/Users/uroblesmellin/projects/bolt20-frontend/bolt-2dot0-frontend/node_modules/express/lib/router/index.js:312:13)
at /Users/uroblesmellin/projects/bolt20-frontend/bolt-2dot0-frontend/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/Users/uroblesmellin/projects/bolt20-frontend/bolt-2dot0-frontend/node_modules/express/lib/router/index.js:330:12)
at next (/Users/uroblesmellin/projects/bolt20-frontend/bolt-2dot0-frontend/node_modules/express/lib/router/index.js:271:10)
at /Users/uroblesmellin/projects/bolt20-frontend/bolt-2dot0-frontend/server/middlewares/write-header.js:9:9
at Layer.handle as handle_request

If I do it in the original way (without changing the order of

app.use(middleware.handle(i18next));

I don't get that error.

Thanks,
Ulises

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment