Skip to content

Commit d8906b8

Browse files
committed
script to search for and complete probable 'index entries'
1 parent 89d3e10 commit d8906b8

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

scripts/complete_index.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Script to identify and complete general index entries with original content.
3+
"""
4+
5+
import glob
6+
import sys
7+
8+
import polib
9+
10+
11+
def complete_index(po_files=None):
12+
"""
13+
Identifies general index entries based on source order and completes them
14+
with original content.
15+
16+
args:
17+
po_files: List of .po files to process. If not provided, it processes
18+
all .po files in the current directory and immediate subdirectories.
19+
20+
returns:
21+
str: Files and entries modified.
22+
"""
23+
24+
# Read .po files
25+
if not po_files:
26+
po_files = [f for f in glob.glob("**/*.po", recursive=True)]
27+
28+
modified_texts = []
29+
po_entries = []
30+
for file in po_files:
31+
try:
32+
po_file = polib.pofile(file)
33+
po_entries = [entry for entry in po_file if not entry.obsolete]
34+
val_max = 0
35+
marks = []
36+
37+
# Compare source index with file order and create marks
38+
for i, entry in enumerate(po_entries):
39+
40+
source_index = int(entry.occurrences[0][1])
41+
if source_index <= val_max:
42+
marks.append(i)
43+
val_max = val_max if source_index <= val_max else source_index
44+
45+
# We only keep the entries that are marked
46+
po_entries = [j for i, j in enumerate(po_entries) if i in marks]
47+
48+
# Complete translation with original text
49+
for entry in po_entries:
50+
user_input = input(f"\n{entry}\nIs this a index entry? (y/N):")
51+
if user_input.lower() == "y":
52+
entry.msgstr = entry.msgid
53+
modified_texts.append(f"Adjusted: {file}\n{entry}")
54+
po_file.save()
55+
56+
except Exception as e:
57+
print(f"{len(modified_texts)} text(s) adjusted.",
58+
f"Error! file {file}: {e}\n")
59+
return 1
60+
61+
print(f"\n{len(modified_texts)} text(s) adjusted",
62+
f"{len(po_files)} file(s) processed.")
63+
64+
65+
if __name__ == "__main__":
66+
po_files = sys.argv[1:]
67+
results = complete_index(po_files)
68+
sys.exit(0 if results != 1 else -1)

0 commit comments

Comments
 (0)