86 lines
3.1 KiB
Python
86 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script to apply completed batch translations back to the main django.po file
|
|
"""
|
|
|
|
import re
|
|
|
|
def apply_batch_to_main_po(batch_file_path, main_po_path):
|
|
"""
|
|
Apply translations from a completed batch file to the main django.po file
|
|
"""
|
|
# Read the completed batch file
|
|
with open(batch_file_path, 'r', encoding='utf-8') as f:
|
|
batch_content = f.read()
|
|
|
|
# Extract translations from batch file
|
|
translations = {}
|
|
current_msgid = None
|
|
|
|
lines = batch_content.split('\n')
|
|
i = 0
|
|
while i < len(lines):
|
|
line = lines[i].strip()
|
|
if line.startswith('msgid: "'):
|
|
current_msgid = line[7:-1] # Extract msgid content
|
|
# Look for the Arabic translation line (next non-empty line after "Arabic Translation:")
|
|
j = i + 1
|
|
while j < len(lines) and not lines[j].strip().startswith('msgstr: "'):
|
|
j += 1
|
|
if j < len(lines) and lines[j].strip().startswith('msgstr: "'):
|
|
translation = lines[j].strip()[8:-1] # Extract msgstr content
|
|
if translation and current_msgid and translation != '""': # Only add non-empty translations
|
|
translations[current_msgid] = translation
|
|
print(f"Extracted: '{current_msgid}' -> '{translation}'")
|
|
current_msgid = None
|
|
i += 1
|
|
|
|
print(f"Found {len(translations)} translations to apply")
|
|
|
|
# Read the main django.po file
|
|
with open(main_po_path, 'r', encoding='utf-8') as f:
|
|
main_content = f.read()
|
|
|
|
# Apply translations to main file
|
|
updated_content = main_content
|
|
applied_count = 0
|
|
|
|
for english, arabic in translations.items():
|
|
# Pattern to find msgid followed by empty msgstr
|
|
pattern = rf'(msgid "{re.escape(english)}"\s*\nmsgstr) ""'
|
|
replacement = rf'\1 "{arabic}"'
|
|
|
|
if re.search(pattern, updated_content):
|
|
updated_content = re.sub(pattern, replacement, updated_content)
|
|
applied_count += 1
|
|
print(f"✓ Applied: '{english}' -> '{arabic}'")
|
|
else:
|
|
print(f"✗ Not found: '{english}'")
|
|
|
|
# Write updated content back to main file
|
|
with open(main_po_path, 'w', encoding='utf-8') as f:
|
|
f.write(updated_content)
|
|
|
|
print(f"\nApplied {applied_count} translations to {main_po_path}")
|
|
return applied_count
|
|
|
|
def main():
|
|
"""Main function to apply batch 01 translations"""
|
|
batch_file = "translation_batch_01_completed.txt"
|
|
main_po_file = "locale/ar/LC_MESSAGES/django.po"
|
|
|
|
print("Applying Batch 01 translations to main django.po file...")
|
|
applied_count = apply_batch_to_main_po(batch_file, main_po_file)
|
|
|
|
if applied_count > 0:
|
|
print(f"\n✅ Successfully applied {applied_count} translations!")
|
|
print("Next steps:")
|
|
print("1. Run: python manage.py compilemessages")
|
|
print("2. Test the translations in the application")
|
|
print("3. Continue with the next batch")
|
|
else:
|
|
print("\n❌ No translations were applied. Please check the batch file format.")
|
|
|
|
if __name__ == "__main__":
|
|
main()
|