config_language.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """``display.language`` validation for ``hermes config set`` — pluggable languages.
  2. The accepted set is ``agent.i18n.supported_languages()``: bundled catalogs ∪ the profile's overlay dir
  3. ∪ every loaded plugin language pack. Packs only exist after discovery, and ``hermes config`` never
  4. imports ``model_tools`` (the usual discovery trigger), so this runs ``discover_plugins()`` first —
  5. otherwise ``hermes config set display.language pl`` would refuse the very pack the user just installed.
  6. """
  7. from __future__ import annotations
  8. from typing import Optional
  9. DISPLAY_LANGUAGE_KEY = "display.language"
  10. def resolve_display_language(value: str) -> Optional[str]:
  11. """Canonical id for *value* (alias/region-tolerant) or ``None`` when no layer supplies it."""
  12. from agent.i18n import reset_language_cache, resolve_language_id
  13. try:
  14. from hermes_cli.plugins import discover_plugins
  15. discover_plugins()
  16. except Exception:
  17. pass # a broken plugin tree must not block setting a bundled language
  18. reset_language_cache() # packs registered during discovery must be visible to this check
  19. return resolve_language_id(value)
  20. def display_language_error(value: str) -> Optional[str]:
  21. """User-facing refusal for an unsupported ``display.language`` value, ``None`` when acceptable.
  22. Auto-detect (empty) is always accepted."""
  23. if not str(value).strip():
  24. return None
  25. if resolve_display_language(value) is not None:
  26. return None
  27. from agent.i18n import supported_languages
  28. return (f"✗ Unknown language {value!r} for {DISPLAY_LANGUAGE_KEY}. Available: "
  29. f"{', '.join(supported_languages())}.\n Install a language pack plugin "
  30. "(hermes plugins install <pack>) or drop <HERMES_HOME>/locales/<id>.yaml to add one.")
  31. __all__ = ["DISPLAY_LANGUAGE_KEY", "display_language_error", "resolve_display_language"]