赞
踩
google翻译的水平越来越高,用处还不少,每次通过访问translate.google.cn来复制粘贴觉得效率太慢。
官方的google翻译好像要收费,找了一圈google翻译的免费api,特别是github上面的,发现好些开源项目已经过时,因为google更新了ttk的token。
找了好些java语言发现不可用,最后发现这个python开源项目更新日期挺近的,亲则是可用,免费而且速度很快。
python要求的版本3.6以上, 依赖的库安装 pip install -r requirements.txt
测试用例,可参照traslate_test.py里的用例如下图
源码的下载:https://download.csdn.net/download/wanlitengfei/86757464
更多的用法见下面:
基础用法:
如果没有指定源语言,会自动侦测源语言
- >>> from pygoogletranslation import Translator
- >>> translator = Translator()
- >>> translator.translate('Good Morning', dest='ta')
- # <Translated src=ko dest=ta text=காலை வணக்கம். pronunciation=Good evening.>
- >>> translator.translate('안녕하세요.', dest='ja')
- # <Translated src=ko dest=ja text=こんにちは。 pronunciation=Kon'nichiwa.>
- >>> translator.translate('veritas lux mea', src='la')
- # <Translated src=la dest=en text=The truth is my light pronunciation=The truth is my light>
高级用法(批量):
可以使用数组批量翻译,只是简单调用一个http session
- >>> from pygoogletranslation import Translator
- >>> translator = Translator()
- >>> t = (translator.translate(["Good ' Morning", "India"], dest="ta"))
- >>> for _t in t:
- >>> print(_t.text)
- # காலை வணக்கம்
- # இந்தியா
语言检测:
- >>> from pygoogletranslation import Translator
- >>> translator = Translator()
- >>> translator.detect('காலை வணக்கம்,')
- # <Detected lang=ta confidence=0.72041003>
- >>> translator.detect('この文章は日本語で書かれました。')
- # <Detected lang=ja confidence=0.64889508>
- >>> translator.detect('This sentence is written in English.')
- # <Detected lang=en confidence=0.22348526>
- >>> translator.detect('Tiu frazo estas skribita en Esperanto.')
- # <Detected lang=eo confidence=0.10538048>
翻译文档类型的(.doc, .docx, .pdf, .txt):
- >>> from pygoogletranslation import Translator
- >>> translator = Translator()
- >>> translator.bulktranslate('test.txt', dest="ta")
- # <bulk translated text>
- # for bulk translation, sometimes you might get an error with response
- # code "429" - Too Many attempts.
- # To overcome this error, add below parameter.
- >>> translator = Translator(retry=NO_OF_ATTEMPTS, sleep=WAIT_SECONDS, retry_message=TRUE)
- >>> translator.bulktranslate('test.txt', dest="ta")
- # retry - no of attemps (default- 3 times)
- # sleep - no of attempts after seconds (default- 5 seconds)
- # retry_message - True - display retrying message (default- False)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。