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
=== modified file 'bzrlib/commands.py'
--- bzrlib/commands.py 2011-07-15 14:27:20 +0000
+++ bzrlib/commands.py 2011-08-25 15:27:13 +0000
@@ -22,6 +22,37 @@
2222
23# TODO: Specific "examples" property on commands for consistent formatting.23# TODO: Specific "examples" property on commands for consistent formatting.
2424
25# format_options from optparse.py,
26# Copyright (c) 2001-2006 Gregory P. Ward
27# Copyright (c) 2002-2006 Python Software Foundation
28# Redistribution and use in source and binary forms, with or without
29# modification, are permitted provided that the following conditions are
30# met:
31#
32# * Redistributions of source code must retain the above copyright
33# notice, this list of conditions and the following disclaimer.
34#
35# * Redistributions in binary form must reproduce the above copyright
36# notice, this list of conditions and the following disclaimer in the
37# documentation and/or other materials provided with the distribution.
38#
39# * Neither the name of the author nor the names of its
40# contributors may be used to endorse or promote products derived from
41# this software without specific prior written permission.
42#
43# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
44# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
45# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
46# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
47# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
48# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
50# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
51# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54
55
25import os56import os
26import sys57import sys
2758
@@ -330,6 +361,32 @@
330 return plugin_cmds.get(key)()361 return plugin_cmds.get(key)()
331 return cmd_or_None362 return cmd_or_None
332363
364# taken from Python library's optparse.py
365# to override method from that module's HelpFormatter.format_option method
366# adding a call to gettext for translations
367# See optparse.py for copyright
368def format_option(self, option):
369 result = []
370 opts = self.option_strings[option]
371 opt_width = self.help_position - self.current_indent - 2
372 if len(opts) > opt_width:
373 opts = "%*s%s\n" % (self.current_indent, "", opts)
374 indent_first = self.help_position
375 else: # start help on same line as opts
376 opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
377 indent_first = 0
378 result.append(opts)
379 if option.help:
380 help_text = gettext(self.expand_default(option))
381 import textwrap
382 help_lines = textwrap.wrap(help_text, self.help_width)
383 result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
384 result.extend(["%*s%s\n" % (self.help_position, "", line)
385 for line in help_lines[1:]])
386 elif opts[-1] != "\n":
387 result.append("\n")
388 return "".join(result)
389
333390
334class Command(object):391class Command(object):
335 """Base class for commands.392 """Base class for commands.
@@ -502,6 +559,10 @@
502 # so we get <https://bugs.launchpad.net/bzr/+bug/249908>. -- mbp559 # so we get <https://bugs.launchpad.net/bzr/+bug/249908>. -- mbp
503 # 20090319560 # 20090319
504 parser = option.get_optparser(self.options())561 parser = option.get_optparser(self.options())
562 # replace format_option with our version above which adds gettext() call.
563 import optparse
564 optparse._ = gettext
565 optparse.HelpFormatter.format_option = format_option
505 options = parser.format_option_help()566 options = parser.format_option_help()
506 # FIXME: According to the spec, ReST option lists actually don't567 # FIXME: According to the spec, ReST option lists actually don't
507 # support options like --1.14 so that causes syntax errors (in Sphinx568 # support options like --1.14 so that causes syntax errors (in Sphinx