Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the current locale in django

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)?

like image 264
user3834658 Avatar asked Jan 20 '15 14:01

user3834658


2 Answers

Did you try to_locale()?

from django.utils.translation import to_locale, get_language
to_locale(get_language())
like image 172
dan-klasson Avatar answered Oct 12 '22 07:10

dan-klasson


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 has de available, Django uses de.

like image 21
Kevin Christopher Henry Avatar answered Oct 12 '22 06:10

Kevin Christopher Henry