From 7de3c86c5a793485d7b686ac80448336ae996689 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Tue, 5 May 2026 08:03:07 -0700 Subject: [PATCH] feat(i18n): add display.language for static message translation (zh/ja/de/es) (#20231) * revert(gateway): remove stale-code self-check and auto-restart Removes the _detect_stale_code / _trigger_stale_code_restart mechanism introduced in #17648 and iterated in #19740. On every incoming message the gateway compared the boot-time git HEAD SHA to the current SHA on disk, and if they differed it would reply with Gateway code was updated in the background -- restarting this gateway so your next message runs on the new code. Please retry in a moment. and then kick off a graceful restart. This is unwanted behaviour: users who run a long-lived gateway and do their own ad-hoc git operations on the checkout end up with their chat interrupted and the current message dropped every time HEAD moves, with no way to opt out. If an operator really needs the old protection against stale sys.modules after "hermes update", the SIGKILL-survivor sweep in hermes update (hermes_cli/main.py, also tagged #17648) already handles the supervisor-respawn case on its own. Removed: gateway/run.py: - _STALE_CODE_SENTINELS, _GIT_SHA_CACHE_TTL_SECS - _read_git_head_sha(), _compute_repo_mtime() module helpers - class-level _boot_wall_time / _boot_repo_mtime / _boot_git_sha / _stale_code_restart_triggered defaults - __init__ boot-snapshot block (_boot_*, _cached_current_sha*, _repo_root_for_staleness, _stale_code_notified) - _current_git_sha_cached(), _detect_stale_code(), _trigger_stale_code_restart() methods - stale-code check + user-facing restart notice at the top of _handle_message() tests/gateway/test_stale_code_self_check.py (deleted, 412 lines) No new logic added. Zero remaining references to any removed symbol. Gateway test suite passes the same 4589 tests it passed before; the 3 pre-existing unrelated failures (discord free-channel, feishu bot admission, teams typing) are unchanged by this commit. * feat(i18n): add display.language for static message translation (zh/ja/de/es) Adds a thin-slice i18n layer covering the highest-impact static user-facing messages: the CLI dangerous-command approval prompt and a handful of gateway slash-command replies (restart-drain, goal cleared, approval expired, config read/save errors). Out of scope (stays English): agent responses, log lines, tool outputs, slash-command descriptions, error tracebacks. Infrastructure: - agent/i18n.py: catalog loader, t() helper, language resolution (HERMES_LANGUAGE env var > display.language config > en) - locales/{en,zh,ja,de,es}.yaml: ~19 translated strings per language - display.language in DEFAULT_CONFIG (hermes_cli/config.py) Tests: - tests/agent/test_i18n.py: 21 tests covering catalog parity, placeholder parity across locales, fallback behavior, env-var override, alias normalization, missing-key graceful degradation. Docs: - website/docs/user-guide/configuration.md: display.language entry plus a short section explaining scope so users don't expect agent responses to translate via this knob. --- agent/i18n.py | 230 +++++++++++++++++++++++ gateway/run.py | 13 +- hermes_cli/config.py | 5 + locales/de.yaml | 24 +++ locales/en.yaml | 35 ++++ locales/es.yaml | 24 +++ locales/ja.yaml | 24 +++ locales/zh.yaml | 24 +++ tests/agent/test_i18n.py | 156 +++++++++++++++ tools/approval.py | 25 +-- website/docs/user-guide/configuration.md | 14 ++ 11 files changed, 557 insertions(+), 17 deletions(-) create mode 100644 agent/i18n.py create mode 100644 locales/de.yaml create mode 100644 locales/en.yaml create mode 100644 locales/es.yaml create mode 100644 locales/ja.yaml create mode 100644 locales/zh.yaml create mode 100644 tests/agent/test_i18n.py diff --git a/agent/i18n.py b/agent/i18n.py new file mode 100644 index 0000000000..98d7ebce9a --- /dev/null +++ b/agent/i18n.py @@ -0,0 +1,230 @@ +"""Lightweight internationalization (i18n) for Hermes static user-facing messages. + +Scope (thin slice, by design): only the highest-impact static strings shown +to the user by Hermes itself -- approval prompts, a handful of gateway slash +command replies, restart-drain notices. Agent-generated output, log lines, +error tracebacks, tool outputs, and slash-command descriptions all stay in +English. + +Catalog files live under ``locales/.yaml`` at the repo root. Each +catalog is a flat dict keyed by dotted paths (e.g. ``approval.choose`` or +``gateway.approval_expired``). Missing keys fall back to English; if English +is missing too, the key path itself is returned so a broken catalog never +crashes the agent. + +Usage:: + + from agent.i18n import t + print(t("approval.choose_long")) # current lang + print(t("gateway.draining", count=3)) # {count} formatted + print(t("approval.choose_long", lang="zh")) # explicit override + +Language resolution order: + 1. Explicit ``lang=`` argument passed to :func:`t` + 2. ``HERMES_LANGUAGE`` environment variable (for tests / quick override) + 3. ``display.language`` from config.yaml + 4. ``"en"`` (baseline) + +Supported languages: en, zh, ja, de, es. Unknown values fall back to en. +""" + +from __future__ import annotations + +import logging +import os +import threading +from functools import lru_cache +from pathlib import Path +from typing import Any + +logger = logging.getLogger(__name__) + +SUPPORTED_LANGUAGES: tuple[str, ...] = ("en", "zh", "ja", "de", "es") +DEFAULT_LANGUAGE = "en" + +# Accept a few natural aliases so users who type "chinese" / "zh-CN" / "jp" +# get the right catalog instead of silently falling back to English. +_LANGUAGE_ALIASES: dict[str, str] = { + "english": "en", "en-us": "en", "en-gb": "en", + "chinese": "zh", "mandarin": "zh", "zh-cn": "zh", "zh-tw": "zh", "zh-hans": "zh", "zh-hant": "zh", + "japanese": "ja", "jp": "ja", "ja-jp": "ja", + "german": "de", "deutsch": "de", "de-de": "de", + "spanish": "es", "español": "es", "espanol": "es", "es-es": "es", "es-mx": "es", +} + +_catalog_cache: dict[str, dict[str, str]] = {} +_catalog_lock = threading.Lock() + + +def _locales_dir() -> Path: + """Return the directory containing locale YAML files. + + Lives next to the repo root so both the bundled install and editable + checkouts find it without PYTHONPATH gymnastics. + """ + # agent/i18n.py -> agent/ -> repo root + return Path(__file__).resolve().parent.parent / "locales" + + +def _normalize_lang(value: Any) -> str: + """Normalize a user-supplied language value to a supported code. + + Accepts supported codes directly, common aliases (``chinese`` -> ``zh``), + and case-insensitive regional tags (``zh-CN`` -> ``zh``). Returns the + default language for unknown values. + """ + if not isinstance(value, str): + return DEFAULT_LANGUAGE + key = value.strip().lower() + if not key: + return DEFAULT_LANGUAGE + if key in SUPPORTED_LANGUAGES: + return key + if key in _LANGUAGE_ALIASES: + return _LANGUAGE_ALIASES[key] + # Try stripping a region suffix (e.g. "pt-br" -> "pt" won't be supported, + # but "zh-CN" -> "zh" will). + base = key.split("-", 1)[0] + if base in SUPPORTED_LANGUAGES: + return base + return DEFAULT_LANGUAGE + + +def _load_catalog(lang: str) -> dict[str, str]: + """Load and flatten one locale YAML file into a dotted-key dict. + + YAML files can be nested for human readability; this produces the flat + key space :func:`t` expects. Cached per-language for the process. + """ + with _catalog_lock: + cached = _catalog_cache.get(lang) + if cached is not None: + return cached + + path = _locales_dir() / f"{lang}.yaml" + if not path.is_file(): + logger.debug("i18n catalog missing for %s at %s", lang, path) + with _catalog_lock: + _catalog_cache[lang] = {} + return {} + + try: + import yaml # PyYAML is already a hermes dependency + with path.open("r", encoding="utf-8") as f: + raw = yaml.safe_load(f) or {} + except Exception as exc: + logger.warning("Failed to load i18n catalog %s: %s", path, exc) + with _catalog_lock: + _catalog_cache[lang] = {} + return {} + + flat: dict[str, str] = {} + _flatten_into(raw, "", flat) + with _catalog_lock: + _catalog_cache[lang] = flat + return flat + + +def _flatten_into(node: Any, prefix: str, out: dict[str, str]) -> None: + if isinstance(node, dict): + for key, value in node.items(): + child_key = f"{prefix}.{key}" if prefix else str(key) + _flatten_into(value, child_key, out) + elif isinstance(node, str): + out[prefix] = node + # Non-string, non-dict leaves are ignored -- catalogs are text-only. + + +@lru_cache(maxsize=1) +def _config_language_cached() -> str | None: + """Read ``display.language`` from config.yaml once per process. + + Cached because ``t()`` is called in hot paths (every approval prompt, + every gateway reply) and re-reading YAML each call would be wasteful. + ``reset_language_cache()`` clears this when config changes at runtime + (e.g. after the setup wizard). + """ + try: + from hermes_cli.config import load_config + cfg = load_config() + lang = (cfg.get("display") or {}).get("language") + if lang: + return _normalize_lang(lang) + except Exception as exc: + logger.debug("Could not read display.language from config: %s", exc) + return None + + +def reset_language_cache() -> None: + """Invalidate cached language resolution and catalogs. + + Call after :func:`hermes_cli.config.save_config` if a running process + needs to pick up a changed ``display.language`` without restart. + """ + _config_language_cached.cache_clear() + with _catalog_lock: + _catalog_cache.clear() + + +def get_language() -> str: + """Resolve the active language using env > config > default order.""" + env_lang = os.environ.get("HERMES_LANGUAGE") + if env_lang: + return _normalize_lang(env_lang) + cfg_lang = _config_language_cached() + if cfg_lang: + return cfg_lang + return DEFAULT_LANGUAGE + + +def t(key: str, lang: str | None = None, **format_kwargs: Any) -> str: + """Translate a dotted key to the active language. + + Parameters + ---------- + key + Dotted path into the catalog, e.g. ``"approval.choose_long"``. + lang + Explicit language override. Takes precedence over env + config. + **format_kwargs + ``str.format`` substitution arguments (``t("gateway.drain", count=3)`` + expects a catalog entry with a ``{count}`` placeholder). + + Returns + ------- + The translated string, or the English fallback if the key is missing in + the target language, or the bare key if English is also missing. + """ + target = _normalize_lang(lang) if lang else get_language() + catalog = _load_catalog(target) + value = catalog.get(key) + + if value is None and target != DEFAULT_LANGUAGE: + # Fall through to English rather than showing a key path to the user. + value = _load_catalog(DEFAULT_LANGUAGE).get(key) + + if value is None: + # Last-ditch: return the key itself. A broken catalog should not + # crash anything; it just looks ugly until someone fixes it. + logger.debug("i18n miss: key=%r lang=%r", key, target) + value = key + + if format_kwargs: + try: + return value.format(**format_kwargs) + except (KeyError, IndexError, ValueError) as exc: + logger.warning( + "i18n format failed for key=%r lang=%r kwargs=%r: %s", + key, target, format_kwargs, exc, + ) + return value + return value + + +__all__ = [ + "SUPPORTED_LANGUAGES", + "DEFAULT_LANGUAGE", + "t", + "get_language", + "reset_language_cache", +] diff --git a/gateway/run.py b/gateway/run.py index 433b41387f..c0a6ae0bb1 100644 --- a/gateway/run.py +++ b/gateway/run.py @@ -39,6 +39,7 @@ from typing import Dict, Optional, Any, List, Union # gateway is a long-running daemon, so its boot cost matters less than # preserving the established test-patch surface. from agent.account_usage import fetch_account_usage, render_account_usage_lines +from agent.i18n import t from hermes_cli.config import cfg_get # --- Agent cache tuning --------------------------------------------------- @@ -7377,7 +7378,7 @@ class GatewayRunner: if self._restart_requested or self._draining: count = self._running_agent_count() if count: - return f"⏳ Draining {count} active agent(s) before restart..." + return t("gateway.draining", count=count) return EphemeralReply("⏳ Gateway restart already in progress...") # Save the requester's routing info so the new gateway process can @@ -7429,7 +7430,7 @@ class GatewayRunner: else: self.request_restart(detached=True, via_service=False) if active_agents: - return f"⏳ Draining {active_agents} active agent(s) before restart..." + return t("gateway.draining", count=active_agents) return EphemeralReply("♻ Restarting gateway. If you aren't notified within 60 seconds, restart from the console with `hermes gateway restart`.") def _is_stale_restart_redelivery(self, event: MessageEvent) -> bool: @@ -8099,7 +8100,7 @@ class GatewayRunner: if lower in ("clear", "stop", "done"): had = mgr.has_goal() mgr.clear() - return "✓ Goal cleared." if had else "No active goal." + return t("gateway.goal_cleared") if had else t("gateway.no_active_goal") # Otherwise — treat the remaining text as the new goal. try: @@ -9317,7 +9318,7 @@ class GatewayRunner: try: user_config: dict = _load_gateway_config() except Exception as e: - return f"⚠️ Could not read config.yaml: {e}" + return t("gateway.config_read_failed", error=e) effective = resolve_footer_config(user_config, platform_key) @@ -9350,7 +9351,7 @@ class GatewayRunner: atomic_yaml_write(config_path, user_config) except Exception as e: logger.warning("Failed to save runtime_footer.enabled: %s", e) - return f"⚠️ Could not save config: {e}" + return t("gateway.config_save_failed", error=e) state = "ON" if new_state else "OFF" example = "" @@ -10788,7 +10789,7 @@ class GatewayRunner: if not has_blocking_approval(session_key): if session_key in self._pending_approvals: self._pending_approvals.pop(session_key) - return "⚠️ Approval expired (agent is no longer waiting). Ask the agent to try again." + return t("gateway.approval_expired") return "No pending command to approve." # Parse args: support "all", "all session", "all always", "session", "always" diff --git a/hermes_cli/config.py b/hermes_cli/config.py index 8cf33b90fe..6ca56422e2 100644 --- a/hermes_cli/config.py +++ b/hermes_cli/config.py @@ -781,6 +781,11 @@ DEFAULT_CONFIG = { "inline_diffs": True, # Show inline diff previews for write actions (write_file, patch, skill_manage) "show_cost": False, # Show $ cost in the status bar (off by default) "skin": "default", + # UI language for static user-facing messages (approval prompts, a + # handful of gateway slash-command replies). Does NOT affect agent + # responses, log lines, tool outputs, or slash-command descriptions. + # Supported: en, zh, ja, de, es. Unknown values fall back to en. + "language": "en", # TUI busy indicator style: kaomoji (default), emoji, unicode (braille # spinner), or ascii. Live-swappable via `/indicator