Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current locale

Tags:

sails.js

The question is simple, however I found nothing about it in the docs:

How can I get the current locale for a request? (the locale used for translations with req.__('to_translate');)

Is there something like req.getLocale(); ?

like image 379
Fabien Henon Avatar asked Jul 13 '26 15:07

Fabien Henon


1 Answers

Indeed, as you stated, you must use req.getLocale()

Citing a paragraph from the sails docs

By default, node-i18n will detect the desired language of a request by examining its language headers. Language headers are set in your users' browser settings, and while they're correct most of the time, you may need the flexibility to override this detected locale and provide your own.

So you should get your locale. eg: "en", if the user that accessed your endpoint, has English configured as its browser language.

Now if you are resetting the locale value in a sails policy, or the controller itself, you might get faulty data if it was set incorrectly, since i assume you already tried the req.getLocale method without success, but since you don't provide more data, i can just guess.

Btw, you can access the current locale in your view, by directly accessing to the locale variable (which is eval-ed from the locals.locale object sent to the view, at least in jade views...)

like image 162
Lu Roman Avatar answered Jul 18 '26 21:07

Lu Roman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!