|
52 | 52 | import argparse |
53 | 53 | import locale |
54 | 54 | import os |
| 55 | +import re |
55 | 56 | import subprocess |
56 | 57 | import sys |
57 | 58 | import typing |
58 | 59 |
|
59 | 60 | PYBABEL_ARGS = [sys.executable, "-m", "babel.messages.frontend"] |
60 | 61 |
|
| 62 | +# Header lines to remove from .po files to avoid unnecessary merge conflicts |
| 63 | +# when cherry-picking commits between branches |
| 64 | +PO_HEADERS_TO_REMOVE = [ |
| 65 | + "POT-Creation-Date", |
| 66 | + "Last-Translator", |
| 67 | +] |
| 68 | + |
| 69 | + |
| 70 | +def _cleanup_po_file(po_file: str) -> None: |
| 71 | + """Remove volatile header lines from a .po file to avoid merge conflicts. |
| 72 | +
|
| 73 | + Args: |
| 74 | + po_file: Path to the .po file to clean up. |
| 75 | + """ |
| 76 | + with open(po_file, "r", encoding="utf-8") as f: |
| 77 | + content = f.read() |
| 78 | + |
| 79 | + # Build a regex pattern to match and remove the header lines |
| 80 | + for header in PO_HEADERS_TO_REMOVE: |
| 81 | + # Match the header line in the msgstr block (e.g., "POT-Creation-Date: ...\n") |
| 82 | + pattern = rf'"{header}:[^"]*\\n"\n' |
| 83 | + content = re.sub(pattern, "", content) |
| 84 | + |
| 85 | + with open(po_file, "w", encoding="utf-8") as f: |
| 86 | + f.write(content) |
| 87 | + |
61 | 88 |
|
62 | 89 | def get_default_language_code() -> str: |
63 | 90 | """Get the default language code of the system. |
@@ -186,9 +213,13 @@ def scan_translations( |
186 | 213 | "--init-missing", |
187 | 214 | "--no-wrap", |
188 | 215 | "--update-header-comment", |
| 216 | + "--ignore-pot-creation-date", |
189 | 217 | ], |
190 | 218 | check=True, |
191 | 219 | ) |
| 220 | + # Clean up the .po file to remove volatile headers |
| 221 | + po_file = os.path.join(translation_dir, code, "LC_MESSAGES", f"{name}.po") |
| 222 | + _cleanup_po_file(po_file) |
192 | 223 | except subprocess.CalledProcessError as e: |
193 | 224 | print("Error: Translation file generation failed.") |
194 | 225 | print(e.stderr) |
|
0 commit comments