Google Translator 是最好的文本翻译工具之一,其 API 可用于多种编程语言。 因此,您可以用您选择的任何编程语言编写程序,将用某种语言编写的文本翻译成许多其他语言。 在本 Python 教程中,我们将解释如何使用 Google Translate API 用 Python 翻译语言。
我们将编写一个 Python 程序,该程序可以将给定的文本从一种语言翻译成另一种语言。 首先,我们需要为 Python 安装 Google Translate API 或 googletrans 库。
For English translation: Python language translator
安装 Python 谷歌翻译 API
要安装 Google Translate API,我们可以使用 pip install 命令后跟 Google Translate API 名称,即 googletrans:
pip install googletrans
如果不指定版本直接安装googletrans库,pip会为你的Python环境安装googletrans-3.0.0,这个版本存在一些bug。 因此,对于本教程,我们将安装 Python googletrans 库的预发布版本,即 4.0.0rc1。
运行以下 pip install 命令为您的 Python 环境安装 Google Translate API v4.0.0rc1:
pip install googletrans==4.0.0rc1
注意:不要使用 pip install googletrans 安装 googletrans 库,因为它在执行程序时会抛出以下错误。
code = unicode(self.RE_TKK.search(r.text).group(1)).replace('var ', '') AttributeError: 'NoneType' object has no attribute 'group'