Files
DeepTutor/tests/book/test_overview_copy.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

94 lines
3.0 KiB
Python

"""The Overview page is built without an LLM, so its copy is ours to get right.
Every other block's wording comes from the model in the book's language. This
one page is rendered deterministically, so its strings used to be
`"中文" if language == "zh" else "English"` conditionals in engine.py — which is
why the language picker could offer eleven languages while the book's own front
page spoke two.
These tests pin the table's shape and keep the remaining gap countable rather
than implicit.
"""
from __future__ import annotations
import pytest
from deeptutor.book.overview_copy import (
_COPY,
REQUIRED_KEYS,
missing_languages,
overview_copy,
)
# What the creator's picker offers (BookCreator.BOOK_LANGUAGES).
PICKER_LANGUAGES = [
"en",
"zh",
"zh-tw",
"ja",
"ko",
"es",
"fr",
"de",
"ru",
"pt",
"it",
"vi",
]
@pytest.mark.parametrize("language", sorted(_COPY))
def test_every_entry_defines_every_key(language: str) -> None:
"""A partial translation would render a stray English string mid-page."""
missing = REQUIRED_KEYS - set(_COPY[language])
assert not missing, f"{language} is missing {missing}"
@pytest.mark.parametrize("language", sorted(_COPY))
def test_the_intro_body_interpolates(language: str) -> None:
body = _COPY[language]["intro_body"].format(concepts=7, chapters=3)
assert "7" in body and "3" in body
assert "{" not in body
@pytest.mark.parametrize("language", sorted(_COPY))
def test_objectives_are_a_non_empty_list_of_strings(language: str) -> None:
objectives = _COPY[language]["objectives"]
assert isinstance(objectives, list) and objectives
assert all(isinstance(o, str) and o.strip() for o in objectives)
def test_a_regional_tag_resolves_to_its_base_language() -> None:
assert overview_copy("zh-CN") is _COPY["zh"]
assert overview_copy("zh-tw") is _COPY["zh-tw"]
assert overview_copy("zh-Hant-TW") is _COPY["zh-tw"]
assert overview_copy("zh-HK") is _COPY["zh-tw"]
assert _COPY["zh-tw"] is not _COPY["zh"]
def test_an_unknown_language_falls_back_to_english_rather_than_breaking() -> None:
for unknown in ("ja", "kl", "", None, " "):
assert overview_copy(unknown) is _COPY["en"]
def test_vietnamese_has_its_own_overview_copy_and_regional_tags_resolve_to_it() -> None:
assert overview_copy("vi") is _COPY["vi"]
assert overview_copy("vi-VN") is _COPY["vi"]
assert _COPY["vi"] is not _COPY["en"]
def test_the_translation_gap_is_reported_honestly() -> None:
"""Not a failure — a ledger. Adding a language is one entry in _COPY."""
gap = missing_languages(PICKER_LANGUAGES)
covered = len(PICKER_LANGUAGES) - len(gap)
assert covered >= 3, "en, zh and zh-tw (via fallback) must be covered"
# If this trips, someone translated more languages — update the number,
# and be glad.
assert set(gap) <= {"ja", "ko", "es", "fr", "de", "ru", "pt", "it"}
def test_english_is_always_present_because_it_is_the_fallback() -> None:
assert "en" in _COPY
assert REQUIRED_KEYS <= set(_COPY["en"])