这个包把提供翻译的几家服务做了个整合,方便 python 调用,包括:谷歌、有道、百度、金山。
支持各种源语言和目标语言,这里是语言列表。
支持代理设置,不过目前提供的谷歌翻译暂时不需要使用代理。
这两天刚好用到,写了点代码,干脆做成包,放出来。
代码很简单,源码在github上,有需要自取吧。
安装
pip install translation
使用
基本使用
from translation import baidu, google, youdao, iciba
print(google('hello world!', dst = 'zh-CN'))
print(google('hello world!', dst = 'ru'))
print(baidu('hello world!', dst = 'zh'))
print(baidu('hello world!', dst = 'ru'))
print(youdao('hello world!', dst = 'zh-CN'))
print(iciba('hello world!', dst = 'zh'))
代理
from translation import google, ConnectError
# 127.0.0.1:1080 is a vaild proxies
try:
print(google('hello world!', src = 'auto', dst = 'zh-CN', proxies = {'http': '127.0.0.1:1080'}))
except ConnectError:
print('Invaild proxy')