Portable locale usage

ssegvic sinisa.segvic at fer.hr
Tue Sep 6 16:46:46 CEST 2011


On 6 ruj, 13:16, Thomas Jollans <t... at jollybox.de> wrote:
> > locale.setlocale(locale.LC_ALL, (myISOCountryCode,
> > locale.getpreferredencoding()))
>
> As far as I can tell, this does work. Can you show us a traceback?

Sorry, I was imprecise.

I wanted to say that the above snippet
does not work both on Windows and Linux.

This is what I get on Windows:

>>> import sys
>>> sys.version
'3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)]'
>>> myISOCountryCode='hr'
>>> locale.setlocale(locale.LC_ALL, (myISOCountryCode, locale.getpreferredencoding()))
Traceback (most recent call last):
  File "<pyshell#113>", line 1, in <module>
    locale.setlocale(locale.LC_ALL, (myISOCountryCode,
locale.getpreferredencoding()))
  File "C:\apps\Python32\lib\locale.py", line 538, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting

The snippet actually works on Linux, as you note.

> It looks like you don't actually care about the encoding: in your first
> example, you use the default system encoding, which you do not control,
> and in your second example, you're using two different encodings on the
> two platforms.

That's true.

That's because currently I care most about
lists of strings being sorted properly (see below).

Nevertheless, it *appears* to me that, in the Unicode era,
the locales could well be decoupled from particular encodings.
But this is another topic.

> So why do you care whether or not the default uses ISO 8859-2 ?

It's not that I care about encoding,
it's that Windows throws locale.Error at me :-)

> > 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?
>
> As I said, I believe the above code should work. It works on my Linux
> system.
>
> What are you attempting to achieve with this setting of the locale,
> without even setting the encoding? Doesn't it make more sense to simply
> use the user's usual locale, and interact with them on their own terms?

For the moment, I only wish to properly sort a Croatian text file
both on Windows and Linux (I am a cautious guy, I like reachable
goals).
When the locale is properly set, sorting works like a charm
with mylist.sort(key=locale.strxfrm).

My current solution to the portability problem is:

import locale
try:
  locale.setlocale(locale.LC_ALL, 'hr_HR.utf8')      # linux
except locale.Error:
  locale.setlocale(locale.LC_ALL, 'Croatian_Croatia.1250')    #
windows

Thanks for your feedback!

Sinisa



More information about the Python-list mailing list