通过 Python 使用谷歌翻译的最佳方式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22587001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:13:29  来源:igfitidea点击:

Best way of using google translation by Python

pythontranslation

提问by lowitty

I am trying to translate large number of text files from English to other several languages. And we use Python in our project, we try to use Google translation service to translate them first then we will correct the mistakes manually.

我正在尝试将大量文本文件从英语翻译成其他几种语言。我们在我们的项目中使用 Python,我们尝试使用谷歌翻译服务先翻译它们,然后我们将手动纠正错误。

I have come up with two ways to translate:

我想出了两种翻译方法:

  1. Use Python Google translation API. Here: goslate 1.1.2: Python Package

  2. Try to program with the google translation page, that is, feed in the text that we want to translate, simulate the HTTP request and process the response. Google Translation

  1. 使用 Python 谷歌翻译 API。此处:goslate 1.1.2:Python 包

  2. 尝试用google翻译页面进行编程,即输入我们要翻译的文本,模拟HTTP请求并处理响应。谷歌翻译

Is anyone have a better offer?

有人有更好的offer吗?

采纳答案by Madison May

Google does in fact have an official translation API with a REST interface. You can check it out here. Note that it is a paid API with no free quota.

实际上,Google 确实有一个带有 REST 接口的官方翻译 API。你可以在这里查看。请注意,它是一个付费 API,没有免费配额。

回答by mou

I made my own google translate function for python ;) try it https://github.com/mouuff/Google-Translate-API

我为 python 制作了自己的谷歌翻译功能;) 试试https://github.com/mouuff/Google-Translate-API

回答by David Noah

Since the origin of this post, connecting to the Google Translate API has become a whole lot easier. That being said, I would still recommend connecting directly to the Google Translate API, but now through it's RapidAPI page here.

自从这篇文章的起源,连接到谷歌翻译 API 变得更加容易。话虽如此,我仍然建议直接连接到 Google Translate API,但现在通过这里的RapidAPI 页面。

You can find out how to obtain an API key here.Just input the API key into the API's function page on Rapid APIand click TEST Function. For example, that's what a basic english to german translation will look like:

您可以在此处了解如何获取 API 密钥只需在 Rapid APIAPI 功能页面中输入 API 密钥,然后单击 TEST Function。例如,这就是基本的英语到德语翻译的样子:

enter image description here

在此处输入图片说明

Just note that deis the language code for German. RapidAPI will generate a code snippet for you so you can just copy and paste the API call directly into your project.

请注意de是德语的语言代码。RapidAPI 将为您生成一个代码片段,因此您可以将 API 调用直接复制并粘贴到您的项目中。

回答by user11620055

One of the simplest ways is to use Selenium for getting the translations of the words and phrases.

最简单的方法之一是使用 Selenium 获取单词和短语的翻译。

Here is a piece of code that gets the word in English and returns the Persian (Farsi) translation. Everything is explained in the readme file on Github:

这是一段获取英文单词并返回波斯语(波斯语)翻译的代码。Github 上的自述文件中解释了所有内容:

https://github.com/mnosrati/Google-Translate-Farsi

https://github.com/mnosrati/Google-Translate-Farsi

回答by Tarun Bhavnani

Try using the googletransmodule. For example:

尝试使用该googletrans模块。例如:

from googletrans import Translator


translator = Translator()  # initalize the Translator object
translations = translator.translate(['see if this helps', 'tarun'], dest='hi')  # translate two phrases to Hindi
for translation in translations:  # print every translation
    print(translation.text)

# Output:
# ????? ?? ?? ??? ???? ??
# ????

The dicts of the supported languages (106) and their ISO639-1 codes:

支持的语言 (106) 及其 ISO639-1 代码的字典:

import googletrans


print(googletrans.LANGCODES)  # {language name: iso639-1 language code}
# or
print(googletrans.LANGUAGES)  # {iso639-1 language code: language name}

See the docsfor more information.

有关更多信息,请参阅文档