Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #12828

Re: Portable locale usage

Date 2011-09-06 13:16 +0200
From Thomas Jollans <t@jollybox.de>
Subject Re: Portable locale usage
References <e4299b1f-c855-4bc9-9e61-3006172410dc@l28g2000yqh.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.796.1315307774.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 06/09/11 11:59, ssegvic wrote:
> 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!
Doesn't it?

> myISOCountryCode='hr'

This is a language code. (there also happens to be a country code 'hr',
but you're referring to the Croatian language, 'hr')

> locale.setlocale(locale.LC_ALL, (myISOCountryCode,
> locale.getpreferredencoding()))

As far as I can tell, this does work. Can you show us a traceback?

> 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'.

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. So why do you care whether or not the default uses ISO
8859-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?

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?

> 3. What is the status of locale.locale_alias (official documentation
> does not mention it)?

I don't know, but I'd assume it's not considered part of the public API,
and you that shouldn't assume that it'll exist in future versions of Python.

Thomas

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Portable locale usage ssegvic <sinisa.segvic@fer.hr> - 2011-09-06 02:59 -0700
  Re: Portable locale usage Thomas Jollans <t@jollybox.de> - 2011-09-06 13:16 +0200
    Re: Portable locale usage ssegvic <sinisa.segvic@fer.hr> - 2011-09-06 07:46 -0700
      Re: Portable locale usage Thomas Jollans <t@jollybox.de> - 2011-09-06 17:53 +0200
        Re: Portable locale usage ssegvic <sinisa.segvic@fer.hr> - 2011-09-07 03:39 -0700
          Re: Portable locale usage Thomas Jollans <t@jollybox.de> - 2011-09-07 15:15 +0200
        Re: Portable locale usage ssegvic <sinisa.segvic@fer.hr> - 2011-09-07 04:17 -0700
    Re: Portable locale usage garabik-news-2005-05@kassiopeia.juls.savba.sk - 2011-09-06 20:58 +0000
      Re: Portable locale usage ssegvic <sinisa.segvic@fer.hr> - 2011-09-07 04:02 -0700
  Re: Portable locale usage Vlastimil Brom <vlastimil.brom@gmail.com> - 2011-09-06 15:13 +0200
    Re: Portable locale usage ssegvic <sinisa.segvic@fer.hr> - 2011-09-06 08:31 -0700

csiph-web