Merge lp:~jr/bzr/i18n-options into lp:bzr

Proposed by Jonathan Riddell
Status: Rejected
Rejected by: Jonathan Riddell
Proposed branch: lp:~jr/bzr/i18n-options
Merge into: lp:bzr
Diff against target: 85 lines (+61/-0)
1 file modified
bzrlib/commands.py (+61/-0)
To merge this branch: bzr merge lp:~jr/bzr/i18n-options
Reviewer Review Type Date Requested Status
Vincent Ladeuil Needs Information
Review via email: mp+72915@code.launchpad.net

Description of the change

Translate command options help text (e.g. the "Show help message." in "-h, --help Show help message.")

This copies and replaces the normal optparse format_options with a local version that adds gettext()

optparse normally expects gettext() to be called when adding the option but that can't be done with our i18n classes because running our i18n.install() imports config which imports commands which initialises the global Options classes so they exist before i18n can be installed.

Not very elegant :(

To post a comment you must log in.
Revision history for this message
Vincent Ladeuil (vila) wrote :

Some of the problems you're encountering were already mentioned in https://code.launchpad.net/~songofacandy/bzr/i18n-command/+merge/62752

Some of the proposed solutions (which couldn't be applied at that time) may also apply here.

Mainly, OptionParser use HelpFormatter and using our own derived class (instead of overriding lower level methods) should address the import issues by delaying the help formatting until our i18n framework is installed.

As said in the review above, this may require a bit more work upfront but should result in less code in the end and simplify the maintenance in the long term.

Also if "imports config which imports commands" is probably fixable.

I can help on both points if you want.

review: Needs Information
Revision history for this message
Vincent Ladeuil (vila) wrote :

@jr: ping, needs any help ?

Revision history for this message
Jonathan Riddell (jr) wrote :

Unmerged revisions

6101. By Jonathan Riddell

replace format_option with our version which adds gettext() call

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bzrlib/commands.py'
2--- bzrlib/commands.py 2011-07-15 14:27:20 +0000
3+++ bzrlib/commands.py 2011-08-25 15:27:13 +0000
4@@ -22,6 +22,37 @@
5
6 # TODO: Specific "examples" property on commands for consistent formatting.
7
8+# format_options from optparse.py,
9+# Copyright (c) 2001-2006 Gregory P. Ward
10+# Copyright (c) 2002-2006 Python Software Foundation
11+# Redistribution and use in source and binary forms, with or without
12+# modification, are permitted provided that the following conditions are
13+# met:
14+#
15+# * Redistributions of source code must retain the above copyright
16+# notice, this list of conditions and the following disclaimer.
17+#
18+# * Redistributions in binary form must reproduce the above copyright
19+# notice, this list of conditions and the following disclaimer in the
20+# documentation and/or other materials provided with the distribution.
21+#
22+# * Neither the name of the author nor the names of its
23+# contributors may be used to endorse or promote products derived from
24+# this software without specific prior written permission.
25+#
26+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27+# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
30+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31+# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32+# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33+# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35+# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37+
38+
39 import os
40 import sys
41
42@@ -330,6 +361,32 @@
43 return plugin_cmds.get(key)()
44 return cmd_or_None
45
46+# taken from Python library's optparse.py
47+# to override method from that module's HelpFormatter.format_option method
48+# adding a call to gettext for translations
49+# See optparse.py for copyright
50+def format_option(self, option):
51+ result = []
52+ opts = self.option_strings[option]
53+ opt_width = self.help_position - self.current_indent - 2
54+ if len(opts) > opt_width:
55+ opts = "%*s%s\n" % (self.current_indent, "", opts)
56+ indent_first = self.help_position
57+ else: # start help on same line as opts
58+ opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
59+ indent_first = 0
60+ result.append(opts)
61+ if option.help:
62+ help_text = gettext(self.expand_default(option))
63+ import textwrap
64+ help_lines = textwrap.wrap(help_text, self.help_width)
65+ result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
66+ result.extend(["%*s%s\n" % (self.help_position, "", line)
67+ for line in help_lines[1:]])
68+ elif opts[-1] != "\n":
69+ result.append("\n")
70+ return "".join(result)
71+
72
73 class Command(object):
74 """Base class for commands.
75@@ -502,6 +559,10 @@
76 # so we get <https://bugs.launchpad.net/bzr/+bug/249908>. -- mbp
77 # 20090319
78 parser = option.get_optparser(self.options())
79+ # replace format_option with our version above which adds gettext() call.
80+ import optparse
81+ optparse._ = gettext
82+ optparse.HelpFormatter.format_option = format_option
83 options = parser.format_option_help()
84 # FIXME: According to the spec, ReST option lists actually don't
85 # support options like --1.14 so that causes syntax errors (in Sphinx