Created
July 7, 2024 08:32
-
-
Save jooray/b1b7597ce49379e7ac600f3399356a6a to your computer and use it in GitHub Desktop.
Diff for Android app translation xml files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A script which takes a reference file (usually English), a translated file and outputs | |
# strings that need to be translated. | |
# | |
# Used for Slovak translation of Phoenix Wallet Android app | |
# Before using, run: | |
# pip install lxml | |
from lxml import etree | |
import sys | |
def extract_untranslated_strings(reference_file, translated_file, output_file): | |
# Parse the reference and translated XML files | |
parser = etree.XMLParser(remove_blank_text=True) | |
reference_tree = etree.parse(reference_file, parser) | |
reference_root = reference_tree.getroot() | |
translated_tree = etree.parse(translated_file, parser) | |
translated_root = translated_tree.getroot() | |
# Dictionary to store translated strings | |
translated_strings = {string.get('name'): string for string in translated_root.findall('.//string')} | |
# Create a new XML tree for untranslated strings | |
new_root = etree.Element('resources') | |
# Iterate over strings in the reference file | |
for string in reference_root.findall('.//string'): | |
string_name = string.get('name') | |
# Include only untranslated strings in the new XML file | |
if string_name not in translated_strings: | |
new_string_element = etree.Element('string') | |
new_string_element.set('name', string_name) | |
new_string_element.text = string.text | |
new_root.append(new_string_element) | |
# Build the new tree | |
new_tree = etree.ElementTree(new_root) | |
# Serialize the new XML tree to the output file, formatted for easy reading | |
with open(output_file, 'wb') as file: | |
new_tree.write(file, pretty_print=True, encoding='utf-8', xml_declaration=True) | |
if __name__ == "__main__": | |
if len(sys.argv) != 4: | |
print("Usage: python translation_diff.py <reference_file> <translated_file> <output_file>") | |
sys.exit(1) | |
reference_file = sys.argv[1] | |
translated_file = sys.argv[2] | |
output_file = sys.argv[3] | |
extract_untranslated_strings(reference_file, translated_file, output_file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment