From da4dba4618568833c7865e11a55ddb4a6b2bfa52 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 24 Sep 2026 22:42:37 +0200 Subject: [PATCH] fix(i18n): repair the three defects a translation reliably comes back with Measured over the entries no rule reported, a machine translation loses the sentence's full stop in 12417 cases, the capital of its opening word in 2139, and opens a run of spaces around a protected span in 610. None of them touches the meaning, so the answer is worth keeping and worth repairing rather than discarding. The full stop goes missing when a closing bracket moves in front of a protected span and takes the punctuation behind it along, which leaves the sentence running into the next one. resegment already puts back a boundary that went missing mid-text; terminate does the same at the end. collapse leaves a run the source carries itself alone, because an aligned block in a code sample means its spacing. Truncation cannot be repaired, only reported, and joins the damaged set with a floor of 120 characters: `Situation` becomes `Lage` and halves its length while saying the same thing, so a shorter entry proves nothing. Co-Authored-By: Claude Opus 5 (1M context) --- tests/unit/python/utils/i18n/test_repairs.py | 122 +++++++++++++++++++ utils/i18n/placeholders.py | 48 ++++++++ utils/i18n/translate.py | 20 +++ 3 files changed, 190 insertions(+) create mode 100644 tests/unit/python/utils/i18n/test_repairs.py diff --git a/tests/unit/python/utils/i18n/test_repairs.py b/tests/unit/python/utils/i18n/test_repairs.py new file mode 100644 index 0000000000..12adc16f33 --- /dev/null +++ b/tests/unit/python/utils/i18n/test_repairs.py @@ -0,0 +1,122 @@ +"""Unit tests for the repairs a restored translation goes through. + +Every case is a shape measured in the catalogs: a bracket that moved in front of +a protected span and took the full stop with it, an opening word the translator +lower-cased, and a run of spaces it opened around a span. +""" + +from __future__ import annotations + +import unittest + +from utils.i18n.placeholders import collapse, mask, recapitalise, terminate, unmask +from utils.i18n.translate import truncated + + +def _round_trip(source: str, translated: str) -> str | None: + """Send ``translated`` back through the unmasking a real answer goes through. + + Args: + source: the source message. + translated: what the translator returned for the masked request. + """ + return unmask(translated, mask(source), source) + + +class TestCollapse(unittest.TestCase): + def test_a_run_the_source_lacks_is_closed(self) -> None: + self.assertEqual( + collapse("Works with the helper", "Works with it"), "Works with the helper" + ) + + def test_a_run_the_source_carries_is_left_alone(self) -> None: + source = "column aligned" + + self.assertEqual( + collapse("Spalte ausgerichtet", source), "Spalte ausgerichtet" + ) + + def test_a_tab_in_the_source_disables_the_repair(self) -> None: + self.assertEqual(collapse("a b", "a\tb"), "a b") + + +class TestTerminate(unittest.TestCase): + def test_a_lost_full_stop_comes_back(self) -> None: + source = "Return whether the paths match (as os.path.samefile() does)." + + self.assertEqual( + terminate("Ob die Pfade gleich sind (wie) os.path.samefile()", source), + "Ob die Pfade gleich sind (wie) os.path.samefile().", + ) + + def test_a_question_mark_comes_back(self) -> None: + self.assertEqual(terminate("Ist es bereit", "Is it ready?"), "Ist es bereit?") + + def test_a_translation_that_kept_one_is_untouched(self) -> None: + self.assertEqual(terminate("Fertig!", "Done."), "Fertig!") + + def test_a_source_without_one_adds_nothing(self) -> None: + self.assertEqual(terminate("Backup", "Backup"), "Backup") + + +class TestRecapitalise(unittest.TestCase): + def test_a_lower_cased_opening_is_restored(self) -> None: + self.assertEqual( + recapitalise("alle `alpha` Kriterien.", "All `alpha` criteria."), + "Alle `alpha` Kriterien.", + ) + + def test_a_lowercase_source_keeps_its_case(self) -> None: + self.assertEqual( + recapitalise("alle Kriterien", "all criteria"), "alle Kriterien" + ) + + def test_an_already_capital_opening_is_untouched(self) -> None: + self.assertEqual( + recapitalise("Alle Kriterien", "All criteria"), "Alle Kriterien" + ) + + def test_an_opening_protected_span_is_untouched(self) -> None: + self.assertEqual( + recapitalise("`alpha` zuerst", "`alpha` first"), "`alpha` zuerst" + ) + + +class TestUnmaskAppliesTheRepairs(unittest.TestCase): + def test_a_lower_cased_opening_is_repaired_on_the_way_back(self) -> None: + restored = _round_trip( + "All `alpha` criteria.", 'alle Kriterien.' + ) + + self.assertEqual(restored, "Alle `alpha` Kriterien.") + + def test_a_missing_full_stop_is_repaired_on_the_way_back(self) -> None: + restored = _round_trip("All `alpha` criteria.", 'Alle Kriterien') + + self.assertEqual(restored, "Alle `alpha` Kriterien.") + + def test_a_space_run_is_repaired_on_the_way_back(self) -> None: + restored = _round_trip( + "All `alpha` criteria.", 'Alle Kriterien.' + ) + + self.assertEqual(restored, "Alle `alpha` Kriterien.") + + +class TestTruncated(unittest.TestCase): + def test_a_short_entry_may_shrink(self) -> None: + self.assertFalse(truncated("Situation", "Lage")) + + def test_a_long_passage_that_halved_is_reported(self) -> None: + source = "A" * 200 + + self.assertTrue(truncated(source, "B" * 80)) + + def test_a_long_passage_that_kept_its_body_is_not(self) -> None: + source = "A" * 200 + + self.assertFalse(truncated(source, "B" * 180)) + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/i18n/placeholders.py b/utils/i18n/placeholders.py index 95608f1cd1..9aa99bc404 100644 --- a/utils/i18n/placeholders.py +++ b/utils/i18n/placeholders.py @@ -111,6 +111,53 @@ def tighten(restored: str) -> str: ) +TERMINATORS = ".!?" +RUN_OF_SPACES = re.compile(r"[ \t]{2,}") + + +def collapse(restored: str, source: str) -> str: + """Return ``restored`` without the space runs the translator opened. + + A run the source carries itself is left alone, because an aligned block in a + code sample means its spacing. + + Args: + restored: the translation with every protected span put back. + source: the source message. + """ + if " " in source or "\t" in source: + return restored + return RUN_OF_SPACES.sub(" ", restored) + + +def terminate(restored: str, source: str) -> str: + """Return ``restored`` with the sentence end the translator lost. + + A closing bracket that moves in front of a protected span takes the full + stop behind it with it, which leaves the sentence running on. + + Args: + restored: the translation with every protected span put back. + source: the source message. + """ + end = source.rstrip()[-1:] + if end not in TERMINATORS or restored.rstrip().endswith(tuple(TERMINATORS)): + return restored + return restored.rstrip() + end + + +def recapitalise(restored: str, source: str) -> str: + """Return ``restored`` with the capital its opening word lost. + + Args: + restored: the translation with every protected span put back. + source: the source message. + """ + if not source[:1].isupper() or not restored[:1].islower(): + return restored + return restored[0].upper() + restored[1:] + + TOKEN = re.compile(r'(?:\s*)?') @@ -200,6 +247,7 @@ def unmask(translated: str, masked: Masked, source: str) -> str | None: position = match.end() pieces.append(html.unescape(translated[position:])) restored = resegment(tighten("".join(pieces).strip()), masked.spans, source) + restored = recapitalise(terminate(collapse(restored, source), source), source) if sorted(seen) != list(range(len(masked.spans))): return None if not restored or protected_spans(restored) != protected_spans(source): diff --git a/utils/i18n/translate.py b/utils/i18n/translate.py index ff4cc56e91..0e4e1ba6f1 100644 --- a/utils/i18n/translate.py +++ b/utils/i18n/translate.py @@ -12,6 +12,25 @@ if TYPE_CHECKING: from babel.messages.catalog import Catalog, Message STRUCTURE = "[]{}`*()" +TRUNCATION_FLOOR = 120 +TRUNCATION_RATIO = 0.5 + + +def truncated(source: str, translation: str) -> bool: + """Return whether ``translation`` kept too little of ``source`` to be one. + + The floor keeps a short entry out: ``Situation`` becomes ``Lage`` and loses + half its characters while saying the same thing. A passage past it that + comes back halved has dropped a clause. + + Args: + source: the source message. + translation: what came back for it. + """ + return ( + len(source) > TRUNCATION_FLOOR + and len(translation) < len(source) * TRUNCATION_RATIO + ) def structure(text: str) -> Counter: @@ -59,6 +78,7 @@ def damaged(catalog: Catalog) -> list[Message]: and ( protected_spans(str(message.string)) != protected_spans(message.id) or missing_names(message.id, str(message.string)) + or truncated(message.id, str(message.string)) or structure(str(message.string)) != structure(message.id) ) ]