You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For those interrested, I've made a gist showing how to catch click exceptions and translate them.
It's quick and dirty (use regexps), but right now it does the job:
To core developers: if you don't want to implement i18n, you could create one exception class by error type (±3 inheriting from UsageError, ±14 from BadParameter) and use attributes to store dynamic elements (parameter and file names/types.... By doing so it would allow developers to just trap exceptions of interrest to them and re-raise them with customized messages without having to mess with regexps depending of the exact messages used by the library.
Agreed, I'd like to see this too. However, I suppose this would also require a much more fine-grained exception hierarchy, one class for each error message. Right now some parts of click raise such generic ones as RuntimeError.
I think you're supposed to subclass Command so you can override main properly, and then pass the subclass like @click.command(cls=TranslatingCommand).
I'm playing around with click in Mailman 3 to replace our homegrown cli parsing code. All of our help text is marked with _() and we use flufl.i18n to do the actual run time translations. I may have more opinions as I get farther along, but for now I don't see any real problem with option help text. E.g. this works fine:
@click.option('-v', '--verbose',is_flag=True, default=False,help=_('Display more debugging information to the log file.'))
It gets tricky in one place: use of the function's docstring for the help prologue. Python only assigns unadorned strings to __doc__. So there's no good way to mark the help prologue for translation, extraction and also use it in click.
Of course, I might be missing a lot as I've just started digging in. ;)
This issue appears to be asking for i18n on Click's internal messages, such as exception strings. There should be nothing preventing use of i18n on your own strings.
What you're showing isn't related to what's being asked for. Nothing has to change for what you're showing. Internal message have to be wrapped for what's being asked for.
#1829 wraps strings that Click shows to a CLI application user with gettext / ngettext. It does not wrap error messages intended for developers. It does not provide any translation files (pot, po, mo).
A project can now translate Click's strings along with their own by generating a pot file, such as with Babel: pip install click; pybabel -o click.pot path/to/click, then combining this catalog with their other message catalogs when loading their translations.
There are still some strings, such as the parameter type in help text (text, integer, etc.), that are not wrapped because they're defined at import time and would require a more complex setup to mark the string for translation then translate it from a variable later at runtime.
From reading the Python docs, it seems like we should be using gettext.translations("click").gettext() instead of the top-level gettext.gettext() to keep our messages out of the default domain. However, as soon as you do that, Python expects the mo files to actually exist. It seems like at that point Click needs to provide an API, probably on click.Context, for specifying where translations are and possibly to override the user's language as well.
From my experience with WTForms, maintaining the translations vastly complicates things. PRs come in at random making changes that are impossible to validate unless we have other trusted speakers of the language. Writing the API to manage translations at runtime is also complicated, I'm not sure if WTForms is a good example or not, and Django's implementation is even more complex.
In conclusion, I think this is good enough for now. It allows a project that wants to provide translations for their CLI to translate messages output by Click, without adding more maintenance to Click at this time.
I think ideally click shipped its own translations.. maybe we could have a public project on weblate or transifex to crowd-source the actual translations?
We should probably discuss that in a maintainer meeting. I don't have the bandwidth to maintain that, or even figure out how to implement it, so someone else would need to volunteer to do it long term. From maintaining WTForms, I know it's not straightforward to make sure everything's available to contributors installing locally and when pushing builds.
Activity
b1kers commentedon Mar 2, 2015
lapause commentedon Jun 9, 2015
+1
For those interrested, I've made a gist showing how to catch click exceptions and translate them.
It's quick and dirty (use regexps), but right now it does the job:
https://gist.github.com/lapause/ed148b2b1481f83f39c5
To core developers: if you don't want to implement i18n, you could create one exception class by error type (±3 inheriting from UsageError, ±14 from BadParameter) and use attributes to store dynamic elements (parameter and file names/types.... By doing so it would allow developers to just trap exceptions of interrest to them and re-raise them with customized messages without having to mess with regexps depending of the exact messages used by the library.
To core developers (2): I'm pretty sure my monkey patching of the click.Command.main is not the best way to go, but http://click.pocoo.org/4/exceptions/ could really benefit from a real life example of how to invoke directly a command declared with multiple arguments, decorators...
untitaker commentedon Jun 10, 2015
RuntimeError.Commandso you can overridemainproperly, and then pass the subclass like@click.command(cls=TranslatingCommand).Un1oR commentedon Oct 7, 2015
warsaw commentedon Jun 17, 2017
I'm playing around with click in Mailman 3 to replace our homegrown cli parsing code. All of our help text is marked with
_()and we use flufl.i18n to do the actual run time translations. I may have more opinions as I get farther along, but for now I don't see any real problem with option help text. E.g. this works fine:It gets tricky in one place: use of the function's docstring for the help prologue. Python only assigns unadorned strings to
__doc__. So there's no good way to mark the help prologue for translation, extraction and also use it in click.Of course, I might be missing a lot as I've just started digging in. ;)
thechief389 commentedon Dec 30, 2018
Should this be implemented in click itself or as part of click-contrib?
Also,
command()andgroup()havehelpparameters so you could dodavidism commentedon Dec 31, 2018
This issue appears to be asking for i18n on Click's internal messages, such as exception strings. There should be nothing preventing use of i18n on your own strings.
thechief389 commentedon Jan 1, 2019
What I meant was should this be built into click or should there be a separate library?
davidism commentedon Jan 1, 2019
What you're showing isn't related to what's being asked for. Nothing has to change for what you're showing. Internal message have to be wrapped for what's being asked for.
thechief389 commentedon Jan 1, 2019
davidism commentedon Mar 24, 2021
So I think what needs to be done is to wrap messages that the user might see with
_(gettext) and leave it at that for now.Saif807380 commentedon Mar 31, 2021
I'd like to work on this.
davidism commentedon Apr 3, 2021
#1829 wraps strings that Click shows to a CLI application user with
gettext/ngettext. It does not wrap error messages intended for developers. It does not provide any translation files (pot,po,mo).A project can now translate Click's strings along with their own by generating a pot file, such as with Babel:
pip install click; pybabel -o click.pot path/to/click, then combining this catalog with their other message catalogs when loading their translations.There are still some strings, such as the parameter type in help text (text, integer, etc.), that are not wrapped because they're defined at import time and would require a more complex setup to mark the string for translation then translate it from a variable later at runtime.
From reading the Python docs, it seems like we should be using
gettext.translations("click").gettext()instead of the top-levelgettext.gettext()to keep our messages out of the default domain. However, as soon as you do that, Python expects themofiles to actually exist. It seems like at that point Click needs to provide an API, probably onclick.Context, for specifying where translations are and possibly to override the user's language as well.From my experience with WTForms, maintaining the translations vastly complicates things. PRs come in at random making changes that are impossible to validate unless we have other trusted speakers of the language. Writing the API to manage translations at runtime is also complicated, I'm not sure if WTForms is a good example or not, and Django's implementation is even more complex.
In conclusion, I think this is good enough for now. It allows a project that wants to provide translations for their CLI to translate messages output by Click, without adding more maintenance to Click at this time.
ThiefMaster commentedon Apr 3, 2021
I think ideally click shipped its own translations.. maybe we could have a public project on weblate or transifex to crowd-source the actual translations?
davidism commentedon Apr 3, 2021
We should probably discuss that in a maintainer meeting. I don't have the bandwidth to maintain that, or even figure out how to implement it, so someone else would need to volunteer to do it long term. From maintaining WTForms, I know it's not straightforward to make sure everything's available to contributors installing locally and when pushing builds.