How to set up Translator from googletrans library?

362 Views Asked by At

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.

3

There are 3 best solutions below

0
On

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.

6
On

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.

0
On

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>