It is easy to get the current language (e.g. en) anywhere in a django app: django.utils.translation.get_lanaguage()
But how do I get the current locale (e.g. en_US or en_GB)?
Did you try to_locale()
?
from django.utils.translation import to_locale, get_language
to_locale(get_language())
The distinction between language
and locale
(in Django, at least) is just a matter of formatting. Both en
and en-us
are languages, and if en-us
is the currently selected language then that will be returned by get_language()
.
So your problem seems to be that Django is not setting the current language the way you expect. There's a long list of techniques Django uses to try and figure out the language to use, so I suggest working your way down that to see why the language isn't what you expect.
For example:
If a base language is available but the sublanguage specified is not, Django uses the base language. For example, if a user specifies
de-at
(Austrian German) but Django only hasde
available, Django usesde
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With