i18n_languages.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. """Language identity for the bundled locales — endonym and script direction — plus the picker list.
  2. One Python table; ``apps/shared/src/i18n.ts`` ``LOCALE_ENDONYMS`` / ``RTL_LOCALES`` must agree for the
  3. ids both sides bundle (the desktop lane's test checks the relationship). Pack-only languages take
  4. their endonym/rtl from the pack registration (``PluginContext.register_locale(endonym=..., rtl=...)``)
  5. and fall back to the bare id.
  6. """
  7. from __future__ import annotations
  8. from typing import Iterable, TypedDict
  9. class LanguageOption(TypedDict):
  10. id: str
  11. endonym: str
  12. rtl: bool
  13. source: str
  14. surfaces: list[str]
  15. # id -> (endonym, rtl). Keep sorted by id; ``en`` is listed first by ``language_options`` regardless.
  16. BUNDLED_LANGUAGE_INFO: dict[str, tuple[str, bool]] = {
  17. "af": ("Afrikaans", False),
  18. "ar": ("العربية", True),
  19. "de": ("Deutsch", False),
  20. "en": ("English", False),
  21. "es": ("Español", False),
  22. "fr": ("Français", False),
  23. "ga": ("Gaeilge", False),
  24. "hu": ("Magyar", False),
  25. "it": ("Italiano", False),
  26. "ja": ("日本語", False),
  27. "ko": ("한국어", False),
  28. "pt": ("Português", False),
  29. "ru": ("Русский", False),
  30. "tr": ("Türkçe", False),
  31. "uk": ("Українська", False),
  32. "zh": ("简体中文", False),
  33. "zh-hant": ("繁體中文", False),
  34. }
  35. BUNDLED_SOURCE = "bundled"
  36. OVERLAY_SOURCE = "overlay"
  37. def describe_language(lang: str, *, pack: dict | None, overlay: bool, surfaces: Iterable[str]) -> LanguageOption:
  38. """One picker row. Precedence for endonym/rtl: bundled table → pack metadata → bare id / LTR.
  39. ``source`` names the highest layer that supplies the language (``plugin:<name>`` > ``overlay`` >
  40. ``bundled``) so a picker can say where a language came from. ``surfaces`` are the surfaces some
  41. layer has strings for (``agent.i18n.language_surfaces``), so a renderer can leave out a language it
  42. would only show in English."""
  43. bundled = BUNDLED_LANGUAGE_INFO.get(lang)
  44. endonym, rtl = bundled if bundled else ((pack or {}).get("endonym") or lang, bool((pack or {}).get("rtl")))
  45. if pack is not None:
  46. source = str(pack.get("source") or "plugin")
  47. elif overlay and bundled is None:
  48. source = OVERLAY_SOURCE
  49. else:
  50. source = BUNDLED_SOURCE
  51. return {"id": lang, "endonym": endonym, "rtl": rtl, "source": source, "surfaces": list(surfaces)}
  52. def language_options() -> list[LanguageOption]:
  53. """``[{"id", "endonym", "rtl", "source", "surfaces"}, ...]`` for every supported language, ``en`` first then sorted
  54. by id — the list ``i18n.languages`` serves and every switcher renders (endonym only, no flags)."""
  55. from agent import i18n, i18n_layers
  56. from hermes_constants import get_hermes_home
  57. home = get_hermes_home()
  58. overlay_langs = i18n_layers.overlay_languages(home)
  59. surfaces = i18n.language_surfaces(str(home))
  60. return [
  61. describe_language(lang, pack=i18n_layers.pack_info(lang), overlay=lang in overlay_langs,
  62. surfaces=surfaces.get(lang, ()))
  63. for lang in i18n.supported_languages()
  64. ]
  65. __all__ = ["BUNDLED_LANGUAGE_INFO", "LanguageOption", "describe_language", "language_options"]