Portable locale usage

Vlastimil Brom vlastimil.brom at gmail.com
Tue Sep 6 09:13:17 EDT 2011


2011/9/6 ssegvic <sinisa.segvic at fer.hr>:
> Hi,
>
> I am musing on how to write portable Python3 code which would
> take advantage of the standard locale module.
>
> For instance, it would be very nice if we could say something like:
>
> # does not work!
> myISOCountryCode='hr'
> locale.setlocale(locale.LC_ALL, (myISOCountryCode,
> locale.getpreferredencoding()))
>
> Up to now, I have found ways to set locale on Linux and Windows:
>
> import locale
> locale.setlocale(locale.LC_ALL, 'hr_HR.utf8')      # works on linux
> locale.setlocale(locale.LC_ALL, 'hrv_HRV.1250') # works on windows
>
> I have noticed that locale defines a dictionary locale.locale_alias,
> and that it contains the following promising keys: 'hr_hr',
> 'hrvatski', 'hr'.
> Unfortunately, both on Windows and Linux all these keys
> are bound to the same outdated string 'hr_HR.ISO8859-2'.
>
> My questions are the following:
>
> 1. Is there a way for writing portable Python code dealing with
> locales
>    (as sketched in the beginning)?
>
> 2. If not, is there anything wrong with that idea?
>
> 3. What is the status of locale.locale_alias (official documentation
> does not mention it)?
>
>
> Cheers,
>
> Sinisa
>
> http://www.zemris.fer.hr/~ssegvic/index_en.html
> --
> http://mail.python.org/mailman/listinfo/python-list
>

There may be some differences btween OSes end the versions, but using
python 2.7 and 3.2 on Win XP and Win7 (Czech)
I get the following results for setlocale:

>>> locale.setlocale(locale.LC_ALL,'Croatian')
'Croatian_Croatia.1250'
>>> locale.getlocale()
('Croatian_Croatia', '1250')
>>> locale.getpreferredencoding(do_setlocale=False)
'cp1250'
>>>

However, "hr" is not recognised on this systems:

>>> locale.setlocale(locale.LC_ALL, "hr")
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "locale.pyc", line 531, in setlocale
Error: unsupported locale setting
>>>

regards,
   vbr



More information about the Python-list mailing list