Skip to content

Commit a920b44

Browse files
committed
Enhance translation file management: Add cleanup function to remove volatile headers and prevent merge conflicts
1 parent 77ab029 commit a920b44

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

doc/release_notes/release_3.13.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11

22
# Version 3.13 #
33

4+
## Version 3.13.5 ##
5+
6+
🛠️ Bug fixes:
7+
8+
* Translation file generation: Ignore POT-Creation-Date and Last-Translator headers to reduce unnecessary diffs
9+
* Added `_cleanup_po_file()` helper function to remove the `POT-Creation-Date` and `Last-Translator` headers from generated `.po` files
10+
* This prevents spurious diffs in version control when regeneration occurs at different times
11+
* Integrated cleanup step into `generate_translation_files()` after `.po` file creation
12+
* Ensures cleaner translation file management and reduces noise in commit history
13+
414
## Version 3.13.4 (2025-12-03) ##
515

616
🛠️ Bug fixes:

guidata/utils/translations.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,39 @@
5252
import argparse
5353
import locale
5454
import os
55+
import re
5556
import subprocess
5657
import sys
5758
import typing
5859

5960
PYBABEL_ARGS = [sys.executable, "-m", "babel.messages.frontend"]
6061

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+
6188

6289
def get_default_language_code() -> str:
6390
"""Get the default language code of the system.
@@ -186,9 +213,13 @@ def scan_translations(
186213
"--init-missing",
187214
"--no-wrap",
188215
"--update-header-comment",
216+
"--ignore-pot-creation-date",
189217
],
190218
check=True,
191219
)
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)
192223
except subprocess.CalledProcessError as e:
193224
print("Error: Translation file generation failed.")
194225
print(e.stderr)

0 commit comments

Comments
 (0)