"""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", "zh-tw", "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", "简体中文": "zh", "中文": "zh", "zh-hk": "zh-tw", "zh-mo": "zh-tw", "traditional chinese": "zh-tw", "繁體中文": "zh-tw", "繁体中文": "zh-tw", "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("_", "-") if code.startswith("zh-hant"): return "zh-tw" 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