setLocale in express 4 with no cookies
See original GitHub issueHi
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:
- Created 7 years ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
i18n.__('sting_to_translate')
dispatches from global object. Doing so will lead to concurrency issues while binding toreq
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:
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!!!