[PATCH] fix "yum --help" with translated "Usage: %s\n". BZ 1033416

Zdenek Pavlas zpavlas at redhat.com
Wed Jan 8 10:01:41 UTC 2014


This looks like a giant hack, but IMO it's the best option we have.
Encoding all command and option descriptions to utf8 is much worse.

Third option is to subclass optparse.IndentedHelpFormatter, override
format_usage(), and pass the subclass to OptionParse constructor.
(more code, does not fix other optparse translations)
---
 test/misc-tests.py | 13 +++++++++++++
 yum/i18n.py        |  8 ++++++++
 2 files changed, 21 insertions(+)

diff --git a/test/misc-tests.py b/test/misc-tests.py
index 7d7d06f..11fd041 100644
--- a/test/misc-tests.py
+++ b/test/misc-tests.py
@@ -150,6 +150,19 @@ class MiscTests(DepsolveTests):
             self.assertEqual(type(actual), type(expected))
             self.assertEqual(actual, expected)
 
+    def testOptparse(self):
+        # make 'Usage: %s\n' translated
+        import gettext
+        def dgettext(domain, msg, orig=gettext.dgettext):
+            if domain=='messages' and msg == 'Usage: %s\n':
+                return 'Pou\xc5\xbeit\xc3\xad: %s\n'
+            return orig(domain, msg)
+        gettext.dgettext = dgettext
+        # run "yum --help"
+        from optparse import OptionParser
+        parser = OptionParser(usage=u'\u011b\u0161\u010d')
+        self.assertRaises(SystemExit, parser.parse_args, args=['--help'])
+
 def setup_logging():
     logging.basicConfig()    
     plainformatter = logging.Formatter("%(message)s")    
diff --git a/yum/i18n.py b/yum/i18n.py
index 76a258d..2c0cbce 100755
--- a/yum/i18n.py
+++ b/yum/i18n.py
@@ -500,6 +500,14 @@ try:
     t = gettext.translation('yum', fallback=True)
     _ = t.ugettext
     P_ = t.ungettext
+
+    # we describe yum commands and options with unicode but optparse
+    # mixes this with non-unicode translations so "yum --help" may fail.
+    # It's much easier to fix this in optparse than in yum. BZ 1033416
+    import optparse
+    if optparse._ is gettext.gettext:
+        #optparse._ = lambda msg: to_unicode(gettext.gettext(msg))
+        optparse._ = gettext.translation('messages', fallback=True).ugettext
 except:
     '''
     Something went wrong so we make a dummy _() wrapper there is just
-- 
1.7.11.7



More information about the Yum-devel mailing list