Changing Locale for datetime.strptime conversions

Antoine Pitrou solipsis at pitrou.net
Tue Jul 6 06:38:14 EDT 2010


On Tue, 6 Jul 2010 03:21:21 -0700 (PDT)
AlienBaby <matt.j.warren at gmail.com> wrote:
> I'm still having a bit of trouble,  for example trying to set  the
> locale to Denmark
> 
> 
> locale.setlocale(locale.LC_ALL, locale.normalize('da_DK'))
> 
> returns with
> 
>     locale.setlocale(locale.LC_ALL, locale.normalize('da_DK'))
>   File "C:\Python26\lib\locale.py", line 494, in setlocale
>     return _setlocale(category, locale)
> locale.Error: unsupported locale setting
> 
> 
> Though, from the docs I understand normalize should return a local
> formatted for use with setlocale?

I think normalize works ok, but setlocale then fails (*). You can only
use a locale if it's installed on the computer. That, and other issues
(such as the fact that the locale setting is process-wide and can
interfere with other parts of your program, or third-party libraries;
or the fact that a given locale can have differences depending on the
vendor) make the locale mechanism very fragile and annoying.

If you want to do this seriously, I suggest you instead take a look at
third-party libraries such as Babel:
http://babel.edgewall.org/


(*):

>>> import locale
>>> locale.normalize('da_DK')
'da_DK.ISO8859-1'
>>> locale.setlocale(locale.LC_ALL, locale.normalize('da_DK'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/locale.py", line 513, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting





More information about the Python-list mailing list