Kaydet (Commit) bb48a8b5 authored tarafından Éric Araujo's avatar Éric Araujo

Allow translators to reorder placeholders in localizable messages from

argparse (#10528).

There is no unit test; I checked with xgettext that no more warnings
were emitted.  Steven approved the change.
üst add7cbfb
...@@ -1079,8 +1079,9 @@ class _SubParsersAction(Action): ...@@ -1079,8 +1079,9 @@ class _SubParsersAction(Action):
try: try:
parser = self._name_parser_map[parser_name] parser = self._name_parser_map[parser_name]
except KeyError: except KeyError:
tup = parser_name, ', '.join(self._name_parser_map) args = {'parser_name': parser_name,
msg = _('unknown parser %r (choices: %s)') % tup 'choices': ', '.join(self._name_parser_map)}
msg = _('unknown parser %(parser_name)r (choices: %(choices)s)') % args
raise ArgumentError(self, msg) raise ArgumentError(self, msg)
# parse all the remaining options into the namespace # parse all the remaining options into the namespace
...@@ -1380,10 +1381,11 @@ class _ActionsContainer(object): ...@@ -1380,10 +1381,11 @@ class _ActionsContainer(object):
for option_string in args: for option_string in args:
# error on strings that don't start with an appropriate prefix # error on strings that don't start with an appropriate prefix
if not option_string[0] in self.prefix_chars: if not option_string[0] in self.prefix_chars:
msg = _('invalid option string %r: ' args = {'option': option_string,
'must start with a character %r') 'prefix_chars': self.prefix_chars}
tup = option_string, self.prefix_chars msg = _('invalid option string %(option)r: '
raise ValueError(msg % tup) 'must start with a character %(prefix_chars)r')
raise ValueError(msg % args)
# strings starting with two prefix characters are long options # strings starting with two prefix characters are long options
option_strings.append(option_string) option_strings.append(option_string)
...@@ -2049,8 +2051,9 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): ...@@ -2049,8 +2051,9 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
if len(option_tuples) > 1: if len(option_tuples) > 1:
options = ', '.join([option_string options = ', '.join([option_string
for action, option_string, explicit_arg in option_tuples]) for action, option_string, explicit_arg in option_tuples])
tup = arg_string, options args = {'option': arg_string, 'matches': options}
self.error(_('ambiguous option: %s could match %s') % tup) msg = _('ambiguous option: %(option)s could match %(matches)s')
self.error(msg % args)
# if exactly one action matched, this segmentation is good, # if exactly one action matched, this segmentation is good,
# so return the parsed action # so return the parsed action
...@@ -2229,8 +2232,9 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): ...@@ -2229,8 +2232,9 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
# TypeErrors or ValueErrors also indicate errors # TypeErrors or ValueErrors also indicate errors
except (TypeError, ValueError): except (TypeError, ValueError):
name = getattr(action.type, '__name__', repr(action.type)) name = getattr(action.type, '__name__', repr(action.type))
msg = _('invalid %s value: %r') args = {'type': name, 'value': arg_string}
raise ArgumentError(action, msg % (name, arg_string)) msg = _('invalid %(type)s value: %(value)r')
raise ArgumentError(action, msg % args)
# return the converted value # return the converted value
return result return result
...@@ -2238,9 +2242,10 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): ...@@ -2238,9 +2242,10 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
def _check_value(self, action, value): def _check_value(self, action, value):
# converted value must be one of the choices (if specified) # converted value must be one of the choices (if specified)
if action.choices is not None and value not in action.choices: if action.choices is not None and value not in action.choices:
tup = value, ', '.join(map(repr, action.choices)) args = {'value': value,
msg = _('invalid choice: %r (choose from %s)') % tup 'choices': ', '.join(map(repr, action.choices))}
raise ArgumentError(action, msg) msg = _('invalid choice: %(value)r (choose from %(choices)s)')
raise ArgumentError(action, msg % args)
# ======================= # =======================
# Help-formatting methods # Help-formatting methods
...@@ -2332,4 +2337,5 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer): ...@@ -2332,4 +2337,5 @@ class ArgumentParser(_AttributeHolder, _ActionsContainer):
should either exit or raise an exception. should either exit or raise an exception.
""" """
self.print_usage(_sys.stderr) self.print_usage(_sys.stderr)
self.exit(2, _('%s: error: %s\n') % (self.prog, message)) args = {'prog': self.prog, 'message': message}
self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
...@@ -35,6 +35,9 @@ Core and Builtins ...@@ -35,6 +35,9 @@ Core and Builtins
Library Library
------- -------
- Issue #10528: Allow translators to reorder placeholders in localizable
messages from argparse.
- Issue #10497: Fix incorrect use of gettext in argparse. - Issue #10497: Fix incorrect use of gettext in argparse.
- Issue #10478: Reentrant calls inside buffered IO objects (for example by - Issue #10478: Reentrant calls inside buffered IO objects (for example by
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment