Generate new language file from base language file and old language file

Tips submitted by PHPMaker users
Post Reply
Posts: 3

Generate new language file from base language file and old language file

Post by mpnunes »

Here is a simple python script to take the base language file, for instance the english version, an old language file, and generates a new file with all the existing translations and at the end the missing items.

import xml.etree.ElementTree as ET

def update_language_file(base_file_path, old_file_path, new_file_path):
    # Parse the base and old files
    tree_base = ET.parse(base_file_path)
    root_base = tree_base.getroot()
    tree_old = ET.parse(old_file_path)
    root_old = tree_old.getroot()
    # Update the version number in the old file to match the base file
    root_old.set('version', root_base.get('version'))
    # Extract phrases into sets for easy comparison
    base_phrases = {child.get('id'): child for child in root_base.find('global')}
    old_phrases = {child.get('id') for child in root_old.find('global')}
    # Find missing phrases
    missing_phrases = set(base_phrases) - old_phrases
    # Append missing phrases to the end of the new file
    for phrase_id in missing_phrases:
        phrase_element = base_phrases[phrase_id]
        comment = ET.Comment(' New phrase added, please translate ')
    # Write the new file
    tree_old.write(new_file_path, encoding='utf-8', xml_declaration=True)

# Example usage
update_language_file('base_file.xml', 'old_file.xml', 'new_file.xml')

Post Reply