Portable locale usage

ssegvic sinisa.segvic at fer.hr
Tue Sep 6 17:31:59 CEST 2011


On 6 ruj, 15:13, Vlastimil Brom <vlastimil.b... at gmail.com> wrote:

> 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

Thanks for your feedback!


So this works only on Linux (in concordance with the documentation):

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

And this works only on Windows (incomplete locale spec probably filled
in by Windows API):

locale.setlocale(locale.LC_ALL, 'croatian')


Obviously, there is a misunderstanding between Python
which uses standard (IANA) language codes
and Windows which, as usual, have their own ways :-(


One possible solution would be to change
locale.locale_alias on Windows so that
it honors the custom Windows conventions:
 'hr' -> 'Croatian_Croatia.1250'
   instead of
 'hr' -> 'hr_HR.ISO8859-2'

In addition, locale.getpreferredencoding()
should probably be changed in order to return
valid Windows encodings ('1250' instead of 'cp1250').

Cheers,

Sinisa



More information about the Python-list mailing list