简体   繁体   English

如何从 googletrans 库设置翻译器?

[英]How to set up Translator from googletrans library?

I am trying to parse, translate and save XML file.我正在尝试解析、翻译和保存 XML 文件。 I am stucked with setting up Translator from googletrans library in Python (probably).我坚持在 Python(可能)中从 googletrans 库设置翻译器。 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'"这是我的错误:“第 8 行,在 translator = Translator(src='en', dest='sk') ^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^ TypeError: Translator.init () got an unexpected keyword argument 'src'”

Can someone tell me, what I have wrong there?有人可以告诉我,我那里有什么问题吗? Thanks.谢谢。

You cannot use src and dest when creating an instance of Translator .创建Translator的实例时不能使用srcdest Those parameters are available on the translate() method.这些参数在translate()方法中可用。

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

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

I tried googletrans, but I got it not to run.我尝试了 googletrans,但无法运行。 I suggest translators instead, it seem more actively developed, too.我建议改为翻译,它似乎也更积极地发展。

Here my test XML Input <- DE:这是我的测试 XML 输入 <- 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: Python 脚本根据您的建议:

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: Output -> ZH:

<?xml version='1.0' encoding='utf-8'?>
<book>
  <text>Books are the salt in the soup</text>
</book>
    

I use DeepL in my project, because I think the translation results matches better for my needs.我在我的项目中使用DeepL ,因为我认为翻译结果更符合我的需求。 The limitation, you need a API key, but it's supported by the company and always updated.限制,你需要一个 API 密钥,但它得到公司的支持并且一直在更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM