Skip to content

Commit

Permalink
✨ Improve support for CLI translations uisng gettext (#417)
Browse files Browse the repository at this point in the history
Co-authored-by: Marcin Jodłowiec <superyode@gmail.com>
Co-authored-by: svlandeg <svlandeg@github.com>
  • Loading branch information
3 people authored Mar 23, 2024
1 parent 5728b2a commit fe9538c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
12 changes: 4 additions & 8 deletions typer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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}]"
Expand Down Expand Up @@ -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)


Expand Down
19 changes: 10 additions & 9 deletions typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit fe9538c

Please sign in to comment.