How/where to call setlocale

344 views
Skip to first unread message

James Hargreaves

unread,
Sep 16, 2014, 4:58:27 AM9/16/14
to django...@googlegroups.com
Hello,

I am running Django/python on Ubuntu. I am finding that my locale is not setup correctly. Running locale from the command line shows this:

LANG=en_GB.UTF-8
LANGUAGE=en_GB:en
LC_CTYPE="en_GB.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_PAPER="en_GB.UTF-8"
LC_NAME="en_GB.UTF-8"
LC_ADDRESS="en_GB.UTF-8"
LC_TELEPHONE="en_GB.UTF-8"
LC_MEASUREMENT="en_GB.UTF-8"
LC_IDENTIFICATION="en_GB.UTF-8"
LC_ALL=

Running python from the command line I can see that my locale is not set hence I am running with the default ("C") locale which has the wrong (non-GB) date format. Calling setlocale(...) fixes this:

>>> import locale
>>> locale.getdefaultlocale()
('en_GB', 'UTF-8')
>>>
>>> locale.getlocale()
(None, None)
>>> locale.nl_langinfo(locale.D_FMT)
'%m/%d/%y'
>>>
>>> locale.setlocale(locale.LC_ALL, '')
'en_GB.UTF-8'
>>>
>>> locale.getlocale()
('en_GB', 'UTF-8')
>>> locale.nl_langinfo(locale.D_FMT)
'%d/%m/%y'

However, I am unsure how/where to run setlocale(...) within my configuration/start-up since this affects the entire environment, though that is not really a problem since all my sites are UK based.

Can someone help please?

Thanks
Jay

rettev...@googlemail.com

unread,
Sep 16, 2014, 8:18:24 AM9/16/14
to django...@googlegroups.com
Hi, 
I ran into a similar problem yesterday while programming a web shop with Django (Version 1.6, Python version 3.3) where language switching should immediately have an effect on the way prices, etc. are displayed (doing so by a <form action="/i18n/setlang/" method="POST" > in the template and redirecting back to the page where it was called). The locale is indeed not set this way and needs to be set explicitly. What I finally came up with as a first working solution as part of a function:

def convert_my_price(_price)  # _price is a decimal.Decimal
    language = django.utils.translation.get_language()
    if language == 'en':
        locale.setlocale(locale.LC_ALL, 'en_US.utf-8')     # 'en_GB.utf-8'  didn't work for me 
    else:
        locale.setlocale(locale.LC_ALL, 'de_DE.utf-8')    # this is the fallback

    _loc = locale.localeconv() 
    .
    .  # process the Decimal
    .  
    return my_price_as_a_string

After doing so I have access to the locale details stored in the dictionairy _loc, like e.g. _loc['thousands_sep'], _loc['currency_symbol'] in this function, but also everywhere else via locale.localeconv() 
This function however is currently called from within class based views, but also implemented as a template filter. Of course one should not set the locale every time when calling this function, as you mentioned locale.getlocale() will should whether it was correctly set or not.

Where you call locale.setlocale depends on your application. When you only have to set it once maybe the best would be to call it in even in e.g. wsgi.py 

Best regards,
Ulrich

James Hargreaves

unread,
Sep 16, 2014, 4:39:08 PM9/16/14
to django...@googlegroups.com
Thanks for your reply Ulrich.

If I set the locale in WSGI would that persist for all connections? If so that sounds like the best option.

Thanks
Jay
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/9kuQBMknp9g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/65e039ab-b2ef-4f78-ab39-0ee53f2f5629%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
a: 1 Oak Cottage, Town Lane, Mobberley, WA16 7HJ
t: 01565 873 019 | 07899 872 306
e: ja...@hargreaves.me.uk

uvetter

unread,
Sep 16, 2014, 6:19:04 PM9/16/14
to django...@googlegroups.com
Dear Jay,

I just tried to following in my wsgi.py file:


import os
import sys
import site
import locale

site.addsitedir('/mypath/lib/python3.3/site-packages')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "test.settings")

os.environ['HTTPS'] = "on"

