classification
Title: argparse uses %s in gettext calls
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: fixed
Dependencies: 10497 Superseder:
Assigned To: eric.araujo Nosy List: belopolsky, bethard, eric.araujo, eric.smith, georg.brandl
Priority: high Keywords: patch

Created on 2010-11-25 12:25 by eric.araujo, last changed 2010-12-03 19:42 by eric.araujo. This issue is now closed.

Files
File name Uploaded Description Edit
fix-gettext-positionals.diff eric.araujo, 2010-12-03 03:37
Messages (9)
msg122357 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-11-25 12:25
When you run xgettext other argparse.py, you get this warning:
“'msgid' format string with unnamed arguments cannot be properly localized: The translator cannot reorder the arguments.  Please consider using a format string with named arguments, and a mapping instead of a tuple for the arguments.”

I don’t know if people already rely on strings from argparse, but for safety I think we should change them only in 3.2, if Steven agrees.

(I have to check other stdlib modules.)
msg122406 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2010-11-25 20:36
I think it's fine to fix this in 3.2 by switching to mappings where necessary.
msg123124 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-02 21:47
Georg, do I have to make a patch before beta1 for this to go in 3.2?
msg123129 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2010-12-02 22:02
That would be good.  While you could argue it's a bug, the fix is also slightly incompatible.
msg123173 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-03 03:37
The incompatibility worries me a bit.  A program compatible with Python 3.1 and 3.2 would have to support both sets of messages; I don’t know whether the i18n tools support having all the strings in one PO file, and how inconvenient it would be for translators.  I assume such transitions have happened in many projects and that people just accept the inconvenience.

Attached patch changes the strings to avoid warnings.  No tests added (I intend to write tests for i18n in argparse in the future, see #10497 and #10529).

Steven, I’d like your +1 before committing anything.  One message in particular is not very helpful, but I’ve been unable to find better placeholders (it’s in ArgumentPaser._get_value):
  msg = _('invalid %(name)s value: %(arg_string)r')
msg123211 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2010-12-03 09:22
If I understand it right, before this patch, people couldn't really supply internationalizations for these calls - they would have had to have a translation for each possible value of, e.g. action.choices or parser.prefix_chars. So I think there's pretty minimal danger of breaking that code.

Specific comments on names in the patch:

* rename %(character) to %(prefix_chars)
* rename %(name) to %(type) and %(arg_string) to %(value)
* rename %(program_name) to %(prog)

Basically, we should use the name of an Action or ArgumentParser attribute whenever it makes sense. Hopefully that will make it more intuitive what these mean when people are translating them.

And yes, with the above changes, +1.
msg123213 - (view) Author: Steven Bethard (bethard) * (Python committer) Date: 2010-12-03 09:26
Hmm. I see I confused this with Issue 10529, where there really was a bug in the gettext calls. Nonetheless, +1 for switching from %s to %(xxx)s in 3.2 - since that's the first release in Python 3 that has argparse, I think it's really okay to do this.
msg123234 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-03 12:43
This bug is a warning, not a fatal error: “The translator cannot reorder the arguments.”  Depending on the language, it may be a real pain.

“Basically, we should use the name of an Action or ArgumentParser attribute whenever it makes sense. Hopefully that will make it more intuitive what these mean when people are translating them.”  Exactly.  That’s why I suggested %(program_name) instead of %(prog); keep in mind translators are not necessarily programmers, so the familiar argparse terms may not make sense to them.

If Georg is okay with the lack of tests (apart manual testing showing that xgettext does not print warnings anymore), I will apply your changes and commit this tonight, which should be in time for beta1.
msg123285 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-03 19:42
Committed in r86993.
History
Date User Action Args
2010-12-03 19:42:43eric.araujosetstatus: pending -> closed
resolution: fixed
messages: + msg123285

stage: commit review -> resolved
2010-12-03 12:43:34eric.araujosetstatus: open -> pending
priority: normal -> high
messages: + msg123234

dependencies: + Incorrect use of gettext in argparse
stage: needs patch -> commit review
2010-12-03 09:26:07bethardsetmessages: + msg123213
2010-12-03 09:22:34bethardsetmessages: + msg123211
2010-12-03 03:37:40eric.araujosetfiles: + fix-gettext-positionals.diff
keywords: + patch
messages: + msg123173
2010-12-02 22:02:53georg.brandlsetmessages: + msg123129
2010-12-02 21:47:46eric.araujosetnosy: + georg.brandl
messages: + msg123124
2010-11-26 05:33:55eric.araujosetassignee: eric.araujo
stage: needs patch
2010-11-25 20:36:52bethardsetmessages: + msg122406
2010-11-25 12:37:44eric.smithsetnosy: + eric.smith
2010-11-25 12:25:37eric.araujocreate