Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Improve support for CLI translations using gettext #417

Merged
merged 5 commits into from Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 4 additions & 8 deletions typer/core.py
Expand Up @@ -162,7 +162,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 @@ -388,7 +388,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 @@ -620,15 +620,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
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