From fe9538cdfbff80e34bc9ee05b074ab009c527661 Mon Sep 17 00:00:00 2001 From: mjod <62674781+mjodmj@users.noreply.github.com> Date: Sat, 23 Mar 2024 18:04:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Improve=20support=20for=20CLI=20tra?= =?UTF-8?q?nslations=20uisng=20gettext=20(#417)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marcin Jodłowiec Co-authored-by: svlandeg --- typer/core.py | 12 ++++-------- typer/rich_utils.py | 19 ++++++++++--------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/typer/core.py b/typer/core.py index 8801c3dafa..ec17bbcf83 100644 --- a/typer/core.py +++ b/typer/core.py @@ -170,7 +170,7 @@ def _extract_default_help_str( default_value = obj.get_default(ctx, call=False) else: if inspect.isfunction(obj.default): - default_value = "(dynamic)" + default_value = _("(dynamic)") else: default_value = obj.default finally: @@ -396,7 +396,7 @@ def get_help_record(self, ctx: click.Context) -> Optional[Tuple[str, str]]: if default_string: extra.append(_("default: {default}").format(default=default_string)) if self.required: - extra.append("required") + extra.append(_("required")) if extra: extra_str = ";".join(extra) help = f"{help} [{extra_str}]" if help else f"[{extra_str}]" @@ -628,15 +628,11 @@ def _typer_format_options( elif param.param_type_name == "option": opts.append(rv) - # TODO: explore adding Click's gettext support, e.g.: - # from gettext import gettext as _ - # with formatter.section(_("Options")): - # ... if args: - with formatter.section("Arguments"): + with formatter.section(_("Arguments")): formatter.write_dl(args) if opts: - with formatter.section("Options"): + with formatter.section(_("Options")): formatter.write_dl(opts) diff --git a/typer/rich_utils.py b/typer/rich_utils.py index fdb61b6719..51da5ca6ea 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -3,6 +3,7 @@ import inspect import sys from collections import defaultdict +from gettext import gettext as _ from os import getenv from typing import Any, DefaultDict, Dict, Iterable, List, Optional, Union @@ -80,17 +81,17 @@ FORCE_TERMINAL = False # Fixed strings -DEPRECATED_STRING = "(deprecated) " -DEFAULT_STRING = "[default: {}]" -ENVVAR_STRING = "[env var: {}]" +DEPRECATED_STRING = _("(deprecated) ") +DEFAULT_STRING = _("[default: {}]") +ENVVAR_STRING = _("[env var: {}]") REQUIRED_SHORT_STRING = "*" -REQUIRED_LONG_STRING = "[required]" +REQUIRED_LONG_STRING = _("[required]") RANGE_STRING = " [{}]" -ARGUMENTS_PANEL_TITLE = "Arguments" -OPTIONS_PANEL_TITLE = "Options" -COMMANDS_PANEL_TITLE = "Commands" -ERRORS_PANEL_TITLE = "Error" -ABORTED_TEXT = "Aborted." +ARGUMENTS_PANEL_TITLE = _("Arguments") +OPTIONS_PANEL_TITLE = _("Options") +COMMANDS_PANEL_TITLE = _("Commands") +ERRORS_PANEL_TITLE = _("Error") +ABORTED_TEXT = _("Aborted.") MARKUP_MODE_MARKDOWN = "markdown" MARKUP_MODE_RICH = "rich"