Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
script to search for and complete probable 'index entries'
  • Loading branch information
cacrespo committed Oct 23, 2023
commit d8906b889d2a964c704c3ad024e186e172385aeb
68 changes: 68 additions & 0 deletions scripts/complete_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Script to identify and complete general index entries with original content.
"""

import glob
Comment thread
cacrespo marked this conversation as resolved.
Outdated
import sys

import polib


def complete_index(po_files=None):
"""
Identifies general index entries based on source order and completes them
with original content.

args:
po_files: List of .po files to process. If not provided, it processes
all .po files in the current directory and immediate subdirectories.

returns:
str: Files and entries modified.
Comment thread
cacrespo marked this conversation as resolved.
Outdated
"""

# Read .po files
if not po_files:
po_files = [f for f in glob.glob("**/*.po", recursive=True)]
Comment thread
cacrespo marked this conversation as resolved.
Outdated

modified_texts = []
po_entries = []
for file in po_files:
try:
po_file = polib.pofile(file)
po_entries = [entry for entry in po_file if not entry.obsolete]
val_max = 0
marks = []
Comment thread
cacrespo marked this conversation as resolved.
Outdated

# Compare source index with file order and create marks
for i, entry in enumerate(po_entries):
Comment thread
cacrespo marked this conversation as resolved.
Outdated

source_index = int(entry.occurrences[0][1])
Comment thread
cacrespo marked this conversation as resolved.
Outdated
if source_index <= val_max:
marks.append(i)
Comment thread
cacrespo marked this conversation as resolved.
Outdated
val_max = val_max if source_index <= val_max else source_index
Comment thread
cacrespo marked this conversation as resolved.
Outdated

# We only keep the entries that are marked
po_entries = [j for i, j in enumerate(po_entries) if i in marks]
Comment thread
cacrespo marked this conversation as resolved.
Outdated

# Complete translation with original text
for entry in po_entries:
Comment thread
cacrespo marked this conversation as resolved.
Outdated
user_input = input(f"\n{entry}\nIs this a index entry? (y/N):")
if user_input.lower() == "y":
entry.msgstr = entry.msgid
modified_texts.append(f"Adjusted: {file}\n{entry}")
po_file.save()

except Exception as e:
Comment thread
cacrespo marked this conversation as resolved.
print(f"{len(modified_texts)} text(s) adjusted.",
Comment thread
cacrespo marked this conversation as resolved.
Outdated
f"Error! file {file}: {e}\n")
return 1

print(f"\n{len(modified_texts)} text(s) adjusted",
f"{len(po_files)} file(s) processed.")


if __name__ == "__main__":
po_files = sys.argv[1:]
results = complete_index(po_files)
sys.exit(0 if results != 1 else -1)