rusackas commented on code in PR #41651: URL: https://github.com/apache/superset/pull/41651#discussion_r3530912411
########## scripts/translations/apply_do_not_translate.py: ########## @@ -0,0 +1,128 @@ +#!/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. +"""Stamp do-not-translate msgids in a .pot with an extracted-comment marker. + +For every msgid listed in ``superset/translations/do-not-translate.txt`` that is +present in the target .pot, add a ``#. MACHINE_READ-DO_NOT_TRANSLATE`` extracted +comment. gettext extracted comments (``#.``) propagate from the .pot into every +language .po on ``pybabel update``, so the do-not-translate status stays +consistent across all catalogs from a single registry. + +Run from ``babel_update.sh`` after the .pot is extracted and normalized (and +before ``pybabel update``). Idempotent: re-running makes no further changes. + +Usage: + python scripts/translations/apply_do_not_translate.py [POT_PATH] + # POT_PATH defaults to superset/translations/messages.pot +""" + +from __future__ import annotations + +import sys +from pathlib import Path + +# The standardized extracted-comment marker. Kept in sync with backfill_po.py. +MARKER: str = "MACHINE_READ-DO_NOT_TRANSLATE" +_MARKER_LINE: str = f"#. {MARKER}" + +TRANSLATIONS_DIR: Path = ( + Path(__file__).parent.parent.parent / "superset" / "translations" +) +DEFAULT_POT: Path = TRANSLATIONS_DIR / "messages.pot" +REGISTRY: Path = TRANSLATIONS_DIR / "do-not-translate.txt" + + +def load_registry(path: Path = REGISTRY) -> set[str]: + """Return the set of do-not-translate msgids (skips comments/blank lines). + + Each line is stripped before the blank/comment check, so trailing + whitespace or an indented comment never yields a msgid that fails to match + the .pot. + """ + if not path.exists(): + return set() + entries: set[str] = set() + for raw_line in path.read_text(encoding="utf-8").splitlines(): + line = raw_line.strip() Review Comment: Annotated `line: str` in 3452af96bb. ########## scripts/translations/backfill_po.py: ########## @@ -152,6 +152,61 @@ def _is_missing(entry: polib.POEntry) -> bool: return not entry.msgstr +# Canonical registry of msgids that must never be machine-translated: literal +# tokens compared against source (SQL keywords, confirmation words), enum values +# (d3 interpolation modes), icon names (e.g. "bolt" -> the ⚡ Explore control +# icon), API field names, code constants, and example placeholders. Translating +# them can break icon lookups, enum matching, or API contracts, or is simply +# meaningless (proper nouns, example values). apply_do_not_translate.py stamps +# these msgids in messages.pot with a `#. MACHINE_READ-DO_NOT_TRANSLATE` +# extracted comment that propagates to every catalog on `pybabel update`. +DO_NOT_TRANSLATE_REGISTRY: Path = TRANSLATIONS_DIR / "do-not-translate.txt" + + +def _load_do_not_translate(path: Path = DO_NOT_TRANSLATE_REGISTRY) -> frozenset[str]: + """Load the do-not-translate msgids (skips comment/blank lines). + + Lines are stripped before the blank/comment checks, matching the parsing in + apply_do_not_translate.py, so trailing whitespace or an indented comment + never yields a msgid that fails to match a catalog entry. + """ + if not path.exists(): + return frozenset() + return frozenset( + stripped + for line in path.read_text(encoding="utf-8").splitlines() + if (stripped := line.strip()) and not stripped.startswith("#") + ) + + +DO_NOT_TRANSLATE: frozenset[str] = _load_do_not_translate() + +# An explicit do-not-translate marker on an entry, matched in either the +# extracted comment (`#. MACHINE_READ-DO_NOT_TRANSLATE`, the standard propagated +# from the .pot) or a translator comment (e.g. the ru catalog's legacy +# "# Не переводить"). Honored so a human's deliberate decision is never +# overridden even if a msgid is missing from the registry. +_DO_NOT_TRANSLATE_COMMENT: re.Pattern[str] = re.compile( + r"machine_read-do_not_translate|не\s+переводить" Review Comment: Checked the ru catalog — all 21 `# Не переводить` msgids are already in the registry, so the standard marker lands there automatically on the next `babel_update.sh` run. Keeping catalog regeneration out of this PR; the legacy comments stay honored by `backfill_po.py` regardless. -- 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]
