Portable locale usage

Siniša Šegvić ssegvic at zemris.fer.hr
Thu Sep 8 17:39:42 CEST 2011


> From: "Laszlo Nagy" <gandalf at shopzeus.com>
> To: "Siniša Šegvić" <ssegvic at zemris.fer.hr>, python-list at python.org
> Sent: Thursday, September 8, 2011 10:41:22 AM
> Subject: Re: Portable locale usage
> > I have set the system-wide locale to Croatian (Croatia)
> > on my development system as instructed by:
> > http://windows.microsoft.com/en-US/windows-vista/Change-the-system-locale
> >
> > Nevertheless, your proposal produces:
> > ('English_United States','1252')
> This is what I see on my Hungarian Windows:
> 
> 
> C:\Users\User>python
> Python 2.7.1 (r271:86832, Nov 27 2010, 17:19:03) [MSC v.1500 64 bit
> (AMD64)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import locale
> >>> locale.setlocale(locale.LC_ALL, '')
> 'Hungarian_Hungary.1250'
> >>> locale.getlocale()
> ('Hungarian_Hungary', '1250')
> >>>
> 
> So I'm 100% sure that the problem is with your system locale settings,
> not Python.

I've just found out how to set the user locale on Windows.

One has to go to Control panel -> Regional and language options,
then select the tab named Formats, and finally 
set the box Current format to the desired language, 
which is in my case Croatian (Croatia).

The whole tab says nothing about locales, I found this by try and test.
This recipe is not affected by the system locale (which I was setting before)!

Now locale.setlocale(locale.LC_ALL, '') sets the Croatian locale.

> > I expect that most of my Windows users will not care
> > to configure their computers with the national locale
> > (and besides, that does not seem to work, anyway).
> Croatian users will most likely use a Croatian Windows, out of the
> box.
> And on those systems, using locale.setlocale(locale.LC_ALL, '') will
> work perfectly. 

Yes it's true, you were right, I was setting 
the Croatian language at the wrong place
(I am not a Windows fan neither, I normally work on Linux).

However, I am not completely happy with this.
OK, no need for system restart, but still,
it would be nice if Python program could
manage around this by itself, 
of course, provided that the required locale is installed.

> > Note that I would very much like
> > to avoid changing the system locale
> > (this requires Administrator password and system restart).
> All right. But you understand, that Croatian ISO8859-2 is not
> supported on windows? 

Yes I do understand that.

I have commented that the Python's locale aliasing engine
should not propose iso8859-2 on Windows systems,
exactly for the reason you mention.


> >> Why are you trying to force a specific locale to your program
> >> anyway?
> > Because I wish to be able to correctly sort Croatian names.
> Well, all right. If you want to sort Croatian names from a program that
> runs on an English (or whatever) system, then you will have to check the
> platform and use a locale that is supported by the platform. (But again,
> this is not Python's limitation. Python doesn't know what encodings are
> supported, in advance, and you cannot use a locale that is not supported...)

I fully agree.

I commented that, if a proper locale is installed,
the following should work on any system:

locale.setlocale(locale.LC_ALL, ('hr', locale.getpreferredencoding())) 

Currently the above does not work on Windows,
and that is because the locale_alias for 'hr'
is bound to 'hr_HR.ISO8859-2'.
Check the source: .../Python-3.2.2/Lib/locale.py, line 537

I was arguing that, on a Windows system, 
the locale_alias for 'hr' should be bound 
to 'Croatian_Croatia.1250'.

Cheers,

Sinisa



More information about the Python-list mailing list