Files
DeepTutor/deeptutor/core/interface_languages.py
Botong b86f06e906 feat(i18n): refine Traditional Chinese terminology and activate zh-tw across web, desktop, and backend
Complete full-stack activation of Traditional Chinese (zh-tw) on top of #3064 and #3065:

- Review and refine 683 app.json entries and 9 common.json entries in web/locales/zh-tw/ for Taiwan Traditional Chinese terminology (fixing OpenCC s2twp artifacts and mainland variants such as 許可權→權限, 賬號/賬戶→帳號/帳戶, 回撥→回呼, 自定義→自訂, 閘道器→閘道, 例項→實例, 只讀→唯讀, 憑據→憑證, 服務端→伺服器端, 配置檔案→設定檔, 地址→位址, 埠→連接埠, 映象→映像/鏡像, and curly quotes→「」) while keeping every key, placeholder, code span, and URL intact.
- Add language.chinese_traditional ("繁體中文") across all 14 web locale catalogs (and portuguese_unchanged_ui.json).
- Register zh-tw in web/i18n/languages.ts (preserving zh-tw/zh-hk/zh-mo/zh-hant before zh), dynamic loader web/i18n/init.ts, and web/lib/datetime.ts (zh-TW).
- Add complete Traditional Chinese Desktop shell copy in apps/desktop/src/i18n.mjs and register zh-tw in apps/desktop/src/main.mjs with zh-tw/zh-hant normalization.
- Wire zh-tw into backend deterministic copy and settings: settings_spec.py, runtime/banner.py, services/i18n.py, book/overview_copy.py, book/export.py, book/language.py, api/routers/quiz_judge.py (+ router_index.json), and zh-hk/zh-mo/zh-hant/繁體中文 handling in interface_languages.py and interface_settings.py (_normalize_response_language).
- Add full-stack regression coverage in tests/i18n/test_traditional_chinese_locale.py, apps/desktop/test/i18n.test.mjs, web/tests/language-preferences.test.ts, web/tests/account-role.test.ts, and tests/book/test_overview_copy.py.

Related to #2933
Related to #684
2026-12-03 04:43:27 -05:00

67 lines
1.8 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", "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