question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce a way to toggle header locale detection

See original GitHub issue

Hi 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:open
  • Created 8 years ago
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mashpiecommented, Apr 9, 2016

well that totally depends on your express setup, call your express-router router, app, server or pony

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:

var birds = require('./birds');
...
app.use('/birds', birds);

The app will now be able to handle requests to /birds and /birds/about, as well as call the timeLog middleware function that is specific to the route.

So I Guess you need to wrap all of your routes into a

app.all('/:lang/*', function(req, res, next){...});

app.use('/:lang', require('./routes/all'))

all.js containing all deeper urls, like

app.use('/', require('./routes/index'));
app.use('/blog', require('./routes/blog'));
app.use('/category', require('./routes/blog-category'));
app.use('/tag', require('./routes/blog-tag'));
app.use('/contact', require('./routes/contact'));

resulting in effectively changing resources to

  • /en
  • /en/blog
  • etc.

I’d love to gist an example but need to quit for today…

0reactions
musabkhunaijircommented, Dec 15, 2018

you can use

app.use(function(req, res, next){ i18n.setLocale(req.query.lang); next(); });

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solved: How to Detect a User's Locale in a Web App | Phrase
In this article, we'll go through three different ways of detecting a user's locale: through the browser's navigator. language s (on the client ......
Read more >
Accept-Language - HTTP - MDN Web Docs
The Accept-Language request HTTP header indicates the natural language and locale that the client prefers. The server uses content negotiation ...
Read more >
Advanced Features: Internationalized Routing - Next.js
There are two locale handling strategies: Sub-path Routing and Domain Routing. ... detect which locale the user prefers based on the Accept-Language header...
Read more >
HTTP headers | Accept-Language - GeeksforGeeks
In a few cases users can change the languages manually otherwise server detects the supported language by the browser's language.
Read more >
How to change the locale in chrome browser - Stack Overflow
On the left, you should see a list of languages. Use mouse to drag the language you want to the top, that will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found