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.

setLocale in express 4 with no cookies

See original GitHub issue

Hi I want to set the locale for every request based on the x-locale request header. So in the API middleware, I check for the x-locale and, if it exists, I do i18n.setLocale(x_locale_header_value). But just after doing that, I call i18n.getLocale() and I always get the default locale (en in this case).

This is necessary for me as I use (just once throughout the API) a handlebars template which is always translated to the same language: default locale ‘en’.

To be honest, I don’t know whether or not I am doing something wrong and/or missing something. I’ve checked the i18n.init method documentation and issues but I don’t use cookies or param language at all, so it doesn’t have effect on the API.

So, any hint is welcome 😃

Cheers!!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mashpiecommented, Jan 4, 2017

i18n.__('sting_to_translate') dispatches from global object. Doing so will lead to concurrency issues while binding to req sticks it’s locale setting to that single request only, which is the only way to ensure correct translations for each and every request handled.

So I strongly advise local usage like so:

i18n.setLocale(req, locale) // sets all of req, res and res.locals

// [...]

req.__('sting_to_translate') // or
res.__('sting_to_translate') // or
res.locals.__('sting_to_translate')
0reactions
telekosmoscommented, Jan 4, 2017

OOOOOK!! Clear explanation!!!

If everything seem to be working fine now (no so many translations performed in my API), what you say makes loads of sense. I knew I was doing something ‘no well’ altogether ☺️ .

Thanks!!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

i18n how to get the cookie information about the language
When you are calling the 'setlocale' url in that you can use: res.cookie("lang", req.params.lang);. and you can get it as: req.cookies.lang.
Read more >
express-locale - npm
It returns (only) full locale identifiers based on the middleware's configuration. Configuration defines possible sources, their order and, ...
Read more >
res.cookie() - Sails.js
Sets a cookie with name ( name ) and value ( value ) to be sent along with the ... set to true...
Read more >
Nodejs - Internationalization in Express with i18n and i18n-2
To change the language, you can set it directly using setLocale(locale) function. Beside that, you can set the cookie locale value for the ......
Read more >
NodeJS/Express - Transifex Developer Hub
Server-side applications with Javascript and Express JS. ... npm install --save express cookie-parser body-parser . ... For this purpose, the
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