-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathupdates_po_files.py
55 lines (48 loc) · 1.94 KB
/
updates_po_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Extracts all the translatable strings from all python files in the
# qualcoder subdirectory, then integrates new strings into the
# existing ".po" files, ready to be translated.
# Written by Kai Dröge (and ChatGPT)
import os
import subprocess
def extract_pot_file(directory, pot_filename):
# List all .py files within the specified directory
py_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.py'):
py_files.append(os.path.join(root, file))
# Run xgettext to create the .pot file
if py_files:
try:
subprocess.run(
['xgettext', '--language=Python', '--keyword=_',
'--output', pot_filename] + py_files,
check=True
)
print(f"Extracted POT file: {pot_filename}")
except subprocess.CalledProcessError as exc:
print(f"Error creating POT file: {exc}")
else:
print("No Python files found to extract translatable strings from.")
def update_po_files(directory, pot_filename):
# List all .po files within the specified directory
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.po'):
po_file = os.path.join(root, file)
try:
# Update each .po file using msgmerge
subprocess.run(
['msgmerge', '--update', po_file, pot_filename],
check=True
)
print(f"Updated PO file: {po_file}")
except subprocess.CalledProcessError as exc:
print(f"Error updating PO file {po_file}: {exc}")
def main():
directory = 'qualcoder'
pot_filename = os.path.join(directory, 'qualcoder.pot')
extract_pot_file(directory, pot_filename)
update_po_files(directory, pot_filename)
if __name__ == '__main__':
main()