Newer
Older
navi-1 / navi / locales.py
"""Interface language tags: normalisation and the effective locale.

Mirror of the frontend i18n slugs: the override picked in Navi's settings →
the language of the gnexus-auth account (mirrored into ``navi_users.locale``)
→ ``en``. Tag normalisation is the same on both sides: ``en-US``/``en_US`` →
``en``, anything outside :data:`SUPPORTED_LOCALES` → ``None``.
"""

SUPPORTED_LOCALES = ("en", "uk", "ru")


def normalize_locale(tag) -> str | None:
    """``en-US`` → ``en``; anything outside SUPPORTED_LOCALES → None."""
    text = str(tag or "").strip().lower()
    prefix = text.split("-", 1)[0].split("_", 1)[0]
    return prefix if prefix in SUPPORTED_LOCALES else None


def effective_locale(override, account) -> str:
    """override → account language → ``en`` (same fallback as the frontend)."""
    return normalize_locale(override) or normalize_locale(account) or "en"