I am trying to parse, translate and save XML file. I am stuck with setting up translator from googletrans library in Python (probably).
Here is my code:
import xml.etree.ElementTree as ET
from googletrans import Translator
# Parse the XML file
tree = ET.parse('input.xml')
# Create a Translator object
translator = Translator(src='en', dest='sk')
# Iterate over the elements in the XML document
for elem in tree.iter():
# Check if the element has text content
if elem.text:
# Translate the text content
translated_text = translator.translate(elem.text).text
# Update the text content of the element
elem.text = translated_text
# Save the modified XML document to a file
tree.write('output.xml')
Here is my error:
"line 8, in
translator = Translator(src='en', dest='sk')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^TypeError: Translator.init() got an unexpected keyword argument 'src'"
Can someone tell me, what I have done wrong there? Thanks.
I use DeepL in my project, because I think the translation results matches better for my needs. The limitation, you need a API key, but it’s supported by the company and always updated.