Skip to content

Provide i18n with gettext #303

Closed

Description

@evgeny-p

It would be useful to wrap every public message with gettext function to allow i18n for application.

Activity

  1. b1kers commented on Mar 2, 2015

    @b1kers
  2. lapause commented on Jun 9, 2015

    @lapause

    +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...

  3. untitaker commented on Jun 10, 2015

    @untitaker
    Contributor
    1. 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.
    2. I think you're supposed to subclass Command so you can override main properly, and then pass the subclass like @click.command(cls=TranslatingCommand).
  4. Un1oR commented on Oct 7, 2015

    @Un1oR
  5. warsaw commented on Jun 17, 2017

    @warsaw

    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. ;)

  6. thechief389 commented on Dec 30, 2018

    @thechief389

    Should this be implemented in click itself or as part of click-contrib?

    Also, command() and group() have help parameters so you could do

    @click.command(cls=TranslatingCommand, help=_('translatable string'))
    def func():
        # No docstring here.
        pass
  7. davidism commented on Dec 31, 2018

    @davidism
    Member

    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.

  8. thechief389 commented on Jan 1, 2019

    @thechief389

    What I meant was should this be built into click or should there be a separate library?

  9. davidism commented on Jan 1, 2019

    @davidism
    Member

    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.

  10. thechief389 commented on Jan 1, 2019

    @thechief389
  11. davidism commented on Mar 24, 2021

    @davidism
    Member

    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.

  12. Saif807380 commented on Mar 31, 2021

    @Saif807380
    Contributor

    I'd like to work on this.

  13. added this to the 8.0.0 milestone on Apr 3, 2021
  14. davidism commented on Apr 3, 2021

    @davidism
    Member

    #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.

  15. ThiefMaster commented on Apr 3, 2021

    @ThiefMaster
    Member

    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?

  16. davidism commented on Apr 3, 2021

    @davidism
    Member

    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.

  17. locked as resolved and limited conversation to collaborators on Apr 18, 2021
  18. added
    help output
    on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

help output

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions