i18n: looking for expertise

"Martin v. Löwis" martin at v.loewis.de
Fri Mar 11 05:05:12 EST 2005


klappnase wrote:
> On my box (mandrake-10.1) sys.getfilesystemencoding() returns
> 'ISO-8859-15',
> however :
> 
>>>>locale.nl_langinfo(locale.CODESET)
> 
> 'ANSI_X3.4-1968'

In the locale API, you have to do

locale.setlocale(locale.LC_ALL, "")

to activate the user's preferences. Python does that on startup,
but then restores it to the "C" locale, since that is the specified
locale for the beginning of the (Python) program.

Try that before invoking nl_langinfo.

> Anyway, my app currently runs with python-2.2 and I would like to keep
> it that way if possible, so I wonder which is the preferred
> replacement for sys.getfilesystemencoding() on versions < 2.3 , or in
> particular, will the method I use to determine "sysencoding" I
> described in my original post be safe or are there any traps I missed
> (it's supposed to run on linux only)?

I would put an nl_langinfo call in-between, since this is more reliable
than getdefaultlocale (which tries to process environment variables
themselves and happens to crash if they are not in an expected form).

See idlelib/IOBinding.py for the algorithm that I use in IDLE to
determine the "user's" encoding. On most systems, this encoding is
good for usage on the file system API, except for MacOS X, which
uses UTF-8 to encode file names regardless of user or system
settings.

Regards,
Martin



More information about the Python-list mailing list