-
Notifications
You must be signed in to change notification settings - Fork 17.9k
feat(i18n): do-not-translate registry + '#. do-not-translate' marker standard #41651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+396
−0
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fb8af9c
feat(i18n): skip do-not-translate entries in backfill_po
aminghadersohi 68186a0
test(i18n): add end-to-end DNT skip test; annotate DNT vars
rusackas def156d
feat(i18n): standardize do-not-translate via registry + .pot marker
aminghadersohi f62560f
chore(i18n): add deck.gl Geojson, dttm, p1/p5/p95/p99 to do-not-trans…
aminghadersohi 8a8533a
chore(i18n): harden apply_do_not_translate per review
aminghadersohi e495903
chore(i18n): annotate DO_NOT_TRANSLATE_REGISTRY constant type
aminghadersohi bfead6a
chore(i18n): fail-fast marker step, strip registry lines in backfill_…
rusackas 14abef6
chore(i18n): annotate local variables in apply_do_not_translate
rusackas 3452af9
chore: annotate local variable per review nit
rusackas ec3b871
chore(i18n): standardize the marker as plain '#. do-not-translate'
rusackas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 ``#. 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 = "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: str = raw_line.strip() | ||
| if line and not line.startswith("#"): | ||
| entries.add(line) | ||
| return entries | ||
|
|
||
|
|
||
| def _escape(msgid: str) -> str: | ||
| """Escape a msgid the way gettext writes it on a `msgid "..."` line.""" | ||
| return msgid.replace("\\", "\\\\").replace('"', '\\"') | ||
|
|
||
|
|
||
| def apply_markers(pot_path: Path, registry: set[str]) -> int: | ||
| """Insert the marker comment above each registry msgid via text edit. | ||
|
|
||
| Text manipulation (rather than a polib round-trip) preserves the .pot's | ||
| exact wrapping/layout, so the only change is the added marker lines. | ||
| Idempotent. Returns the number of entries newly marked. | ||
| """ | ||
| lines: list[str] = pot_path.read_text(encoding="utf-8").split("\n") | ||
| targets: set[str] = {f'msgid "{_escape(m)}"' for m in registry} | ||
| out: list[str] = [] | ||
| changed: int = 0 | ||
| for line in lines: | ||
| if line in targets and (not out or out[-1] != _MARKER_LINE): | ||
| # `#.` extracted comments precede `msgid`; these registry entries are | ||
| # bare single-line msgids, so inserting directly above is correct. | ||
| out.append(_MARKER_LINE) | ||
| changed += 1 | ||
| out.append(line) | ||
| if changed: | ||
| pot_path.write_text("\n".join(out), encoding="utf-8") | ||
| return changed | ||
|
|
||
|
|
||
| def main() -> None: | ||
| """Stamp the marker onto the target .pot from the registry.""" | ||
| pot_path: Path = Path(sys.argv[1]) if len(sys.argv) > 1 else DEFAULT_POT | ||
| if not pot_path.exists(): | ||
| print(f"POT file not found: {pot_path}", file=sys.stderr) | ||
| sys.exit(1) | ||
| # Fail fast if the registry file is absent: babel_update.sh depends on this | ||
| # step to stamp the .pot, and continuing would silently publish catalogs | ||
| # without any do-not-translate markers. An existing-but-empty registry is a | ||
| # valid state (nothing to mark), so only a missing file is an error. | ||
| if not REGISTRY.exists(): | ||
| print( | ||
| f"do-not-translate registry not found at {REGISTRY}; refusing to " | ||
| "produce unmarked translation artifacts.", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(1) | ||
| registry: set[str] = load_registry() | ||
| if not registry: | ||
| print( | ||
| f"do-not-translate registry {REGISTRY} is empty; nothing to mark.", | ||
| file=sys.stderr, | ||
| ) | ||
| return | ||
| changed: int = apply_markers(pot_path, registry) | ||
| print( | ||
| f"do-not-translate: marked {changed} new entr(y/ies) with {MARKER} " | ||
| f"in {pot_path.name} ({len(registry)} msgids in registry).", | ||
| file=sys.stderr, | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # 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. | ||
| # | ||
| # Do-not-translate registry. | ||
| # | ||
| # One msgid per line. These strings must never be translated because they are | ||
| # machine-consumed or otherwise must stay identical to the source: icon names, | ||
| # enum/option values, SQL keywords, 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). | ||
| # | ||
| # scripts/translations/apply_do_not_translate.py stamps each of these msgids in | ||
| # messages.pot with a `#. MACHINE_READ-DO_NOT_TRANSLATE` extracted comment | ||
| # (run from babel_update.sh after extraction). pybabel update then propagates | ||
| # that marker into every language catalog, so the do-not-translate status is | ||
| # consistent across languages. scripts/translations/backfill_po.py reads this | ||
| # file (and honors the marker / legacy translator comments) to leave these | ||
| # entries untranslated. Lines starting with '#' and blank lines are ignored. | ||
| 10000 | ||
| DELETE | ||
| ECharts | ||
| EMAIL_REPORTS_CTA | ||
| GROUP BY | ||
| NOT GROUPED BY | ||
| OVERWRITE | ||
| TEMPORAL_RANGE | ||
| WFS | ||
| WMS | ||
| XYZ | ||
| bolt | ||
| crontab | ||
| error_message | ||
| pivoted_xlsx | ||
| schema1,schema2 | ||
| sql | ||
| step-after | ||
| step-before | ||
| superset.example.com | ||
| your-project-1234-a1 | ||
| deck.gl Geojson | ||
| dttm | ||
| p1 | ||
| p5 | ||
| p95 | ||
| p99 |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.