sys.path.append('/mypath/test')
sys.path.append('/mypath/test/test')
sys.path.append('/mypath/test/app1')
sys.path.append('/mypath/test/app2')

activate_env=os.path.expanduser("/mypath/bin/activate_this.py")
exec(open(activate_env).read())

locale.setlocale(locale.LC_ALL, 'en_US.utf-8')

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()



This sets the locale correctly for all calls to my Django application. 

I just realized that in your first post you mentioned that "this affects the entire environment". I just read through the locale module documentation, which states that "The C standard defines the locale as a program-wide property ....". This means that there will be no system-wide changes when you call locale.setenv(). Or did I misunderstand you?

Best wishes,
Ulrich 

James Hargreaves

unread,
Sep 17, 2014, 2:39:08 AM9/17/14
to django...@googlegroups.com
Thanks Ulrich.

No I think I've misread the documentation with regards it affecting the whole environment rather than the program.

I'll try your solution myself but it looks like it will work, thanks!

Jay
--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/9kuQBMknp9g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

For more options, visit https://groups.google.com/d/optout.

James Hargreaves

unread,
Sep 18, 2014, 5:08:56 PM9/18/14
to django...@googlegroups.com
Hi Ulrich et al,

Thanks again for your help.

I'm having issues with your solution. It works when making a literal call to locale but Django is not using the correct locale for some reason.

I added this to my application.wsgi:

    import locale

    locale.setlocale(locale.LC_ALL, 'en_GB.utf-8')

I added a context processor which outputs the following:

    def common(request=None):
        return {
            'TODAY' : date.today(),
            'LOCALE' : locale.getlocale(),
            'LOCALE_DF' : locale.nl_langinfo(locale.D_FMT),
        }

And I added the following to my template:

    <pre>{{ LOCALE }}</pre>
    <pre>{{ LOCALE_DF }}</pre>
    <pre>{{ TODAY|date:"SHORT_DATE_FORMAT" }}</pre>

Which unexpectedly outputs this:

    <pre>('en_GB', 'UTF-8')</pre>
    <pre>%d/%m/%y</pre>
    <pre>09/18/2014</pre>

I also tried adding the locale.setlocale(...) line to my settings.py instead but that had no effect either.

Any ideas appreciated please.

Thanks
Jay

James Hargreaves

unread,
Sep 19, 2014, 3:54:21 AM9/19/14
to django...@googlegroups.com
Thanks again for your reply Ulrich. I finally figured out the problem :)

Towards the bottom of my settings.py I had mistakenly overwritten the LANGUAGE_CODE as follows:

    LANGUAGE_CODE = 'en'

Django must determine the locale internally based on the LANGUAGE_CODE specified. Needless to say this line broke the locale lookup since en is not a valid language code.

Once I removed the line above the locale was set correctly. In fact, I was able to remove the manual setlocale(...) call from my wsgi.py file without any issues.

Thanks again for your help, it is very much appreciated :)

Jay

uvetter

unread,
Sep 19, 2014, 5:16:15 AM9/19/14
to django...@googlegroups.com
Dear Jay,
glad that it works now.
I got myself confused about terms such as language name and locale name. Setting the locale via locale.setlocale does indeed have no influence on the Django settings. I knew that but I misunderstood you. I set locale.setlocale in the example because I wanted to make use of locale details for correct currency display (i.e. the currency symbol, whether the currency symbol is displayed in front of the numbers or after them, etc.). I found than Django has some of the locale details implemented, e.g. the thousand separator, but not all of them to my best knowledge. 
To be honest: it is not really clear to me how Django distinguishes/defines locale and language. For the definition of a locale, at least when looking at locale.setlocale, the encoding is required additionally. 

Best wishes,
Ulrich

Jay
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.


--
a: 1 Oak Cottage, Town Lane, Mobberley, WA16 7HJ
t: 01565 873 019 | 07899 872 306
e: ja...@hargreaves.me.uk




--
a: 1 Oak Cottage, Town Lane, Mobberley, WA16 7HJ
t: 01565 873 019 | 07899 872 306
e: ja...@hargreaves.me.uk
Reply all
Reply to author
Forward
0 new messages