Locale confusion
Jorgen Grahn
jgrahn-nntq at algonet.se
Wed Jan 12 16:38:01 EST 2005
On 11 Jan 2005 05:49:32 -0800, Serge.Orlov at gmail.com <Serge.Orlov at gmail.com> wrote:
> Jorgen Grahn wrote:
> [snip]
>
>>
>> frailea> cat foo
>> import locale
>> print locale.getlocale()
>> locale.setlocale(locale.LC_CTYPE)
>> print locale.getlocale()
...
>> When I run it as a script it isn't though, and the setlocale() call
> does not
>> appear to fall back to looking at $LANG as it's supposed to(?), so my
>> LC_CTYPE remains in the POSIX locale:
...
>> So, is this my fault or Python's? I realize I could just adapt and
> set
>> $LC_CTYPE explicitly in my environment, but I don't want to
> capitulate for a
>> Python bug, if that's what this is.
>
> Try locale.setlocale(locale.LC_CTYPE,"") as in your C program.
Oops, you are right. locale.setlocale(locale.LC_CTYPE,"") sets the locale
from my environment (and gets it right!) while
locale.setlocale(locale.LC_CTYPE) /returns/ the current locale. I don't know
how I could have missed that, since it's clearly documented and also maps
directly to C usage.
> It would
> be great if locale.setlocale with one parameter would be deprecated,
> because it suddenly acts like getlocale. It's unpythonic.
I dislike the term "unpythonic", but I tend to agree with you in practice
here. Even better, but maybe not feasible, would be an approach to locales
which doesn't involve changing a global state in this fashion.
> By the way, since you took time to setup various LC_* variables there
> is no need to play with LC_CTYPE category. Just use the standard idiom.
> import locale
> locale.setlocale(LC_ALL,"")
Thanks for pointing that out. I picked out LC_CTYPE for my small program
because I was in a hurry and didn't want to risk non-standard sorting
elsewhere in the program. I hate what the LC_COLLATE=C does to swedish
national characters, but I hate what LC_COLLATE=sv_SE does to non-alphabetic
characters even more.
To paraphrase Barbie: "i18n is hard". ;-)
/Jorgen
--
// Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
\X/ algonet.se> R'lyeh wgah'nagl fhtagn!
More information about the Python-list
mailing list