Skip to content

Commit a070cc5

Browse files
committed
update gettex action
1 parent 9f6f2e5 commit a070cc5

File tree

1 file changed

+52
-26
lines changed

1 file changed

+52
-26
lines changed

scripts/update_translations.py

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,65 @@
11
#!/usr/bin/env python3
22
"""
3-
Update translation files preserving POT-Creation-Date.
3+
Update translation files preserving complete header metadata.
44
"""
55
import subprocess
6-
import re
76
from pathlib import Path
87

9-
def get_pot_date(po_file):
10-
"""Extract POT-Creation-Date value from PO file."""
8+
def extract_full_header(po_file):
9+
"""Extract complete header from PO file."""
10+
if not po_file.exists():
11+
return None
12+
13+
header_lines = []
14+
in_header = False
15+
found_msgstr = False
16+
1117
with open(po_file, 'r', encoding='utf-8') as f:
1218
for line in f:
13-
if 'POT-Creation-Date:' in line:
14-
# Extract just the date value
15-
match = re.search(r'POT-Creation-Date: ([^\\]+)', line)
16-
if match:
17-
return match.group(1)
18-
return None
19+
# Detect start of header (first msgid "")
20+
if line.strip() == 'msgid ""' and not in_header:
21+
in_header = True
22+
header_lines.append(line)
23+
# Inside header
24+
elif in_header:
25+
header_lines.append(line)
26+
# Detect msgstr of header
27+
if line.strip() == 'msgstr ""':
28+
found_msgstr = True
29+
# Detect end of header (empty line after msgstr)
30+
elif found_msgstr and line.strip() == '':
31+
break
32+
33+
return ''.join(header_lines) if header_lines else None
1934

20-
def restore_pot_date(po_file, old_date):
21-
"""Restore POT-Creation-Date value in PO file."""
22-
if not old_date:
35+
def restore_full_header(po_file, old_header):
36+
"""Restore complete header in PO file."""
37+
if not old_header:
2338
return
2439

2540
with open(po_file, 'r', encoding='utf-8') as f:
26-
content = f.read()
41+
lines = f.readlines()
42+
43+
# Find where header ends in current file
44+
in_header = False
45+
found_msgstr = False
46+
header_end_idx = 0
2747

28-
# Replace only the date value, keeping the line format
29-
content = re.sub(
30-
r'("POT-Creation-Date: )[^\\]+(\\n")',
31-
r'\g<1>' + old_date + r'\g<2>',
32-
content
33-
)
48+
for idx, line in enumerate(lines):
49+
if line.strip() == 'msgid ""' and not in_header:
50+
in_header = True
51+
elif in_header:
52+
if line.strip() == 'msgstr ""':
53+
found_msgstr = True
54+
elif found_msgstr and line.strip() == '':
55+
header_end_idx = idx + 1
56+
break
3457

35-
with open(po_file, 'w', encoding='utf-8') as f:
36-
f.write(content)
58+
# Replace header with old one
59+
if header_end_idx > 0:
60+
new_content = old_header + ''.join(lines[header_end_idx:])
61+
with open(po_file, 'w', encoding='utf-8') as f:
62+
f.write(new_content)
3763

3864
def main():
3965
po_dir = Path('po')
@@ -56,8 +82,8 @@ def main():
5682
if not po_file.exists():
5783
continue
5884

59-
# Save old POT-Creation-Date
60-
old_pot_date = get_pot_date(po_file)
85+
# Save old header
86+
old_header = extract_full_header(po_file)
6187

6288
# Update PO file
6389
subprocess.run([
@@ -66,8 +92,8 @@ def main():
6692
str(po_file), str(pot_file)
6793
], check=True)
6894

69-
# Restore old POT-Creation-Date
70-
restore_pot_date(po_file, old_pot_date)
95+
# Restore old header
96+
restore_full_header(po_file, old_header)
7197

7298
if __name__ == '__main__':
7399
main()

0 commit comments

Comments
 (0)