Locale settings

"Martin v. Löwis" martin at v.loewis.de
Mon Apr 21 16:26:35 EDT 2003


Bob van der Poel wrote:

> Now, for the most part this works. However, one user of my program has 
> just upgraded to Linux Mandrake 9.1 and (I think) he has set his 
> language to "de". In any event, the getdefaultlocal() seems to return 
> 'de' which isn't a valid python encoding (??) and the above code then 
> converts to his 'ascii'.

Using getdefaultlocale() is broken, and cannot be fixed. On Unix, use

locale.nl_langinfo(locale.CODESET)

instead; if supported, this will tell you the user's preferred encoding.
Be aware that
a) neither nl_langinfo nor CODESET may be supported by the system,
b) they may return a codeset for which a Python codec is not available,
c) you may have to invoke setlocale(locale.LC_CTYPE, "") before
    nl_langinfo returns a reasonable response (e.g. on Solaris)

Python 2.3 has a function locale.getpreferredencoding which does all
that. I recommend you copy its source code into your application, and
use that instead.

Regards,
Martin





More information about the Python-list mailing list