1

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.

0

3 Answers 3

1

I tried googletrans, but I got it not to run. I suggest translators instead, it seem more actively developed, too.

Here my test XML Input <- DE:

<?xml version="1.0" encoding="utf-8"?>
<book>
  <text>Bücher sind das Salz in der Suppe</text>
</book>

Python script according your suggestion:

import xml.etree.ElementTree as ET

import translators as ts
import translators.server as tss
from_language, to_language = 'de', 'en'

tree= ET.parse('translator_de.xml')
root = tree.getroot()

ET.dump(root)

for elem in root:
    if elem.text is not None:
        translated_text = tss.google(elem.text, from_language, to_language)
        elem.text = translated_text
        
tree.write('translator_en.xml', encoding='utf-8', xml_declaration=True)
        
ET.dump(root)

Output -> EN:

<?xml version='1.0' encoding='utf-8'?>
<book>
  <text>Books are the salt in the soup</text>
</book>
    
Sign up to request clarification or add additional context in comments.

Comments

1

You cannot use src and dest when creating an instance of Translator. Those parameters are available on the translate() method.

translator = Translator()
...
translated_text = translator.translate(elem.text, src='en', dest='sk').text

See https://py-googletrans.readthedocs.io/en/latest/#googletrans-translator.

6 Comments

To work around "'NoneType' object has no attribute 'group'" error in googletrans 3.0.0, use 3.1.0a0 instead. See github.com/ssut/py-googletrans/issues/280.
I tried this, but still not work. I think this package is outdated. Even the examples from documentation doesn't work on my tests.
@Hermann12: Using 3.1.0a0 worked for me. That is all I can say.
@Hermann12: googletrans 4.0.0rc1 also worked for me.
works with 4.0.0rc1 and python 3.11 as suggested from mzjn, with small change: translator = Translator() and translated_text = translator.translate(elem.text, src='de', dest='en').text.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.