Files
DeepTutor/deeptutor/core/interface_languages.py

56 lines
1.5 KiB
Python

"""Backend interface-locale contract, independent of settings or launch code.
Keep this domain aligned with shipped interface locales and settings choices.
Model reply languages have a separate, broader contract.
"""
from __future__ import annotations
from typing import Any, Literal, cast, get_args
UiLanguage = Literal["en", "zh", "fr", "de", "uk", "pl", "pt", "vi", "ja", "tr", "it", "hu", "ko"]
SUPPORTED_UI_LANGUAGES: tuple[str, ...] = get_args(UiLanguage)
_ALIASES = {
"english": "en",
"chinese": "zh",
"cn": "zh",
"simplified chinese": "zh",
"french": "fr",
"français": "fr",
"francais": "fr",
"german": "de",
"deutsch": "de",
"ukrainian": "uk",
"ua": "uk",
"polish": "pl",
"polski": "pl",
"portuguese": "pt",
"português": "pt",
"vietnamese": "vi",
"tiếng việt": "vi",
"tieng viet": "vi",
"turkish": "tr",
"türkçe": "tr",
"italian": "it",
"italiano": "it",
"japanese": "ja",
"jp": "ja",
"hungarian": "hu",
"magyar": "hu",
"korean": "ko",
"한국어": "ko",
}
def normalize_ui_language(value: Any) -> UiLanguage | None:
"""Canonicalize known interface locales; leave fallback policy to callers."""
if not isinstance(value, str):
return None
code = value.strip().lower().replace("_", "-")
code = _ALIASES.get(code, code)
if code not in SUPPORTED_UI_LANGUAGES:
base = code.split("-", 1)[0]
code = _ALIASES.get(base, base)
return cast(UiLanguage, code) if code in SUPPORTED_UI_LANGUAGES else None