From c9dd2ea489db1a5f7c5294a31a17ecbe46a2a2e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammet=20=C5=9E=C3=BCkr=C3=BC=20Demir?= <41967334+dsm@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:36:50 +0300 Subject: [PATCH] add translate_with_deepl.py. --- contrib/translate_with_deepl.py | 48 +++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 contrib/translate_with_deepl.py diff --git a/contrib/translate_with_deepl.py b/contrib/translate_with_deepl.py new file mode 100644 index 000000000..7207270a7 --- /dev/null +++ b/contrib/translate_with_deepl.py @@ -0,0 +1,48 @@ +import os +import xml.etree.ElementTree as ET +import deepl # Import DeepL API + +# Set up DeepL API key +token = 'your_deepL_api_key' # Replace with your DeepL API key +translator = deepl.Translator(token) + +# select target lang +def translate_text(text, source_lang="EN", target_lang="XX"): + """ + Translates text using DeepL API. + """ + try: + # Call DeepL API for translation + result = translator.translate_text(text, source_lang=source_lang, target_lang=target_lang) + return result.text + except Exception as e: + print(f"Error translating text: {e}") + return text # Return the original text in case of an error + +def process_ts_file(input_file, output_file): + """ + Processes a .ts XML file to translate `unfinished` entries. + """ + tree = ET.parse(input_file) + root = tree.getroot() + + for context in root.findall("context"): + for message in context.findall("message"): + source = message.find("source") + translation = message.find("translation") + if translation is not None and translation.get("type") == "unfinished": + if source is not None: + source_text = source.text + # Translate the source text + translated_text = translate_text(source_text) + print(f"Translating: {source_text} -> {translated_text}") + translation.text = translated_text + translation.attrib.pop("type", None) # Remove 'unfinished' attribute + + # Save the updated file + tree.write(output_file, encoding="utf-8", xml_declaration=True) + +# Example usage +input_ts_file = "qucs_xx.ts" # Replace with your input file path +output_ts_file = "qucs_xx_translated.ts" # Replace with your desired output file path +process_ts_file(input_ts_file, output_ts_file)