I18n issue with optik

Steven Bethard steven.bethard at gmail.com
Sun Apr 1 12:26:54 EDT 2007


Thorsten Kampe 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. But that's 
> nonsense because the Encoding is declared in the Po (localisation) 
> file.
> 
> How can I set the encoding of sys.stdout to another encoding? Of 
> course this would be a terrible hack if the encoding of the 
> localisation changes or different translators use different 
> encodings...

If print_help() is what's wrong, you should probably hack print_help() 
instead of sys.stdout.  You could try something like::

     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
         file.write(self.format_help())

     optparse.OptionParser.print_help = print_help

     cmdlineparser = optparse.OptionParser(description=...)
     ...

That is, you could monkey-patch print_help() before you create an 
OptionParser.

STeVe



More information about the Python-list mailing list