mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-06 17:29:46 +08:00
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
118 lines
4.5 KiB
Python
118 lines
4.5 KiB
Python
"""Deterministic language selection for book creation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import unicodedata
|
|
|
|
from deeptutor.services.prompt.language import normalize_language
|
|
|
|
_AUTO_LANGUAGE = "auto"
|
|
|
|
# Only phrased requests are treated as explicit language selection. Merely
|
|
# mentioning "Japanese history" must not force a Japanese book. Word cues are
|
|
# bounded so "in Germany" or "Berlin German" is not read as "in German".
|
|
_LANGUAGE_CUES: tuple[tuple[str, str], ...] = (
|
|
("zh-tw", r"(?:繁體中文|繁体中文|正體中文|正体中文|\bin\s+traditional\s+chinese\b)"),
|
|
("zh", r"(?:用(?:简体中文|中文|汉语)|以(?:中文|汉语)|\bin\s+chinese\b|\bin\s+mandarin\b)"),
|
|
("ja", r"(?:日本語で|用(?:日语|日文)|\bin\s+japanese\b)"),
|
|
("ko", r"(?:한국어로|用(?:韩语|韓語)|\bin\s+korean\b)"),
|
|
("ru", r"(?:\bпо-русски\b|\bна\s+русском\b|\bin\s+russian\b)"),
|
|
(
|
|
"es",
|
|
r"(?:\ben\s+español\b|\ben\s+espanol\b|\bin\s+spanish\b|\bcastellano\b"
|
|
r"|用(?:西班牙语|西班牙文))",
|
|
),
|
|
("fr", r"(?:\ben\s+français\b|\ben\s+francais\b|\bin\s+french\b|用(?:法语|法文))"),
|
|
("de", r"(?:\bauf\s+deutsch\b|\bin\s+german\b|用(?:德语|德文))"),
|
|
(
|
|
"pt",
|
|
r"(?:\bem\s+português\b|\bem\s+portugues\b|\bin\s+portuguese\b|用(?:葡萄牙语|葡萄牙文))",
|
|
),
|
|
("it", r"(?:\bin\s+italiano\b|\bin\s+italian\b|用(?:意大利语|意大利文))"),
|
|
(
|
|
"vi",
|
|
r"(?:\bbằng\s+tiếng\s+việt\b|\bbang\s+tieng\s+viet\b|\bin\s+vietnamese\b"
|
|
r"|用越南(?:语|語|文))",
|
|
),
|
|
("tr", r"(?:\btürkçe\s+olarak\b|\btürkçe\s+yaz(?:ın)?\b|\bin\s+turkish\b|用土耳其(?:语|文))"),
|
|
("hu", r"(?:\bmagyarul\b|\bmagyar\s+nyelven\b|\bin\s+hungarian\b|\bauf\s+ungarisch\b)"),
|
|
("en", r"(?:\bin\s+english\b|英語で|用(?:英语|英文)|\ben\s+anglais\b|\ben\s+inglés\b)"),
|
|
)
|
|
|
|
_COMPILED_CUES = tuple(
|
|
(language, re.compile(pattern, re.IGNORECASE | re.UNICODE))
|
|
for language, pattern in _LANGUAGE_CUES
|
|
)
|
|
|
|
|
|
def _explicit_language(user_intent: str) -> str | None:
|
|
"""Return the last explicit language request in the intent."""
|
|
matches: list[tuple[int, str]] = []
|
|
for language, pattern in _COMPILED_CUES:
|
|
for match in pattern.finditer(user_intent):
|
|
matches.append((match.start(), language))
|
|
if not matches:
|
|
return None
|
|
return max(matches, key=lambda item: item[0])[1]
|
|
|
|
|
|
def _script_language(user_intent: str) -> str | None:
|
|
"""Infer high-confidence scripts without a language-detection dependency."""
|
|
counts = {"latin": 0, "han": 0, "kana": 0, "hangul": 0, "cyrillic": 0}
|
|
|
|
for char in user_intent:
|
|
if not char.isalpha():
|
|
continue
|
|
script = unicodedata.name(char, "").split(" ", 1)[0]
|
|
if script == "LATIN":
|
|
counts["latin"] += 1
|
|
elif script in {"HIRAGANA", "KATAKANA"}:
|
|
counts["kana"] += 1
|
|
elif script == "HANGUL":
|
|
counts["hangul"] += 1
|
|
elif script == "CYRILLIC":
|
|
counts["cyrillic"] += 1
|
|
elif (
|
|
"\u3400" <= char <= "\u4dbf"
|
|
or "\u4e00" <= char <= "\u9fff"
|
|
or "\uf900" <= char <= "\ufaff"
|
|
):
|
|
counts["han"] += 1
|
|
|
|
# Han characters are ambiguous between Chinese and Japanese. Kana resolves
|
|
# that ambiguity; Kanji-only Japanese remains intentionally conservative.
|
|
if counts["kana"]:
|
|
return "ja"
|
|
if counts["hangul"] > max(counts["latin"], counts["han"], counts["cyrillic"]):
|
|
return "ko"
|
|
if counts["cyrillic"] > counts["latin"]:
|
|
return "ru"
|
|
if counts["han"] > max(counts["latin"], counts["hangul"], counts["cyrillic"]):
|
|
return "zh"
|
|
return None
|
|
|
|
|
|
def resolve_book_language(
|
|
*,
|
|
user_intent: str,
|
|
requested_language: str | None = "auto",
|
|
fallback_language: str | None = "en",
|
|
) -> str:
|
|
"""Resolve a concrete book language before any generation stage runs.
|
|
|
|
Explicit selections always win. ``auto`` uses an explicit request cue, then
|
|
a high-confidence script signal, then the caller's fallback (normally the
|
|
interface language).
|
|
"""
|
|
requested = normalize_language(requested_language)
|
|
fallback = normalize_language(fallback_language)
|
|
if requested not in {_AUTO_LANGUAGE, "automatic", "detect"}:
|
|
return requested
|
|
|
|
intent = user_intent or ""
|
|
return _explicit_language(intent) or _script_language(intent) or fallback
|
|
|
|
|
|
__all__ = ["resolve_book_language"]
|