Getting the current locale in django
Asked Answered
W

4

6

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

Wiburg answered 20/1, 2015 at 14:11 Comment(0)
A
4

Did you try to_locale()?

from django.utils.translation import to_locale, get_language
to_locale(get_language())
Absher answered 20/1, 2015 at 14:47 Comment(2)
to_locale(get_language()) still only outputs 'en' if language is 'en'.Wiburg
The docs for to_locale say: Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is True, the last component is lower-cased (en_us)...... It therefore expects a language name which is a language + country. My problem is that a locale contains both the language and country, so cannot be extracted from just the language.Wiburg
O
2

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.

Osseous answered 20/1, 2015 at 18:40 Comment(0)
U
2

Django's django.utils.translation.to_locale() expects a "language name" like en-us and will convert that to a "locale name" like en_US.

Source code: https://github.com/django/django/blob/master/django/utils/translation/init.py#L271-L284

It basically just does some string manipulation.

If your "language name" is just a simple language code like en, it will return just en. If you want to convert en to a locale like en_US.UTF8, you will have to write your own to_locale() function. You'll have to determine what locale you want for the language code en. Example:

LANG_TO_LOCALE = {
    'en': 'en_US.UTF8',
    'nl': 'nl_NL.UTF8',
    'es': 'es_ES.UTF8'
}

def lang_to_locale(language_code):
    return LANG_TO_LOCALE.get(language_code)

Depending on your taste you might for example want to get the locale en_GB.UTF8 instead of en_US.UTF8 for the language code en.

This then can be used to set the locale in Python:

import locale
locale.setlocale(locale.LC_ALL, lang_to_locale('nl'))

And then you can get a month name in the desired language:

from datetime import datetime
print(datetime.strftime(datetime.now(), '%B'))  # Prints month name in Dutch

To make this work you need to have the appropriate locale packages installed on your system. On Ubuntu you can do this with sudo dpkg-reconfigure locales.

Uncovenanted answered 14/3, 2019 at 17:13 Comment(0)
M
2

In the relevant (virtual) environment:

python
>>> import locale
>>> locale.getlocale()

e.g. ('en_GB', 'UTF-8')

Moly answered 7/4, 2019 at 16:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.