argparse localization support
Peter Otten
__peter__ at web.de
Sun Aug 26 13:21:54 EDT 2012
Kwpolska wrote:
> I am using argparse in my project. I want to localize it, but it
> seems to be impossible to change some things. See this, for example:
>
> usage: trash [-h] [-V] [-e] [-l] [-r] [-v] [-w] [PLIK [PLIK ...]]
>
> Trashman — menedżer śmietnika XDG w Pythonie.
>
> positional arguments:
> PLIK pliki do wyrzucenia
>
> optional arguments:
> -h, --help show this help message and exit
> -V, --version show program's version number and exit
>
> (a snippet of help for Trashman, a Python trash manager, with
> LANG='pl_PL.UTF-8'; https://github.com/Kwpolska/trashman )
>
> Anyways, you can see that -h and -V (added semi-automatically by
> argparse), “usage:” and “positional/optional arguments:” are still in
> English. It doesn’t look very professional. Also, I want to use
> fancy UTF-8 characters. Is there a way to fix that?
I don't know much about gettext, but the following suggests that most
strings in argparse are properly wrapped:
$ cat localize_argparse.py
import gettext
def my_gettext(s):
return s.upper()
gettext.gettext = my_gettext
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-V", action="version")
args = parser.parse_args()
$ python localize_argparse.py -h
USAGE: localize_argparse.py [-h] [-V]
OPTIONAL ARGUMENTS:
-h, --help SHOW THIS HELP MESSAGE AND EXIT
-V show program's version number and exit
The workaround for the "-V" option would be to add the help message
explicitly
parser.add_argument("-V", ..., help=_("show..."))
You still have to provide all translations yourself.
More information about the Python-list
mailing list