I18n issue with optik

Leo Kislov Leo.Kislov at gmail.com
Sun Apr 1 17:24:17 EDT 2007


On Apr 1, 8:47 am, Thorsten Kampe <thors... at thorstenkampe.de> wrote:
> I guess the culprit is this snippet from optparse.py:
>
> # used by test suite
> def _get_encoding(self, file):
>     encoding = getattr(file, "encoding", None)
>     if not encoding:
>         encoding = sys.getdefaultencoding()
>     return encoding
>
> def print_help(self, file=None):
>     """print_help(file : file = stdout)
>
>     Print an extended help message, listing all options and any
>     help text provided with them, to 'file' (default stdout).
>     """
>     if file is None:
>         file = sys.stdout
>     encoding = self._get_encoding(file)
>     file.write(self.format_help().encode(encoding, "replace"))
>
> So this means: when the encoding of sys.stdout is US-ASCII, Optparse
> sets the encoding to of the help text to ASCII, too.

.encode() method doesn't set an encoding. It encodes unicode text into
bytes according to specified encoding. That means optparse needs ascii
or unicode (at least) for help text. In other words you'd better use
unicode throughout your program.

> But that's
> nonsense because the Encoding is declared in the Po (localisation)
> file.

For backward compatibility gettext is working with bytes by default,
so the PO file encoding is not even involved. You need to use unicode
gettext.

> How can I set the encoding of sys.stdout to another encoding?

What are you going to set it to? As I understand you're going to
distribute your program to some users. How are you going to find out
the encoding of the terminal of your users?

  -- Leo




More information about the Python-list mailing list