空下来的时候写了个 python 的翻译包,有需求的可以一用

2016-07-07 21:23:08 +08:00
 NxnXgpuPSfsIT

这个包把提供翻译的几家服务做了个整合,方便 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')

地址

Github 文档

4585 次点击
所在节点    Python
18 条回复
katyang
2016-07-07 21:53:13 +08:00
安装遇到错误
from main import Translation
ImportError: No module named 'main'

红字显示 Command "python setup.py egg_info" failed with error code 1 in ……
vimmer
2016-07-07 22:08:02 +08:00
与`ydcv`有什么区别?
yuyang1110
2016-07-07 22:27:48 +08:00
proxy 并不需要呀。。。 python 用 http_proxy=blah blah python main.py 就好了
NxnXgpuPSfsIT
2016-07-07 22:40:01 +08:00
@yuyang1110 我是写一个要打包成 exe 的程序的时候用到了这个东西,所以就加了 proxy 进去。也方便中途修改吧。
NxnXgpuPSfsIT
2016-07-07 22:40:48 +08:00
@vimmer 这个多了谷歌百度金山?有文档?那个没用过具体不是很清楚
NxnXgpuPSfsIT
2016-07-07 22:45:10 +08:00
@katyang 不好意思,刚好像因为连不上 pypi , py3 的更新没成功传上去,你重新试一下: pip install translation --upgrade
fzinfz
2016-07-07 22:54:19 +08:00
我是来找英语 bug 的...
a ... proxies? ies , es , s......
NxnXgpuPSfsIT
2016-07-07 23:06:10 +08:00
@fzinfz 哈哈,好的。因为参数的 key 是 proxies ,所以将就着只能这样了(摊手)
katyang
2016-07-07 23:40:22 +08:00
@NxnXgpuPSfsIT 下载成功,已经开始使用,谢谢
katyang
2016-07-09 11:44:55 +08:00
@NxnXgpuPSfsIT 想问问,没有加必应翻译是故意的吗?
NxnXgpuPSfsIT
2016-07-09 19:58:50 +08:00
@katyang 不是啦,之后更新版本再加
katyang
2016-07-10 08:18:55 +08:00
NxnXgpuPSfsIT
2016-07-17 21:07:08 +08:00
@katyang 已更新 Bing 翻译
katyang
2016-07-18 07:37:35 +08:00
@NxnXgpuPSfsIT
太好了
我试了
dst = 'zh-CN'
dst = 'zh'
都显示 No language flag
但是 dst='ru' 可以
NxnXgpuPSfsIT
2016-07-18 10:21:53 +08:00
@katyang 支持的语言标志可以看这里: http://translation.readthedocs.io/zh_CN/latest/Language/
katyang
2016-07-18 11:05:21 +08:00
@NxnXgpuPSfsIT 谢谢,已经用上了 :)
GoTop
2016-09-09 17:07:08 +08:00
google 翻译,用了 http 代理还是提示异常呢
请问怎么使用 socks5 代理啊
NxnXgpuPSfsIT
2016-09-13 21:43:15 +08:00
@GoTop 现在版本的 google 翻译应该不用代理也可以用

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/290981

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX