rusackas commented on code in PR #39443:
URL: https://github.com/apache/superset/pull/39443#discussion_r3277158440


##########
scripts/translations/check_translation_regression.py:
##########
@@ -0,0 +1,235 @@
+#!/usr/bin/env python3
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""
+Check that source-code changes don't cause translation regressions.
+
+Usage
+-----
+Count non-fuzzy translated entries in all .po files and write JSON to stdout:
+
+    python check_translation_regression.py --count
+
+Compare the current .po state against a previously-recorded baseline and fail
+if any language lost translations:
+
+    python check_translation_regression.py --compare /path/to/before.json
+
+Optionally write a markdown report to a file (used by CI to post a PR comment):
+
+    python check_translation_regression.py --compare before.json --report 
report.md
+
+Use a translations directory other than the repo default (used by CI to count
+against a separate base-branch worktree):
+
+    python check_translation_regression.py --count \\
+        --translations-dir /tmp/base-worktree/superset/translations
+
+Typical CI workflow
+-------------------
+1. Create a base-branch worktree alongside the PR worktree
+2. Run babel_update.sh in the base worktree (extract from BASE source +
+   BASE translations) — produces the baseline that already accounts for any
+   pre-existing drift on the base branch
+3. Record baseline:  python ... --count --translations-dir BASE_TREE > 
before.json
+4. Run babel_update.sh in the PR worktree (extract from PR source +
+   PR translations) — this lets a contributor fix a regression by committing
+   .po updates alongside source changes
+5. Compare:  python ... --compare before.json [--report report.md]
+
+The base run absorbs any pre-existing drift on master; the PR run is
+checked against that baseline, so the question CI answers is "did this PR
+net-decrease the translated count, given both sides' source + .po files?".
+"""
+
+import argparse
+import json
+import re
+import subprocess
+import sys
+from pathlib import Path
+from typing import Optional
+
+DEFAULT_TRANSLATIONS_DIR = (
+    Path(__file__).resolve().parent.parent.parent / "superset" / "translations"
+)
+
+# English .po files use empty msgstr by convention (source language == target),
+# so they always show 0 translated entries and should not be checked.
+SKIP_LANGS = {"en"}
+
+
+def count_translated(po_file: Path) -> int:
+    """Return the number of non-fuzzy translated messages in a .po file."""
+    import shutil  # noqa: PLC0415
+
+    msgfmt = shutil.which("msgfmt") or "msgfmt"
+    result = subprocess.run(  # noqa: S603
+        [msgfmt, "--statistics", "-o", "/dev/null", str(po_file)],
+        capture_output=True,
+        text=True,
+        check=True,
+    )
+    # stderr: "123 translated messages, 4 fuzzy translations, 56 untranslated 
messages."
+    match = re.search(r"(\d+) translated message", result.stderr)
+    if not match:

Review Comment:
   Still declining — the underlying claim is incorrect. `re.search` matches 
substrings, not whole words, so `r'(\d+) translated message'` matches both 
singular and plural forms:
   
   ```
   >>> re.search(r'(\d+) translated message', '1 translated message')        → 
'1'
   >>> re.search(r'(\d+) translated message', '123 translated messages, ...') → 
'123'
   >>> re.search(r'(\d+) translated message', '0 translated messages, ...')   → 
'0'
   ```
   
   The trailing 's' is past the match boundary. Adding `s?` would be a no-op 
since the pattern doesn't require the next character. No bug here; not changing 
the code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to