Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

argparse doesn't offer localization interface for "version" action #60990

Open
thorsten mannequin opened this issue Dec 26, 2012 · 5 comments
Open

argparse doesn't offer localization interface for "version" action #60990

thorsten mannequin opened this issue Dec 26, 2012 · 5 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@thorsten
Copy link
Mannequin

thorsten mannequin commented Dec 26, 2012

BPO 16786
Nosy @jdetrey
PRs
  • gh-77956: Add the words 'default' and 'version' help text localizable #12711
  • Files
  • patch_1.diff
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2012-12-26.13:19:58.143>
    labels = ['type-bug', '3.8', '3.9', '3.10', '3.11', '3.7', 'library']
    title = 'argparse doesn\'t offer localization interface for "version" action'
    updated_at = <Date 2021-08-08.13:35:06.519>
    user = 'https://bugs.python.org/thorsten'

    bugs.python.org fields:

    activity = <Date 2021-08-08.13:35:06.519>
    actor = 'jdetrey'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2012-12-26.13:19:58.143>
    creator = 'thorsten'
    dependencies = []
    files = ['35950']
    hgrepos = []
    issue_num = 16786
    keywords = ['patch']
    message_count = 5.0
    messages = ['178209', '222932', '222997', '236350', '399214']
    nosy_count = 5.0
    nosy_names = ['bethard', 'thorsten', 'paul.j3', 'jdetrey', 'proski']
    pr_nums = ['12711']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue16786'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10', 'Python 3.11']

    @thorsten
    Copy link
    Mannequin Author

    thorsten mannequin commented Dec 26, 2012

    The - deprecated - "version" keyword for argparse.ArgumentParser allowed for localization of the "show program's version number and exit" help text for -v/--version (output of "-h"/"--help")

    The new version action for add_argument does not allow this - resulting in a partially translated output for the -v/--version option.

    @thorsten thorsten mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Dec 26, 2012
    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Jul 13, 2014

    @thorsten sorry about the delay involved here.

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Jul 14, 2014

    In this patch I added the '_()' localization to the '_VersionAction' default 'help'.

    While this is a straight forward change, I haven't tested it. It does run test_argparse.py, but that doesn't have any tests for localization.

    According to the discussion here:
    https://mail.python.org/pipermail/python-dev/2010-May/100215.html
    this default help was originally None, and adding this string was seen
    as a worthwhile convenience. Localization was not considered.

    Does this use of _() really save the user effort? May be it would if they are already using the depricated version. Otherwise they could just give the version argument their own non default help line. I haven't used localization enough to know.

    @proski
    Copy link
    Mannequin

    proski mannequin commented Feb 21, 2015

    I have tested the patch. It fixes the problem for me.

    You are right, new programs would just supply translated help to the version action. No effort would be saved.

    But the programs updated from the deprecated syntax may rely on a separate string list for translating strings found in argparse.py. They would lose the translation for the "--version" help without any warning.
    Debugging and fixing that would take some effort.

    @jdetrey
    Copy link
    Mannequin

    jdetrey mannequin commented Aug 8, 2021

    Dear all,

    As commented on PR 12711 (#12711 (review)), there is a slight issue with the proposed patch, as it translates the --version help string as soon as the argparse module is imported (at which point the programmer might not have correctly initialized the gettext global domain yet). The suggested modification of PR 12711 should fix this, so that the translation only happens when an instance of _VersionAction is actually created:

    diff a/Lib/argparse.py b/Lib/argparse.py
    --- a/Lib/argparse.py
    +++ b/Lib/argparse.py
    @@ -1042,7 +1042,9 @@ def __init__(self,
                      version=None,
                      dest=SUPPRESS,
                      default=SUPPRESS,
    -                 help="show program's version number and exit"):
    +                 help=None):
    +        if help is None:
    +            help = _("show program's version number and exit")
             super(_VersionAction, self).__init__(
                 option_strings=option_strings,
                 dest=dest,
    

    However, I'm not sure I understand correctly Pavel's comment as to why merging this patch would make some old programs lose their translation for this string: since argparse does not itself provide any translation, it is up to the programmers to provide gettext translations for argparse strings as well. Adding the call to _() here seems harmless to me:

    • if a program already has a gettext translation string for "show program's version number and exit", it will be used, as expected;
    • otherwise, if it hasn't (and relies on other mechanisms to translate this string), the string will remain untranslated, and the "custom" translation mechanisms will be able to process it as before.

    In any case, my guess is that localized programs already explicitly pass a localized help string to the _VersionAction constructor in order to circumvent the nonlocalized default help string:

    parser.add_argument('-V', action='version', version='%(prog)s 3.9',
                        help=_("show program's version number and exit"))
    

    These programs should also remain completely unaffected by this change.

    Thanks!

    Kind regards,
    Jérémie.

    @jdetrey jdetrey mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes labels Aug 8, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    Status: Bugs
    Status: No status
    Development

    No branches or pull requests

    0 participants