V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
NxnXgpuPSfsIT
V2EX  ›  Python

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

  •  1
     
  •   NxnXgpuPSfsIT ·
    littlecodersh · 2016-07-07 21:23:08 +08:00 · 4565 次点击
    这是一个创建于 2853 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    18 条回复    2016-09-13 21:43:15 +08:00
    katyang
        1
    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
        2
    vimmer  
       2016-07-07 22:08:02 +08:00
    与`ydcv`有什么区别?
    yuyang1110
        3
    yuyang1110  
       2016-07-07 22:27:48 +08:00
    proxy 并不需要呀。。。 python 用 http_proxy=blah blah python main.py 就好了
    NxnXgpuPSfsIT
        4
    NxnXgpuPSfsIT  
    OP
       2016-07-07 22:40:01 +08:00
    @yuyang1110 我是写一个要打包成 exe 的程序的时候用到了这个东西,所以就加了 proxy 进去。也方便中途修改吧。
    NxnXgpuPSfsIT
        5
    NxnXgpuPSfsIT  
    OP
       2016-07-07 22:40:48 +08:00
    @vimmer 这个多了谷歌百度金山?有文档?那个没用过具体不是很清楚
    NxnXgpuPSfsIT
        6
    NxnXgpuPSfsIT  
    OP
       2016-07-07 22:45:10 +08:00
    @katyang 不好意思,刚好像因为连不上 pypi , py3 的更新没成功传上去,你重新试一下: pip install translation --upgrade
    fzinfz
        7
    fzinfz  
       2016-07-07 22:54:19 +08:00 via iPad
    我是来找英语 bug 的...
    a ... proxies? ies , es , s......
    NxnXgpuPSfsIT
        8
    NxnXgpuPSfsIT  
    OP
       2016-07-07 23:06:10 +08:00
    @fzinfz 哈哈,好的。因为参数的 key 是 proxies ,所以将就着只能这样了(摊手)
    katyang
        9
    katyang  
       2016-07-07 23:40:22 +08:00
    @NxnXgpuPSfsIT 下载成功,已经开始使用,谢谢
    katyang
        10
    katyang  
       2016-07-09 11:44:55 +08:00
    @NxnXgpuPSfsIT 想问问,没有加必应翻译是故意的吗?
    NxnXgpuPSfsIT
        11
    NxnXgpuPSfsIT  
    OP
       2016-07-09 19:58:50 +08:00   ❤️ 1
    @katyang 不是啦,之后更新版本再加
    katyang
        12
    katyang  
       2016-07-10 08:18:55 +08:00
    NxnXgpuPSfsIT
        13
    NxnXgpuPSfsIT  
    OP
       2016-07-17 21:07:08 +08:00   ❤️ 1
    @katyang 已更新 Bing 翻译
    katyang
        14
    katyang  
       2016-07-18 07:37:35 +08:00
    @NxnXgpuPSfsIT
    太好了
    我试了
    dst = 'zh-CN'
    dst = 'zh'
    都显示 No language flag
    但是 dst='ru' 可以
    NxnXgpuPSfsIT
        15
    NxnXgpuPSfsIT  
    OP
       2016-07-18 10:21:53 +08:00
    @katyang 支持的语言标志可以看这里: http://translation.readthedocs.io/zh_CN/latest/Language/
    katyang
        16
    katyang  
       2016-07-18 11:05:21 +08:00
    @NxnXgpuPSfsIT 谢谢,已经用上了 :)
    GoTop
        17
    GoTop  
       2016-09-09 17:07:08 +08:00
    google 翻译,用了 http 代理还是提示异常呢
    请问怎么使用 socks5 代理啊
    NxnXgpuPSfsIT
        18
    NxnXgpuPSfsIT  
    OP
       2016-09-13 21:43:15 +08:00
    @GoTop 现在版本的 google 翻译应该不用代理也可以用
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   979 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:42 · PVG 03:42 · LAX 12:42 · JFK 15:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.