kaauh_ats/fix_po_duplicates.py
2025-11-03 13:00:12 +03:00

92 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""
Fix duplicate message definitions in .po files
"""
import re
import os
def fix_po_duplicates(po_file):
"""Remove duplicate message definitions from a .po file"""
print(f"Processing {po_file}...")
with open(po_file, 'r', encoding='utf-8') as f:
content = f.read()
# Split into entries
entries = []
current_entry = []
lines = content.split('\n')
i = 0
while i < len(lines):
line = lines[i]
if line.startswith('msgid '):
# Start of new entry
if current_entry:
entries.append('\n'.join(current_entry))
current_entry = [line]
elif line.startswith('msgstr ') and current_entry:
# End of entry
current_entry.append(line)
entries.append('\n'.join(current_entry))
current_entry = []
else:
if current_entry:
current_entry.append(line)
i += 1
# Add the last entry if it exists
if current_entry:
entries.append('\n'.join(current_entry))
# Remove duplicates, keeping the first occurrence
seen_msgids = set()
unique_entries = []
for entry in entries:
# Extract msgid
msgid_match = re.search(r'msgid\s+"([^"]*)"', entry)
if msgid_match:
msgid = msgid_match.group(1)
if msgid not in seen_msgids:
seen_msgids.add(msgid)
unique_entries.append(entry)
else:
print(f" Removing duplicate: {msgid}")
else:
# Header or other entry, keep it
unique_entries.append(entry)
# Rebuild content
new_content = '\n\n'.join(unique_entries)
# Write back to file
with open(po_file, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f" Fixed {po_file} - removed {len(entries) - len(unique_entries)} duplicates")
def main():
"""Fix duplicates in both English and Arabic .po files"""
po_files = [
'locale/ar/LC_MESSAGES/django.po',
'locale/en/LC_MESSAGES/django.po'
]
for po_file in po_files:
if os.path.exists(po_file):
fix_po_duplicates(po_file)
else:
print(f"File not found: {po_file}")
print("\n✅ Duplicate fixing completed!")
print("Now run: python manage.py compilemessages")
if __name__ == "__main__":
main()