mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 21:19:31 +08:00
Merge pull request #3065 from hongwei-2026/codex/language-normalization
fix(i18n): share backend interface locale normalization
This commit is contained in:
55
deeptutor/core/interface_languages.py
Normal file
55
deeptutor/core/interface_languages.py
Normal file
@@ -0,0 +1,55 @@
|
||||
"""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
|
||||
@@ -6,6 +6,7 @@ import os
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from deeptutor.core.interface_languages import normalize_ui_language
|
||||
from deeptutor.runtime.home import get_runtime_home
|
||||
|
||||
from .runtime_settings import RuntimeSettingsService
|
||||
@@ -48,37 +49,7 @@ def _coerce_port(value: Any) -> int | None:
|
||||
|
||||
|
||||
def _normalize_language(value: Any) -> str | None:
|
||||
if not isinstance(value, str):
|
||||
return None
|
||||
language = value.strip().lower().replace("_", "-")
|
||||
base = language.split("-", 1)[0]
|
||||
if language == "english" or base == "en":
|
||||
return "en"
|
||||
if language == "chinese" or base in {"zh", "cn"}:
|
||||
return "zh"
|
||||
if language in {"french", "français", "francais"} or base == "fr":
|
||||
return "fr"
|
||||
if language in {"german", "deutsch"} or base == "de":
|
||||
return "de"
|
||||
if language == "ukrainian" or base in {"uk", "ua"}:
|
||||
return "uk"
|
||||
if language in {"polish", "polski"} or base == "pl":
|
||||
return "pl"
|
||||
if language in {"portuguese", "português"} or base == "pt":
|
||||
return "pt"
|
||||
if language in {"vietnamese", "tiếng việt", "tieng viet"} or base == "vi":
|
||||
return "vi"
|
||||
if language in {"turkish", "türkçe"} or base == "tr":
|
||||
return "tr"
|
||||
if language in {"italian", "italiano"} or base == "it":
|
||||
return "it"
|
||||
if language == "japanese" or base in {"ja", "jp"}:
|
||||
return "ja"
|
||||
if language in {"hungarian", "magyar"} or base == "hu":
|
||||
return "hu"
|
||||
if language in {"korean", "한국어"} or base == "ko":
|
||||
return "ko"
|
||||
return None
|
||||
return normalize_ui_language(value)
|
||||
|
||||
|
||||
def load_launch_settings(project_root: Path | None = None) -> LaunchSettings:
|
||||
|
||||
@@ -13,14 +13,14 @@ import os
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import threading
|
||||
from typing import Any, Literal
|
||||
from typing import Any
|
||||
|
||||
from deeptutor.core.interface_languages import UiLanguage as UiLanguage
|
||||
from deeptutor.core.interface_languages import normalize_ui_language
|
||||
from deeptutor.response_languages import SUPPORTED_RESPONSE_LANGUAGES
|
||||
from deeptutor.services.path_service import get_path_service
|
||||
from deeptutor.tools.builtin import USER_TOGGLEABLE_TOOL_NAMES
|
||||
|
||||
UiLanguage = Literal["en", "zh", "fr", "de", "uk", "pl", "pt", "vi", "ja", "tr", "it", "hu", "ko"]
|
||||
|
||||
DEFAULT_UI_SETTINGS: dict[str, Any] = {
|
||||
# "system" follows the OS. "snow" is the pure-white Default palette.
|
||||
"theme": "system",
|
||||
@@ -107,66 +107,14 @@ def _interface_settings_file():
|
||||
|
||||
|
||||
def _normalize_language(language: Any, default: str = "en") -> str:
|
||||
"""
|
||||
Normalize language codes:
|
||||
- en/english -> en
|
||||
- zh/chinese/cn -> zh
|
||||
- fr/french/français -> fr
|
||||
- de/german/deutsch -> de
|
||||
- uk/ukrainian/ua -> uk
|
||||
- pl/polish/polski -> pl
|
||||
- vi/vietnamese/tiếng việt -> vi
|
||||
- ja/jp/japanese -> ja
|
||||
- hu/hungarian/magyar -> hu
|
||||
- ko/korean/한국어 -> ko
|
||||
|
||||
An unknown code falls back to ``default`` rather than raising, so this is
|
||||
also the gate that decides which languages exist at all: a locale shipped
|
||||
in ``web/locales/`` but missing here is silently served as English.
|
||||
"""
|
||||
if language is None or language == "":
|
||||
language = default
|
||||
|
||||
if isinstance(language, str):
|
||||
s = language.lower().strip().replace("_", "-")
|
||||
base = s.split("-", 1)[0]
|
||||
if s == "english" or base == "en":
|
||||
return "en"
|
||||
if s == "chinese" or base in {"zh", "cn"}:
|
||||
return "zh"
|
||||
if s in {"french", "français", "francais"} or base == "fr":
|
||||
return "fr"
|
||||
if s in {"german", "deutsch"} or base == "de":
|
||||
return "de"
|
||||
if s == "ukrainian" or base in {"uk", "ua"}:
|
||||
return "uk"
|
||||
if s in {"polish", "polski"} or base == "pl":
|
||||
return "pl"
|
||||
if s in {"portuguese", "português"} or base == "pt":
|
||||
return "pt"
|
||||
if s in {"vietnamese", "tiếng việt", "tieng viet"} or base == "vi":
|
||||
return "vi"
|
||||
if s in {"turkish", "türkçe"} or base == "tr":
|
||||
return "tr"
|
||||
if s in {"italian", "italiano"} or base == "it":
|
||||
return "it"
|
||||
if s == "japanese" or base in {"ja", "jp"}:
|
||||
return "ja"
|
||||
if s in {"hungarian", "magyar"} or base == "hu":
|
||||
return "hu"
|
||||
if s in {"korean", "한국어"} or base == "ko":
|
||||
return "ko"
|
||||
|
||||
# Fall back to default
|
||||
if isinstance(default, str):
|
||||
return _normalize_language(default, "en")
|
||||
return "en"
|
||||
"""Normalize a saved interface locale with the caller's fallback policy."""
|
||||
return normalize_ui_language(language) or normalize_ui_language(default) or "en"
|
||||
|
||||
|
||||
def _normalize_response_language(language: Any, default: str = "en") -> str:
|
||||
"""Normalize only the wider model-output-language domain.
|
||||
|
||||
Interface language remains en/zh. This function accepts the labels a user
|
||||
Interface locales have their own registry. This accepts the labels a user
|
||||
may have copied from an issue or another deployment, then maps region
|
||||
variants to their supported prompt-language base.
|
||||
"""
|
||||
|
||||
@@ -77,13 +77,17 @@ that silently serves English on that surface.
|
||||
diff is easier to review.
|
||||
|
||||
3. **Backend interface language**
|
||||
- Add the code to `UiLanguage` and `_normalize_language()` in
|
||||
`deeptutor/services/settings/interface_settings.py`.
|
||||
- Add it to `_normalize_language()` in
|
||||
`deeptutor/services/config/launch_settings.py` so CLI/startup language
|
||||
overrides survive normalization.
|
||||
- Add the code to `UiLanguage` and any aliases to `normalize_ui_language()`
|
||||
in `deeptutor/core/interface_languages.py`. Settings and launcher readers
|
||||
share this contract while keeping their own fallback policies.
|
||||
- Keep the UI domain aligned with shipped catalogs, web/CLI pickers and
|
||||
settings choices. Output-only languages must not be enabled here early.
|
||||
When activating a distinct script/region locale, preserve it before the
|
||||
generic base-language fallback.
|
||||
- Add it to `_LANGUAGE_CHOICES` in
|
||||
`deeptutor/services/config/settings_spec.py`.
|
||||
- Add backend acceptance together with its complete locale integration;
|
||||
the shared registry does not certify translated status/CLI/desktop assets.
|
||||
|
||||
4. **Reply language, if the language is also offered for answers**
|
||||
- Add it to `SUPPORTED_RESPONSE_LANGUAGES` in
|
||||
|
||||
74
tests/core/test_interface_languages.py
Normal file
74
tests/core/test_interface_languages.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""One backend locale contract for settings and process startup."""
|
||||
|
||||
from typing import get_args
|
||||
|
||||
import pytest
|
||||
|
||||
from deeptutor.core.interface_languages import (
|
||||
SUPPORTED_UI_LANGUAGES,
|
||||
UiLanguage,
|
||||
normalize_ui_language,
|
||||
)
|
||||
from deeptutor.services.config.launch_settings import _normalize_language as launch_language
|
||||
from deeptutor.services.settings.interface_settings import _normalize_language, resolve_languages
|
||||
|
||||
|
||||
@pytest.mark.parametrize("code", SUPPORTED_UI_LANGUAGES)
|
||||
def test_registry_and_both_backend_normalizers_agree(code):
|
||||
assert code in get_args(UiLanguage)
|
||||
assert normalize_ui_language(code) == _normalize_language(code) == launch_language(code) == code
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"raw,expected",
|
||||
[
|
||||
(" ZH_TW ", "zh"),
|
||||
("zh-Hant", "zh"),
|
||||
("zh-Hant-TW", "zh"),
|
||||
("zh-CN", "zh"),
|
||||
("zh-Hans", "zh"),
|
||||
("ko-KR", "ko"),
|
||||
("korean", "ko"),
|
||||
("한국어", "ko"),
|
||||
("français", "fr"),
|
||||
("deutsch", "de"),
|
||||
("ua", "uk"),
|
||||
("jp", "ja"),
|
||||
],
|
||||
)
|
||||
def test_region_and_name_aliases_survive_startup(raw, expected):
|
||||
assert (
|
||||
normalize_ui_language(raw) == _normalize_language(raw) == launch_language(raw) == expected
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("raw", [None, 12, "", "xx", "unsupported"])
|
||||
def test_callers_keep_distinct_fallback_policies(raw):
|
||||
assert launch_language(raw) is None
|
||||
assert _normalize_language(raw, "fr") == "fr"
|
||||
assert _normalize_language(raw, "unsupported") == "en"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("code", SUPPORTED_UI_LANGUAGES)
|
||||
def test_legacy_migration_and_explicit_reply_preference_remain_separate(code):
|
||||
assert resolve_languages({"language": code}) == {"language": code, "response_language": code}
|
||||
assert resolve_languages({"language": code, "response_language": "ja"}) == {
|
||||
"language": code,
|
||||
"response_language": "ja",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("code", ["es", "es-ES", "Spanish", "traditional chinese"])
|
||||
def test_unshipped_interface_languages_are_not_activated(code):
|
||||
assert normalize_ui_language(code) is None
|
||||
assert launch_language(code) is None
|
||||
assert _normalize_language(code) == "en"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("code", ["es", "zh-tw"])
|
||||
def test_output_only_languages_remain_valid_reply_preferences(code):
|
||||
assert code not in SUPPORTED_UI_LANGUAGES
|
||||
assert resolve_languages({"language": "ko", "response_language": code}) == {
|
||||
"language": "ko",
|
||||
"response_language": code,
|
||||
}
|
||||
@@ -34,6 +34,14 @@ def _web_interface_languages() -> list[str]:
|
||||
WEB_LANGUAGES = _web_interface_languages()
|
||||
|
||||
|
||||
def test_backend_ui_domain_matches_shipped_interface_choices() -> None:
|
||||
assert (
|
||||
set(get_args(UiLanguage))
|
||||
== set(WEB_LANGUAGES)
|
||||
== {code for code, _label, _description in _LANGUAGE_CHOICES}
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("code", WEB_LANGUAGES)
|
||||
def test_web_interface_language_is_a_backend_ui_language(code: str) -> None:
|
||||
assert code in get_args(UiLanguage)
|
||||
|
||||
Reference in New Issue
Block a user