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


Groups > comp.lang.python > #12828

Re: Portable locale usage

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <t@jollybox.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.006
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python.': 0.04; 'attempting': 0.04; 'instance,': 0.05; 'defines': 0.07; 'python': 0.08; '(there': 0.09; 'encoding:': 0.09; 'locale': 0.09; 'subject:usage': 0.16; 'windows:': 0.16; 'wrote:': 0.16; 'language,': 0.17; 'linux': 0.17; 'language': 0.17; 'idea?': 0.18; "doesn't": 0.22; 'assume': 0.22; 'header:In-Reply-To:1': 0.22; 'versions': 0.23; 'dictionary': 0.23; 'platforms.': 0.23; 'referring': 0.23; "shouldn't": 0.23; "user's": 0.23; 'noticed': 0.24; 'skip:l 30': 0.24; 'code': 0.25; 'string': 0.26; 'code.': 0.26; 'windows': 0.26; 'work.': 0.27; 'import': 0.28; 'bound': 0.29; 'interact': 0.29; 'module.': 0.29; 'looks': 0.29; 'second': 0.29; "skip:' 10": 0.30; 'usual': 0.31; 'hi,': 0.32; 'does': 0.32; 'actually': 0.33; 'there': 0.33; 'to:addr:python-list': 0.33; 'it?': 0.33; 'header:User-Agent:1': 0.34; '(as': 0.34; 'setting': 0.34; 'keys': 0.34; 'like:': 0.34; 'uses': 0.35; 'anything': 0.36; 'example,': 0.37; 'using': 0.37; 'but': 0.37; 'something': 0.37; 'two': 0.37; 'not,': 0.38; 'could': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'received:192': 0.39; 'ways': 0.39; 'why': 0.39; 'to:addr:python.org': 0.39; 'sense': 0.39; "i'd": 0.40; "it's": 0.40; 'happens': 0.40; 'more': 0.60; 'your': 0.61; 'believe': 0.65; 'show': 0.67; 'dealing': 0.69; 'received:62': 0.70; 'care': 0.71; 'country': 0.76; 'encoding,': 0.84; 'encoding?': 0.84; 'from:addr:t': 0.84
Date Tue, 06 Sep 2011 13:16:50 +0200
From Thomas Jollans <t@jollybox.de>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:6.0) Gecko/20110816 Thunderbird/6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Portable locale usage
References <e4299b1f-c855-4bc9-9e61-3006172410dc@l28g2000yqh.googlegroups.com>
In-Reply-To <e4299b1f-c855-4bc9-9e61-3006172410dc@l28g2000yqh.googlegroups.com>
X-Enigmail-Version 1.3.1
OpenPGP id=5C8691ED
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.796.1315307774.27778.python-list@python.org> (permalink)
Lines 61
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1315307774 news.xs4all.nl 2421 [2001:888:2000:d::a6]:41072
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:12828

Show key headers only | 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