Skip to content

Commit bb48a8b

Browse files
committed
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.
1 parent add7cbf commit bb48a8b

2 files changed

Lines changed: 23 additions & 14 deletions

File tree

Lib/argparse.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,8 +1079,9 @@ def __call__(self, parser, namespace, values, option_string=None):
10791079
try:
10801080
parser = self._name_parser_map[parser_name]
10811081
except KeyError:
1082-
tup = parser_name, ', '.join(self._name_parser_map)
1083-
msg = _('unknown parser %r (choices: %s)') % tup
1082+
args = {'parser_name': parser_name,
1083+
'choices': ', '.join(self._name_parser_map)}
1084+
msg = _('unknown parser %(parser_name)r (choices: %(choices)s)') % args
10841085
raise ArgumentError(self, msg)
10851086

10861087
# parse all the remaining options into the namespace
@@ -1380,10 +1381,11 @@ def _get_optional_kwargs(self, *args, **kwargs):
13801381
for option_string in args:
13811382
# error on strings that don't start with an appropriate prefix
13821383
if not option_string[0] in self.prefix_chars:
1383-
msg = _('invalid option string %r: '
1384-
'must start with a character %r')
1385-
tup = option_string, self.prefix_chars
1386-
raise ValueError(msg % tup)
1384+
args = {'option': option_string,
1385+
'prefix_chars': self.prefix_chars}
1386+
msg = _('invalid option string %(option)r: '
1387+
'must start with a character %(prefix_chars)r')
1388+
raise ValueError(msg % args)
13871389

13881390
# strings starting with two prefix characters are long options
13891391
option_strings.append(option_string)
@@ -2049,8 +2051,9 @@ def _parse_optional(self, arg_string):
20492051
if len(option_tuples) > 1:
20502052
options = ', '.join([option_string
20512053
for action, option_string, explicit_arg in option_tuples])
2052-
tup = arg_string, options
2053-
self.error(_('ambiguous option: %s could match %s') % tup)
2054+
args = {'option': arg_string, 'matches': options}
2055+
msg = _('ambiguous option: %(option)s could match %(matches)s')
2056+
self.error(msg % args)
20542057

20552058
# if exactly one action matched, this segmentation is good,
20562059
# so return the parsed action
@@ -2229,18 +2232,20 @@ def _get_value(self, action, arg_string):
22292232
# TypeErrors or ValueErrors also indicate errors
22302233
except (TypeError, ValueError):
22312234
name = getattr(action.type, '__name__', repr(action.type))
2232-
msg = _('invalid %s value: %r')
2233-
raise ArgumentError(action, msg % (name, arg_string))
2235+
args = {'type': name, 'value': arg_string}
2236+
msg = _('invalid %(type)s value: %(value)r')
2237+
raise ArgumentError(action, msg % args)
22342238

22352239
# return the converted value
22362240
return result
22372241

22382242
def _check_value(self, action, value):
22392243
# converted value must be one of the choices (if specified)
22402244
if action.choices is not None and value not in action.choices:
2241-
tup = value, ', '.join(map(repr, action.choices))
2242-
msg = _('invalid choice: %r (choose from %s)') % tup
2243-
raise ArgumentError(action, msg)
2245+
args = {'value': value,
2246+
'choices': ', '.join(map(repr, action.choices))}
2247+
msg = _('invalid choice: %(value)r (choose from %(choices)s)')
2248+
raise ArgumentError(action, msg % args)
22442249

22452250
# =======================
22462251
# Help-formatting methods
@@ -2332,4 +2337,5 @@ def error(self, message):
23322337
should either exit or raise an exception.
23332338
"""
23342339
self.print_usage(_sys.stderr)
2335-
self.exit(2, _('%s: error: %s\n') % (self.prog, message))
2340+
args = {'prog': self.prog, 'message': message}
2341+
self.exit(2, _('%(prog)s: error: %(message)s\n') % args)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Core and Builtins
3535
Library
3636
-------
3737

38+
- Issue #10528: Allow translators to reorder placeholders in localizable
39+
messages from argparse.
40+
3841
- Issue #10497: Fix incorrect use of gettext in argparse.
3942

4043
- Issue #10478: Reentrant calls inside buffered IO objects (for example by

0 commit comments

Comments
 (0)