|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Strip stray '#, fuzzy' markers and compile .po files to .mo, in one pass. |
| 3 | +
|
| 4 | +CPython's Tools/i18n/msgfmt.py treats a fuzzy flag on the file header as |
| 5 | +applying to every entry in the file, silently producing an empty .mo |
| 6 | +catalog. Since our translations are complete (not actually rough |
| 7 | +drafts), we strip the marker before compiling. |
| 8 | +
|
| 9 | +Compilation calls msgfmt.make() directly instead of spawning a fresh |
| 10 | +Python interpreter per file, since interpreter startup is the dominant |
| 11 | +cost when compiling hundreds of small .po files. |
| 12 | +
|
| 13 | +Reads paths from stdin (one per line, e.g. via `find ... | python3 |
| 14 | +compile_mo.py`). |
| 15 | +""" |
| 16 | +import sys |
| 17 | +import os |
| 18 | + |
| 19 | +# venv/cpython/Tools/i18n/msgfmt.py, relative to repo root. |
| 20 | +sys.path.insert(0, os.path.join("venv", "cpython", "Tools", "i18n")) |
| 21 | +import msgfmt # noqa: E402 |
| 22 | + |
| 23 | + |
| 24 | +def strip_fuzzy(path: str) -> None: |
| 25 | + with open(path, encoding="utf-8") as f: |
| 26 | + lines = f.readlines() |
| 27 | + |
| 28 | + kept = [line for line in lines if not line.lstrip().startswith("#, fuzzy")] |
| 29 | + |
| 30 | + if kept != lines: |
| 31 | + with open(path, "w", encoding="utf-8") as f: |
| 32 | + f.writelines(kept) |
| 33 | + |
| 34 | + |
| 35 | +def compile_po(path: str) -> None: |
| 36 | + msgfmt.MESSAGES = {} |
| 37 | + out = path[:-3] + ".mo" if path.endswith(".po") else path + ".mo" |
| 38 | + msgfmt.make(path, out) |
| 39 | + |
| 40 | + |
| 41 | +def process(path: str) -> None: |
| 42 | + strip_fuzzy(path) |
| 43 | + compile_po(path) |
| 44 | + |
| 45 | + |
| 46 | +def main() -> None: |
| 47 | + for line in sys.stdin: |
| 48 | + path = line.strip() |
| 49 | + if path: |
| 50 | + process(path) |
| 51 | + |
| 52 | + |
| 53 | +if __name__ == "__main__": |
| 54 | + main() |
0 commit comments