i18n: looking for expertise

klappnase klappnase at web.de
Fri Mar 11 19:25:24 EST 2005


"Martin v. Löwis" <martin at v.loewis.de> wrote in message news:<42316d58$0$13332$9b622d9e at news.freenet.de>...
>
> 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).
> 

Thanks!!

Actually I came to try my code on another box today which still runs
python2.2 and found that my original code crashed because neither
sys.getpreferredencoding() nor sys.stdin.encoding exist and
locale.getdefaultlocale()[1] returnd 'de' .So I changed my
_sysencoding() function to this:

def _sysencoding():
    # try to guess the system default encoding
    try:
        enc = locale.getpreferredencoding().lower()
        if _find_codec(enc):
            print 'Setting locale to %s' % enc
            return enc
    except AttributeError:
        # our python is too old, try something else
        pass
    locale.setlocale(locale.LC_ALL, "")
    enc = locale.nl_langinfo(locale.CODESET).lower()
    if _find_codec(enc):
        print 'Setting locale to %s' % enc
        return enc
    # the last try
    enc = locale.getdefaultlocale()[1].lower()
    if _find_codec(enc):
        print 'Setting locale to %s' % enc
        return enc
    # aargh, nothing good found, fall back to latin1 and hope for the
best
    print 'Warning: cannot find usable locale, using latin-1'
    return 'iso-8859-1'

> See idlelib/IOBinding.py for the algorithm that I use in IDLE to
> determine the "user's" encoding.

I guess I should have done so from the beginning.

Thanks again and best regards

Michael



More information about the Python-list mailing list