unicode and locale of machine ?
Martin v. Loewis
martin at v.loewis.de
Mon Mar 25 08:43:51 EST 2002
pekka niiranen <krissepu at vip.fi> writes:
> Is it possible to read the current keyboard layout or
> language settings in windows/unix and use that
> automatically to convert text from unicode to printable
> in another machine ?
Some machines might not provide you with that information, so you
might have a hard time figuring out what encoding to use. Here are
some strategies:
- On Unix, try locale.nl_langinfo(locale.CODESET). If that is
available, it should be a string identifying the user's encoding.
Pitfalls:
- you might need to call setlocale before nl_langinfo returns
something useful; make sure to set atleast the LC_CTYPE category.
- the operating system may not support nl_langinfo, or CODESET.
- the user may not have configured her locale properly
- the encoding returned by the operating system may not be known
to Python
- On Windows, when passing strings to GUI code, you can use the "mbcs"
encoding, which is Python's name for the CP_ACP codepage, which is
Microsoft's abbreviation for "ANSI code page", which is Microsoft's
name for the user's locale's encoding.
Pitfalls:
- Windows has *two* locale's encodings: the ANSI code page, and the
OEM code page. The cmd.exe/command.com window, by default, uses
the OEM code page, for which Python has no easy wrapper.
- In any specific console window, the console code page may have
been changed.
If you find the encoding "enc" that the user users, you can print the
Unicode strings uni just by doing
print uni.encode("enc")
HTH,
Martin
More information about the Python-list
mailing list