|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +"""Drop the python-format flag from messages whose ``%`` is literal text. |
| 19 | +
|
| 20 | +Babel flags a message ``python-format`` whenever its msgid contains something |
| 21 | +that parses as a %-format directive. UI text such as "% of total" or |
| 22 | +"% calculation" parses by accident: ``% o`` and ``% c`` read as a space flag |
| 23 | +plus a conversion character. The app never %-formats these strings, and msgfmt |
| 24 | +rejects any translation that does not repeat the accidental directive. This |
| 25 | +swaps ``python-format`` for ``no-python-format`` on those entries. |
| 26 | +
|
| 27 | +Babel re-adds the flag every time it writes a catalog, so ``babel_update.sh`` |
| 28 | +runs this after its ``pybabel update`` pass, and ``backfill_po.py`` applies |
| 29 | +``is_literal_percent`` when it re-serializes a catalog. Only ``#,`` flag lines |
| 30 | +are rewritten, so the canonical wrapping of the files is untouched. |
| 31 | +
|
| 32 | +Usage: |
| 33 | + python scripts/translations/literal_percent.py [PATH ...] |
| 34 | + # PATH defaults to messages.pot and every language's messages.po |
| 35 | +""" |
| 36 | + |
| 37 | +from __future__ import annotations |
| 38 | + |
| 39 | +import re |
| 40 | +import sys |
| 41 | +from pathlib import Path |
| 42 | + |
| 43 | +from babel.messages.catalog import PYTHON_FORMAT |
| 44 | +from babel.messages.pofile import unescape |
| 45 | + |
| 46 | +TRANSLATIONS_DIR: Path = ( |
| 47 | + Path(__file__).parent.parent.parent / "superset" / "translations" |
| 48 | +) |
| 49 | + |
| 50 | +# A percent sign followed by a space and a word, as in "% of" or "50% off". |
| 51 | +# A real space-flag directive ("% d") is never followed by more letters. |
| 52 | +_LITERAL_PERCENT = re.compile(r"% (?=[A-Za-z]{2})") |
| 53 | + |
| 54 | + |
| 55 | +def is_literal_percent(*msgids: str) -> bool: |
| 56 | + """Return True if Babel sees %-format directives only in literal ``% word`` text. |
| 57 | +
|
| 58 | + Pass the msgid and, for plural entries, the msgid_plural. A message that |
| 59 | + also has a real placeholder (``%(name)s``, ``%s``) keeps its flag. |
| 60 | + """ |
| 61 | + return any(PYTHON_FORMAT.search(msgid) for msgid in msgids) and not any( |
| 62 | + PYTHON_FORMAT.search(_LITERAL_PERCENT.sub(" ", msgid)) for msgid in msgids |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +def _read_field(lines: list[str], i: int, keyword: str) -> tuple[str | None, int]: |
| 67 | + """Read a possibly wrapped ``keyword "..."`` field starting at line ``i``. |
| 68 | +
|
| 69 | + Returns the unescaped value (None if line ``i`` is not that field) and the |
| 70 | + index of the first line after it. |
| 71 | + """ |
| 72 | + if i >= len(lines) or not lines[i].startswith(f'{keyword} "'): |
| 73 | + return None, i |
| 74 | + parts = [lines[i][len(keyword) :].strip()] |
| 75 | + i += 1 |
| 76 | + while i < len(lines) and lines[i].startswith('"'): |
| 77 | + parts.append(lines[i].strip()) |
| 78 | + i += 1 |
| 79 | + return "".join(unescape(part) for part in parts), i |
| 80 | + |
| 81 | + |
| 82 | +def fix_flags(path: Path) -> bool: |
| 83 | + """Rewrite the flags of literal-percent entries in ``path``. |
| 84 | +
|
| 85 | + Returns True if the file changed. |
| 86 | + """ |
| 87 | + lines = path.read_text(encoding="utf-8").splitlines(keepends=True) |
| 88 | + changed = False |
| 89 | + for i, line in enumerate(lines): |
| 90 | + if not line.startswith("#,"): |
| 91 | + continue |
| 92 | + flags = [flag.strip() for flag in line[2:].split(",")] |
| 93 | + if "python-format" not in flags: |
| 94 | + continue |
| 95 | + j = i + 1 |
| 96 | + while j < len(lines) and lines[j].startswith("#|"): |
| 97 | + j += 1 |
| 98 | + _, j = _read_field(lines, j, "msgctxt") |
| 99 | + msgid, j = _read_field(lines, j, "msgid") |
| 100 | + if msgid is None: |
| 101 | + continue |
| 102 | + msgids = [msgid] |
| 103 | + plural, j = _read_field(lines, j, "msgid_plural") |
| 104 | + if plural is not None: |
| 105 | + msgids.append(plural) |
| 106 | + if not is_literal_percent(*msgids): |
| 107 | + continue |
| 108 | + flags = [flag for flag in flags if flag != "python-format"] |
| 109 | + if "no-python-format" not in flags: |
| 110 | + flags.append("no-python-format") |
| 111 | + lines[i] = "#, " + ", ".join(flags) + "\n" |
| 112 | + changed = True |
| 113 | + if changed: |
| 114 | + path.write_text("".join(lines), encoding="utf-8", newline="\n") |
| 115 | + return changed |
| 116 | + |
| 117 | + |
| 118 | +def main(argv: list[str]) -> int: |
| 119 | + paths = [Path(arg) for arg in argv[1:]] or [ |
| 120 | + TRANSLATIONS_DIR / "messages.pot", |
| 121 | + *sorted(TRANSLATIONS_DIR.glob("*/LC_MESSAGES/messages.po")), |
| 122 | + ] |
| 123 | + for path in paths: |
| 124 | + if fix_flags(path): |
| 125 | + print(f"Cleared python-format on literal-percent entries in {path}") |
| 126 | + return 0 |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + sys.exit(main(sys.argv)) |
0 commit comments