Introduce a way to toggle header locale detection
See original GitHub issueHi there, I’ve recently started using your module and it just worked out of the box, which is amazing!
I just have one basic problem which drives me crazy. The init
function uses headers as last resort to find user’s locale. Which, used alone, is not quite safe and somehow makes defaultLocale
meaningless.
This is my setup:
i18n.configure({
locales: ['bg', 'en'],
defaultLocale: 'bg',
directory: path.join(config.root, '/locales'),
queryParameter: 'lang',
autoReload: false, // watch for changes in json files to reload locale on updates - defaults to false
updateFiles: true // whether to write new locale information to disk - defaults to true
});
And let’s say I use this schema for switching languages:
example.com // I hope it's using the default locale
example.com?lang=en // should be using English
But then I understand that my browser sends Accept-Language:en-US,en;q=0.8,bg;q=0.6,fr;q=0.4
, so my defaultLocale
is just skipped and I get the page in English, despite NOT providing a language parameter.
I think there should be a way to disable header check, just like there is a way to disable both query
and cookie
lookup (by not providing parameters for them). Relying on user settings in order to get specific language alone seems like very dangerous game, and it’s a highly recommended no-no.
I think there could be a parameter if headers check should be done, set to true
by default 😉 For now, I’m shamelessly short-term fixing my issue with:
app.use(function(req, res, next) {
delete req.headers['accept-language'];
next();
});
Issue Analytics
- State:
- Created 8 years ago
- Comments:15 (6 by maintainers)
Top GitHub Comments
well that totally depends on your express setup, call your express-router
router
,app
,server
orpony
…your 404 will most probably result from unproper nesting of routes. Please refer to http://expressjs.com/en/guide/routing.html esp the very last section:
So I Guess you need to wrap all of your routes into a
all.js
containing all deeper urls, likeresulting in effectively changing resources to
I’d love to gist an example but need to quit for today…
you can use
app.use(function(req, res, next){ i18n.setLocale(req.query.lang); next(); });