Supports the latest Typecho 1.3.0 and introduces an AI-driven, self-healing i18n translation pipeline - #64
Open
little-gt wants to merge 1 commit into
Conversation
1. Introduced techniques and mechanisms to automatically extract translatable multilingual text from the latest Typecho projects, thereby enhancing the efficiency of template updates; 2. Integrated automated translation technology based on Tencent Cloud TMT, enabling the rapid establishment of a multilingual foundation while supporting the overwriting of TMT cache data—following manual refinement—for use in subsequent updates; 3. Incorporated AI-driven automated workflow capabilities (Skills) to make multilingual processing smarter.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR turns the language pack from a static (hand-maintained, 2018-era) artifact into a
reproducible, machine-translation-driven, and self-validating engineering project. It adds a
complete toolchain under
tools/that scans Typecho's PHP source, machine-translates every stringvia Tencent Cloud TMT, hardens the output against the well-known failure modes of MT (broken
placeholders, emptied HTML tags, casing corruption), validates the result with a CI-ready checker,
and keeps human polish safe through a self-healing translation cache.
flowchart LR A[Typecho 1.3.0 PHP source] -->|tools/_i18n_analyze.py| B[original/messages.pot<br/>898 strings, Simplified-Chinese source of truth] B -->|tools/_tmt_translate.py<br/>TMT + hardening| C[original/projects/*.po<br/>10 languages] C -->|msgfmt| D[src/langs/*.mo<br/>install artifacts] C -->|tools/_po_check.py<br/>8-point validation| E[_po_check_report.json<br/>CI gate] C -->|tools/_build_tmt_cache.py<br/>re-seed| F[tools/runtime/_tmt_cache.json<br/>offline + polish-safe] F -->|served on next run| BWhat this PR changes
1. Template extraction —
tools/_i18n_analyze.py.phponly, skippingvendor/,usr/langs/,.git/, …),strips PHP comments while preserving line numbers, and extracts every i18n call:
_t(),_e()(singular) and_n()(plural).original/messages.potwithaccurate
#: path/file.php:linereferences, so every string is traceable back to its source.msgid+msgid_plural.2. Machine translation engine —
tools/_tmt_translate.pyCalls the Tencent Cloud TMT
TextTranslateAPI and regenerates alloriginal/projects/*.pofiles, keeping the POT's structure, ordering, and
#:references intact. It is not a naive"send string → store result" wrapper — it actively defends the output:
% s/% d/%1$ s(spaces inserted),%S/%D(wrong case),{sl ug}/{Cid}(split / re-cased).
repair_placeholder_spacing()andrestore_placeholder_case()re-assemblethem losslessly — only the broken placeholder is fixed, normal inter-word spacing is untouched.
(e.g.
分类 <a href="%s">%s</a> 已经被增加) are split bysplit_markup(); tags are preservedverbatim and only their inner text is translated, then re-assembled in original order. A
two-strategy fallback (mask-entire-element-then-translate → per-segment translate) guarantees the
tag structure never collapses into an empty link.
punctuation-terminated sentences),
_retry_untranslated()strips the trailing punctuation andretries. If the result still lacks placeholders, or the API errors / target is unsupported, the
string falls back to the source — and is recorded in a report, never silently accepted.
nplurals=1languages (zh_TW / ja_JP / ko_KR) get exactlyone
msgstrslot;nplurals=3(ru_RU) andn>1forms are taken fromDEFAULT_PLURAL_FORMS,so no spurious "missing plural slot" false positives.
ThreadPoolExecutor+ aRateLimiter(QPS≈4, the TMTceiling is 5), up to 3 retries per request, and an atomic cache write (
tmp+os.replace) with adirty-flag so it only flushes when something actually changed.
3. Validation gate —
tools/_po_check.pyA CI-ready checker that reuses
_tmt_translate.py's exact parsing and validation functions(
parse_po,is_safe_translation,check_html_integrity) so there is a single source of truthbetween translator and checker. It runs 8 checks per file:
msgstr, or strings still equal to the Chinese source%s/%d/%{name}/{name}/HTML tags/entities dropped or mutatedextra)msgstrcount vs. headerPlural-Forms<a>…</a>→<a></a>)msgstridentical to source (skipped for zh_TW via OpenCCs2t; ja_JP exempt for pure-Hanzi).poIt writes
tools/runtime/_po_check_report.json, prints a human-readable table, and exits non-zerowhen any issue is found — ready to drop into CI.
4. Self-healing cache & human polish —
tools/_build_tmt_cache.py_tmt_translate.pyserves translations fromtools/runtime/_tmt_cache.json(and on loadpurges cached entries that equal the Chinese source, fail placeholder checks, or have emptied
HTML tags — so a one-time MT failure is never permanently frozen).
_build_tmt_cache.pyre-seedsthe whole cache from the current
original/projects/*.po, so:.poedits survive the next translation instead of being silently overwritten(the documented polish loop is: edit
.po→_build_tmt_cache.py→msgfmt→_po_check.py).5. Docs, environment, and project hygiene
.venv/;.gitignorenow ignores.venv/. Dependencies are minimal:tencentcloud-sdk-python-common(TMT) andopencc-python-reimplemented(Traditional-Chinese / Hanzi detection).msgfmt(gettext) compiles.mo.tools/README.mdand atools/SKILL.mdmaintenance playbook covering the full workflow, known MT failure modes and theirhandling, the cache self-healing rule, the Traditional-Chinese special case, and how to add a language.
Why this mechanism is advanced
Most i18n MT pipelines stop at "call the API, write the file." This one treats MT as an
untrusted component that must be validated, repaired, and corrected — a closed loop rather than
a fire-and-forget step:
repair_placeholder_spacing,restore_placeholder_case) and re-validated before it is accepted. Broken placeholders andemptied HTML tags — the two most common, most damaging MT defects in UI strings — are caught and
either fixed or rejected, never shipped.
split_markup), not ablob of text. Tags survive byte-for-byte; only inner text is translated, with a fallback strategy
that still preserves correctness when the engine drops the masking tokens.
evicts bad translations, and on every run bad results are not written back. The system
improves over repeated runs instead of ossifying its first mistake — a property rarely found in
translation scripts.
is_safe_translation/check_html_integrity/parse_pocode, so "what we generate" and"what we accept" can never drift apart.
messages.potyou can deterministically rebuild theentire 10-language pack, and after
_build_tmt_cache.pythe heavy lifting needs no network atall. Human polish is a first-class, cache-safe operation.
_po_check.py's non-zero exit on any defect makes the pack's healthmachine-enforceable, and its OpenCC-aware verbatim detection avoids the false positives that would
otherwise make such a gate unusable for CJK targets.
plural slots,
#:references) avoids dependency onpolib/Babelbehavioral quirks and givesbyte-exact, POT-faithful output.
Result
pt_BR, ru_RU, tr_TR, zh_TW.
original/messages.pot: 898 source strings (~904 translatable fields incl. plurals), extractedfrom Typecho 1.3.0.
tools/_po_check.pyfinal run: 10/10 OK, 100% completeness, 0 issues across all checks(completeness, placeholders, POT alignment, Chinese residual, stale, plural, HTML, verbatim copies).
.pofiles (≈8970 entries) → subsequent runs are offline andpolish-safe.
Quick start
Polish loop (hand-edit a
.po):python tools/_build_tmt_cache.py # re-seed cache so edits survive msgfmt original/projects/xx_XX.po -o src/langs/xx_XX.mo python tools/_po_check.py