├── .gitignore ├── run.py ├── translation ├── exception.py ├── models │ ├── google.py │ ├── youdao.py │ ├── iciba.py │ ├── bing.py │ ├── __init__.py │ ├── baidu.py │ └── language.py ├── tools.py ├── __init__.py └── main.py ├── LICENSE.txt ├── setup.py ├── docs ├── Exception.md ├── Language.md └── index.md ├── README.md ├── README_EN.md └── README.rst /.gitignore: -------------------------------------------------------------------------------- 1 | build/* 2 | dist/* 3 | translation.egg-info/* 4 | *.pyc 5 | *.swp 6 | -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | from translation import Translation, baidu, google, youdao, iciba, bing 2 | 3 | print(baidu('hello world!', dst = 'zh')) 4 | print(youdao('hello world!', dst = 'zh-CN')) 5 | print(iciba('hello world!', dst = 'zh')) 6 | print(google('hello world!', dst = 'zh-CN')) 7 | print(bing('hello world!', dst = 'zh-CHS')) 8 | -------------------------------------------------------------------------------- /translation/exception.py: -------------------------------------------------------------------------------- 1 | class TranslateError(Exception): 2 | def __init__(self, message): 3 | self.message = message 4 | def __str__(self): 5 | return self.message 6 | 7 | class ConnectError(Exception): 8 | def __init__(self, message): 9 | self.message = message 10 | def __str__(self): 11 | return self.message 12 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | 3 | Copyright (C) 2016 LittleCoder 4 | 5 | Everyone is permitted to copy and distribute verbatim or 6 | modified copies of this program, and changing it is allowed 7 | as long as the name is changed. 8 | 9 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 10 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 11 | 12 | 0. You just DO WHAT THE FUCK YOU WANT TO. 13 | 14 | -------------------------------------------------------------------------------- /translation/models/google.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | headers = { 4 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0', } 5 | BASE_URL = 'http://translate.google.cn/translate_a/t' 6 | params = { 7 | 'client': 'p', 8 | 'ie': 'UTF-8', 9 | 'oe': 'UTF-8', 10 | 'tl': None, 11 | 'sl': None, 12 | 'text': None, } 13 | 14 | def google(text, src, dst, proxies): 15 | params['text'] = text 16 | params['tl'] = dst 17 | params['sl'] = src 18 | r = requests.post(BASE_URL, params, headers = headers, proxies = proxies).json() 19 | return r[0] if isinstance(r, list) else r 20 | 21 | if __name__ == '__main__': 22 | print(google('Test', 'auto', 'zh-CN', {'http': 'http://127.0.0.1:1080'})) 23 | -------------------------------------------------------------------------------- /translation/models/youdao.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | headers = { 4 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0', } 5 | BASE_URL = 'http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule&smartresult=ugc' 6 | params = { 7 | 'i' : None, 8 | 'type' : 'AUTO', 9 | 'doctype' : 'json', 10 | 'xmlVersion' : '1.8', 11 | 'keyfrom' : 'fanyi.web', 12 | 'ue' : 'UTF-8', 13 | 'action' : 'FY_BY_CLICKBUTTON', 14 | 'typoResult' : 'true', } 15 | 16 | def youdao(text, src, dst, proxies): 17 | params['i'] = text 18 | return requests.post(BASE_URL, params, headers = headers, proxies = proxies 19 | ).json()['translateResult'][0][0]['tgt'] 20 | 21 | if __name__ == '__main__': 22 | print(youdao('hello world!', 'auto', 'zh-CN', {})) 23 | -------------------------------------------------------------------------------- /translation/models/iciba.py: -------------------------------------------------------------------------------- 1 | import re, time, json 2 | 3 | import requests 4 | 5 | BASE_URL = 'http://www.iciba.com/index.php' 6 | headers = { 7 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0', } 8 | params = { 9 | 'callback' : 'jQuery183041155304097723566_1468758748486', 10 | 'a' : 'getWordMean', 11 | 'c' : 'search', 12 | 'list' : '1,7,17', 13 | 'word' : None, 14 | '_' : time.time() * 1e3, } 15 | 16 | def iciba(text, src, dst, proxies): 17 | params['word'] = requests.utils.quote(requests.utils.quote(text.encode('utf8'))) 18 | r = requests.get(BASE_URL, params, headers = headers, proxies = proxies) 19 | j = json.loads(re.findall('\(({.*})\)$', r.text)[0]) 20 | return j['baesInfo']['translate_result'] 21 | 22 | if __name__ == '__main__': 23 | print(iciba('this is a test', 'auto', 'zh-CN', {})) 24 | -------------------------------------------------------------------------------- /translation/models/bing.py: -------------------------------------------------------------------------------- 1 | import time, json 2 | 3 | import requests 4 | 5 | BASE_URL = 'http://www.bing.com/translator' 6 | 7 | headers = { 8 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0', 9 | 'Content-Type': 'application/json; charset=UTF-8', } 10 | 11 | def bing(text, src, dst, proxies): 12 | s = requests.session() 13 | s.headers.update(headers) 14 | s.get(BASE_URL, proxies = proxies) 15 | 16 | data = [{ 17 | 'id' : int(time.time() % 1e6), 18 | 'text' : text, }] 19 | data = json.dumps(data, separators = (',', ':')) 20 | url = '%s/api/Translate/TranslateArray?from=%s&to=%s'%( 21 | BASE_URL, '-' if src == 'auto' else src, dst) 22 | j = s.post(url, data = data, proxies = proxies).json() 23 | return j['items'][0]['text'] 24 | 25 | if __name__ == '__main__': 26 | print(bing('this is a test', 'auto', 'zh-CHS', proxies = {'http': 'http://127.0.0.1:1080',})) 27 | -------------------------------------------------------------------------------- /translation/models/__init__.py: -------------------------------------------------------------------------------- 1 | __all__ = ['DEFAULT', 'TRANSLATION_DICT', 2 | 'BASE_URL_LIST', 'languageDict'] 3 | 4 | from . import google, youdao, iciba, baidu, bing 5 | from .language import languageDict 6 | 7 | DEFAULT = 'youdao' 8 | 9 | def translation_wrapper(fn): 10 | def _translation_wrapper(*args, **kwargs): 11 | try: 12 | return fn(*args, **kwargs) 13 | except: 14 | return '' 15 | return _translation_wrapper 16 | 17 | TRANSLATION_DICT = { 18 | 'google' : translation_wrapper(google.google), 19 | 'youdao' : translation_wrapper(youdao.youdao), 20 | 'iciba' : translation_wrapper(iciba.iciba), 21 | 'baidu' : translation_wrapper(baidu.baidu), 22 | 'bing' : translation_wrapper(bing.bing), } 23 | 24 | BASE_URL_LIST = { 25 | 'google' : google.BASE_URL, 26 | 'youdao' : youdao.BASE_URL, 27 | 'iciba' : iciba.BASE_URL, 28 | 'baidu' : baidu.BASE_URL, 29 | 'bing' : bing.BASE_URL, } 30 | 31 | for k in TRANSLATION_DICT.keys(): 32 | if k not in languageDict.keys(): 33 | del TRANSLATION_DICT[k] 34 | 35 | for k in BASE_URL_LIST.keys(): 36 | if k not in languageDict.keys(): 37 | del BASE_URL_LIST[k] 38 | -------------------------------------------------------------------------------- /translation/tools.py: -------------------------------------------------------------------------------- 1 | import requests 2 | 3 | from .exception import TranslateError, ConnectError 4 | from .models import TRANSLATION_DICT, BASE_URL_LIST, languageDict 5 | 6 | def verify_language_flag(flag, defaultLanguage): 7 | # Whether translation is provided 8 | if defaultLanguage not in TRANSLATION_DICT.keys(): 9 | raise TranslateError('No such translation: ' + defaultLanguage) 10 | # Whether language flag is available 11 | if flag not in languageDict[defaultLanguage].keys(): 12 | raise TranslateError('No language flag in %s named: %s'%( 13 | defaultLanguage, flag)) 14 | 15 | def test_proxies(proxies, defaultLanguage): 16 | try: 17 | requests.get(BASE_URL_LIST[defaultLanguage], proxies = proxies, timeout = 3) 18 | except requests.exceptions.ConnectionError as e: 19 | raise ConnectError(str(e.message)) 20 | except requests.exceptions.ConnectTimeout: 21 | raise ConnectError('Proxies can\'t work properly, \ 22 | you need to set or change a proxy') 23 | except requests.exceptions.ProxyError: 24 | raise ConnectError('Proxies can\'t work properly, \ 25 | you need to set or change a proxy') 26 | except AttributeError: 27 | raise ConnectError('Proxies have wrong format') 28 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | from codecs import open 3 | from os import path 4 | import translation 5 | 6 | here = path.abspath(path.dirname(__file__)) 7 | 8 | with open(path.join(here, 'README.rst'), encoding='utf-8') as f: 9 | long_description = f.read() 10 | 11 | setup( 12 | name='translation', 13 | 14 | version=translation.__version__, 15 | 16 | description='A translation package', 17 | 18 | long_description=long_description, 19 | 20 | url='https://github.com/littlecodersh/translation', 21 | 22 | author='LittleCoder', 23 | author_email='i7meavnktqegm1b@qq.com', 24 | 25 | license='MIT', 26 | 27 | classifiers=[ 28 | 'Development Status :: 3 - Alpha', 29 | 30 | 'Intended Audience :: Developers', 31 | 'Topic :: Software Development :: Libraries :: Python Modules', 32 | 33 | 'License :: OSI Approved :: MIT License', 34 | 35 | 'Programming Language :: Python :: 2.7', 36 | 'Programming Language :: Python :: 3.5', 37 | ], 38 | 39 | keywords='translate translation', 40 | 41 | # You can just specify the packages manually here if your project is 42 | # simple. Or you can use find_packages(). 43 | packages=find_packages(), 44 | 45 | install_requires=['requests'], 46 | 47 | # List additional groups of dependencies here 48 | extras_require={}, 49 | ) 50 | -------------------------------------------------------------------------------- /translation/__init__.py: -------------------------------------------------------------------------------- 1 | __version__ = '1.0.5' 2 | __all__ = ['Translation', 'TranslateError', 'ConnectError', 3 | 'set_default_translation', 'set_default_language', 4 | 'set_default_proxies', 'get', 'get_all', 5 | 'google', 'youdao', 'iciba', 'baidu', 'bing'] 6 | 7 | from .main import Translation 8 | from .exception import TranslateError, ConnectError 9 | 10 | t = Translation() 11 | set_default_translation = t.set_default_translation 12 | set_default_language = t.set_default_language 13 | set_default_proxies = t.set_default_proxies 14 | get = t.get 15 | get_all = t.get_all 16 | 17 | def google(text, src = None, dst = None, proxies = None): 18 | return t.get(text, default = 'google', src = src, 19 | dst = dst, proxies = proxies) 20 | def youdao(text, src = None, dst = None, proxies = None): 21 | return t.get(text, default = 'youdao', src = src, 22 | dst = dst, proxies = proxies) 23 | def iciba(text, src = None, dst = None, proxies = None): 24 | return t.get(text, default = 'iciba', src = src, 25 | dst = dst, proxies = proxies) 26 | def baidu(text, src = None, dst = None, proxies = None): 27 | return t.get(text, default = 'baidu', src = src, 28 | dst = dst, proxies = proxies) 29 | def bing(text, src = None, dst = None, proxies = None): 30 | return t.get(text, default = 'bing', src = src, 31 | dst = dst, proxies = proxies) 32 | -------------------------------------------------------------------------------- /translation/models/baidu.py: -------------------------------------------------------------------------------- 1 | #coding=utf8 2 | import time, re, json 3 | 4 | import requests 5 | 6 | BASE_URL = 'http://fanyi.baidu.com/v2transapi' 7 | 8 | headers = { 9 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0', } 10 | def correct_txt(text): 11 | url = 'http://correctxt.baidu.com/correctxt' 12 | jQueryFlag = 'jQuery1113014284725192946546_1466505692432' 13 | params = { 14 | 'text': text, 15 | 'callback': jQueryFlag, 16 | 'ie': 'utf-8', 17 | 'version': 0, 18 | 'from': 'FanyiWeb', 19 | '_': int(time.time()*1e3), } 20 | t = requests.get(url, params, headers = headers).text 21 | try: 22 | return json.loads(re.search('%s\((.*?)\)$'%jQueryFlag, t).groups()[0])['correctxt'] 23 | except (AttributeError, ValueError): 24 | return '' 25 | def baidu(text, src, dst, proxies): 26 | text = correct_txt(text) 27 | if text == '': return '' 28 | url = BASE_URL 29 | data = { 30 | 'from': src, 31 | 'to': dst, 32 | 'query': text, 33 | 'transtype': 'realtime', 34 | 'simple_means_flag': '3', } 35 | return requests.post(url, data, headers = headers, proxies = proxies 36 | ).json()['trans_result']['data'][0]['result'][0][1] 37 | 38 | if __name__ == '__main__': 39 | print(baidu(u'测试', 'auto', 'en', {})) 40 | -------------------------------------------------------------------------------- /translation/main.py: -------------------------------------------------------------------------------- 1 | from .models import TRANSLATION_DICT, DEFAULT 2 | from .tools import verify_language_flag, test_proxies 3 | from .exception import TranslateError 4 | 5 | class Translation(object): 6 | def __init__(self, default = [DEFAULT], src = 'auto', 7 | dst = 'zh-CN', proxies = {}): 8 | self.set_default_translation(default) 9 | self.set_default_language(src, dst) 10 | self.set_default_proxies(proxies) 11 | def set_default_translation(self, default): 12 | if isinstance(default, str): default = [default] 13 | self.default = [] 14 | for i in default: 15 | if i in TRANSLATION_DICT.keys(): 16 | self.default.append(i) 17 | if not self.default: self.default = [DEFAULT] 18 | def set_default_language(self, src, dst): 19 | self.src = src 20 | self.dst = dst 21 | def set_default_proxies(self, proxies): 22 | self.proxies = proxies 23 | def get(self, text, default = None, src = None, 24 | dst = None, proxies = None): 25 | if text == '': return '' 26 | default = default or self.default[0] 27 | src = src or self.src 28 | dst = dst or self.dst 29 | proxies = proxies or self.proxies 30 | for i in (src, dst): verify_language_flag(i, default) 31 | test_proxies(proxies, default) 32 | r = TRANSLATION_DICT[default](text, src, dst, proxies) 33 | if r == '': raise TranslateError('No translation get, you may retry') 34 | return r 35 | def get_all(self, text, default = None, src = None, 36 | dst = None, proxies = None): 37 | d = {} 38 | for t in (default or self.default): d[t] = self.get(text, t, src, dst, proxies) 39 | return d 40 | -------------------------------------------------------------------------------- /docs/Exception.md: -------------------------------------------------------------------------------- 1 | ## TranslateError 2 | 3 | 以下两种情况将会抛出`TranslationError`: 4 | * 使用了错误的指定翻译(仅支持谷歌、有道、百度、金山) 5 | * 使用了错误的语言flag 6 | 7 | **使用了错误的制定翻译** 8 | 9 | ```python 10 | from translation import get, TranslateError 11 | 12 | try: 13 | print(get('hello', default = 'humanbrain', dst = 'zh')) 14 | except TranslateError as e: 15 | print(e.message) 16 | 17 | # 将会显示:No such translation: humanbrain 18 | ``` 19 | 20 | **使用了错误的语言flag** 21 | 22 | ```python 23 | from translation import get, TranslateError 24 | 25 | try: 26 | print(get('hello', default = 'baidu', dst = 'zh-CN')) 27 | except TranslateError as e: 28 | print(e.message) 29 | 30 | # 将会显示:No language flag in baidu named: zh-CN 31 | # 这是因为baidu这里官方认可的中文的简称为`zh`,我这里未作修改 32 | ``` 33 | 34 | ## ConnectError 35 | 36 | 以下两种情况会抛出`ConnectError` 37 | * 无法连接网络或者网络连接时间过长 38 | * 代理服务器不可用 39 | 40 | **无法连接网络或者网络连接时间过长** 41 | 42 | ```python 43 | # 比如拔掉了网线以后 = = 44 | 45 | from translation import get, ConnectError 46 | 47 | try: 48 | print(get('hello world!', default = 'baidu', dst = 'zh')) 49 | except ConnectError as e: 50 | print(e.message) 51 | 52 | # 会显示: 53 | # HTTPConnectionPool(host='fanyi.baidu.com', port=80): Max retries exceeded 54 | # with url: /v2transapi (Caused by NewConnectionError(': Failed to establi 56 | # sh a new connection: [Errno 11001] getaddrinfo failed',)) 57 | ``` 58 | 59 | **代理服务器不可用** 60 | 61 | ```python 62 | # 比如 127.0.0.1:9012 不是你的代理端口 63 | 64 | from translation import get, ConnectError 65 | 66 | try: 67 | print(get('hello world!', default = 'baidu', dst = 'zh', proxies = {'http': '127.0.0.1:9012'})) 68 | except ConnectError as e: 69 | print(e.message) 70 | 71 | # 会显示: 72 | # HTTPConnectionPool(host='127.0.0.1', port=9012): Max retries exceeded with url: 73 | # http://fanyi.baidu.com/v2transapi (Caused by ProxyError('Cannot connect to proxy 74 | # .', NewConnectionError(': Failed to establish a new connection: [Errno 10061] ',))) 76 | ``` 77 | -------------------------------------------------------------------------------- /translation/models/language.py: -------------------------------------------------------------------------------- 1 | languageDict = { 2 | 'google': {"el": "Greek", "eo": "Esperanto", "en": "English", "af": "Afrikaans", "sw": "Swahili", "ca": "Catalan", "it": "Italian", "iw": "Hebrew", "sv": "Swedish", "cs": "Czech", "cy": "Welsh", "ar": "Arabic", "ur": "Urdu", "ga": "Irish", "eu": "Basque", "et": "Estonian", "az": "Azerbaijani", "id": "Indonesian", "es": "Spanish", "ru": "Russian", "gl": "Galician", "nl": "Dutch", "pt": "Portuguese", "la": "Latin", "tr": "Turkish", "tl": "Filipino", "lv": "Latvian", "lt": "Lithuanian", "th": "Thai", "vi": "Vietnamese", "gu": "Gujarati", "ro": "Romanian", "is": "Icelandic", "pl": "Polish", "ta": "Tamil", "yi": "Yiddish", "be": "Belarusian", "fr": "French", "bg": "Bulgarian", "uk": "Ukrainian", "hr": "Croatian", "bn": "Bengali", "sl": "Slovenian", "ht": "Haitian Creole", "da": "Danish", "fa": "Persian", "hi": "Hindi", "fi": "Finnish", "hu": "Hungarian", "ja": "Japanese", "ka": "Georgian", "te": "Telugu", "zh-TW": "Chinese Traditional", "sq": "Albanian", "no": "Norwegian", "ko": "Korean", "kn": "Kannada", "mk": "Macedonian", "zh-CN": "Chinese Simplified", "sk": "Slovak", "mt": "Maltese", "de": "German", "ms": "Malay", "sr": "Serbian", "auto": "Auto"}, 3 | 4 | 'baidu': {"el": "\u5e0c\u814a\u8bed", "en": "\u82f1\u8bed", "zh": "\u4e2d\u6587", "rom": "\u7f57\u9a6c\u5c3c\u4e9a\u8bed", "it": "\u610f\u5927\u5229\u8bed", "ara": "\u963f\u62c9\u4f2f\u8bed", "cs": "\u6377\u514b\u8bed", "spa": "\u897f\u73ed\u7259\u8bed", "ru": "\u4fc4\u8bed", "fra": "\u6cd5\u8bed", "est": "\u7231\u6c99\u5c3c\u4e9a\u8bed", "nl": "\u8377\u5170\u8bed", "pt": "\u8461\u8404\u7259\u8bed", "swe": "\u745e\u5178\u8bed", "cht": "\u7e41\u4f53\u4e2d\u6587", "th": "\u6cf0\u8bed", "kor": "\u97e9\u8bed", "fin": "\u82ac\u5170\u8bed", "pl": "\u6ce2\u5170\u8bed", "auto": "\u81ea\u52a8\u68c0\u6d4b", "dan": "\u4e39\u9ea6\u8bed", "de": "\u5fb7\u8bed", "jp": "\u65e5\u8bed", "hu": "\u5308\u7259\u5229\u8bed", "bul": "\u4fdd\u52a0\u5229\u4e9a\u8bed", "wyw": "\u6587\u8a00\u6587", "slo": "\u65af\u6d1b\u6587\u5c3c\u4e9a\u8bed", "yue": "\u7ca4\u8bed"}, 5 | 6 | 'iciba': {"fr": "\u6cd5\u8bed", "en": "\u82f1\u6587", "zh": "\u4e2d\u6587", "de": "\u5fb7\u8bed", "ko": "\u97e9\u8bed", "ja": "\u65e5\u8bed", "es": "\u897f\u73ed\u7259\u8bed", "auto": "Auto"}, 7 | 8 | 'youdao': {"ru": "\u4fc4\u8bed", "fr": "\u6cd5\u8bed", "en": "\u82f1\u6587", "kr": "\u97e9\u8bed", "sp": "\u897f\u73ed\u7259\u8bed", "zh-CN": "\u4e2d\u6587", "ja": "\u65e5\u8bed", "auto": "Auto"}, 9 | 10 | 'bing': {"-": "Auto", "auto": "Auto", "el": "Greek", "zh-CHS": "Chinese Simplified", "zh-CHT": "Chinese Traditional", "af": "Afrikaans", "ko": "Korean", "ca": "Catalan", "it": "Italian", "cy": "Welsh", "ar": "Arabic", "otq": "Quer\u00e9taro Otomi", "cs": "Czech", "et": "Estonian", "id": "Indonesian", "es": "Spanish", "ru": "Russian", "mww": "Hmong Daw", "pt": "Portuguese", "no": "Norwegian", "tr": "Turkish", "lv": "Latvian", "lt": "Lithuanian", "en": "English", "th": "Thai", "vi": "Vietnamese", "ro": "Romanian", "pl": "Polish", "fr": "French", "bg": "Bulgarian", "uk": "Ukrainian", "hr": "Croatian", "sr-Cyrl": "Serbian (Cyrillic)", "de": "German", "ht": "Haitian Creole", "da": "Danish", "fa": "Persian", "hi": "Hindi", "fi": "Finnish", "hu": "Hungarian", "ja": "Japanese", "he": "Hebrew", "bs-Latn": "Bosnian (Latin)", "tlh": "Klingon", "sw": "Kiswahili", "sv": "Swedish", "ur": "Urdu", "sk": "Slovak", "mt": "Maltese", "tlh-Qaak": "Klingon (pIqaD)", "sr-Latn": "Serbian (Latin)", "ms": "Malay", "sl": "Slovenian", "yua": "Yucatec Maya", "nl": "Dutch"}, 11 | } 12 | 13 | -------------------------------------------------------------------------------- /docs/Language.md: -------------------------------------------------------------------------------- 1 | 语言列表更新时间: 160627 2230h 2 | 3 | 注意,其中金山词霸,有道翻译仅支持文档给出的语言翻译为中文。 4 | 5 | **Google** 6 | ``` 7 | el : Greek, 8 | eo : Esperanto, 9 | en : English, 10 | af : Afrikaans, 11 | sw : Swahili, 12 | ca : Catalan, 13 | it : Italian, 14 | iw : Hebrew, 15 | sv : Swedish, 16 | cs : Czech, 17 | cy : Welsh, 18 | ar : Arabic, 19 | ur : Urdu, 20 | ga : Irish, 21 | eu : Basque, 22 | et : Estonian, 23 | az : Azerbaijani, 24 | id : Indonesian, 25 | es : Spanish, 26 | ru : Russian, 27 | gl : Galician, 28 | nl : Dutch, 29 | pt : Portuguese, 30 | la : Latin, 31 | tr : Turkish, 32 | tl : Filipino, 33 | lv : Latvian, 34 | lt : Lithuanian, 35 | th : Thai, 36 | vi : Vietnamese, 37 | gu : Gujarati, 38 | ro : Romanian, 39 | is : Icelandic, 40 | pl : Polish, 41 | ta : Tamil, 42 | yi : Yiddish, 43 | be : Belarusian, 44 | fr : French, 45 | bg : Bulgarian, 46 | uk : Ukrainian, 47 | hr : Croatian, 48 | bn : Bengali, 49 | sl : Slovenian, 50 | ht : Haitian Creole, 51 | da : Danish, 52 | fa : Persian, 53 | hi : Hindi, 54 | fi : Finnish, 55 | hu : Hungarian, 56 | ja : Japanese, 57 | ka : Georgian, 58 | te : Telugu, 59 | zh-TW : Chinese Traditional, 60 | sq : Albanian, 61 | no : Norwegian, 62 | ko : Korean, 63 | kn : Kannada, 64 | mk : Macedonian, 65 | zh-CN : Chinese Simplified, 66 | sk : Slovak, 67 | mt : Maltese, 68 | de : German, 69 | ms : Malay, 70 | sr : Serbian 71 | ``` 72 | 73 | **bing** 74 | ``` 75 | af : Afrikaans 76 | ar : Arabic 77 | bs-Latn : Bosnian (Latin) 78 | bg : Bulgarian 79 | ca : Catalan 80 | zh-CHS : Chinese Simplified 81 | zh-CHT : Chinese Traditional 82 | hr : Croatian 83 | cs : Czech 84 | da : Danish 85 | nl : Dutch 86 | en : English 87 | et : Estonian 88 | fi : Finnish 89 | fr : French 90 | de : German 91 | el : Greek 92 | ht : Haitian Creole 93 | he : Hebrew 94 | hi : Hindi 95 | mww : Hmong Daw 96 | hu : Hungarian 97 | id : Indonesian 98 | it : Italian 99 | ja : Japanese 100 | sw : Kiswahili 101 | tlh : Klingon 102 | tlh-Qaak : Klingon (pIqaD) 103 | ko : Korean 104 | lv : Latvian 105 | lt : Lithuanian 106 | ms : Malay 107 | mt : Maltese 108 | no : Norwegian 109 | fa : Persian 110 | pl : Polish 111 | pt : Portuguese 112 | otq : Querétaro Otomi 113 | ro : Romanian 114 | ru : Russian 115 | sr-Cyrl : Serbian (Cyrillic) 116 | sr-Latn : Serbian (Latin) 117 | sk : Slovak 118 | sl : Slovenian 119 | es : Spanish 120 | sv : Swedish 121 | th : Thai 122 | tr : Turkish 123 | uk : Ukrainian 124 | ur : Urdu 125 | vi : Vietnamese 126 | cy : Welsh 127 | yua : Yucatec Maya 128 | ``` 129 | 130 | **Baidu** 131 | ``` 132 | auto 自动检测 133 | zh 中文 134 | en 英语 135 | yue 粤语 136 | wyw 文言文 137 | jp 日语 138 | kor 韩语 139 | fra 法语 140 | spa 西班牙语 141 | th 泰语 142 | ara 阿拉伯语 143 | ru 俄语 144 | pt 葡萄牙语 145 | de 德语 146 | it 意大利语 147 | el 希腊语 148 | nl 荷兰语 149 | pl 波兰语 150 | bul 保加利亚语 151 | est 爱沙尼亚语 152 | dan 丹麦语 153 | fin 芬兰语 154 | cs 捷克语 155 | rom 罗马尼亚语 156 | slo 斯洛文尼亚语 157 | swe 瑞典语 158 | hu 匈牙利语 159 | cht 繁体中文 160 | ``` 161 | 162 | **iciba** 163 | ``` 164 | zh 中文 165 | ja 日语 166 | es 西班牙语 167 | en 英文 168 | ko 韩语 169 | fr 法语 170 | de 德语 171 | ``` 172 | 173 | **youdao** 174 | ``` 175 | zh-CN 中文 176 | en 英文 177 | ja 日语 178 | kr 韩语 179 | ru 俄语 180 | sp 西班牙语 181 | fr 法语 182 | ``` 183 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # translation 2 | 3 | ![python27](https://img.shields.io/badge/python-2.7-ff69b4.svg) ![python35](https://img.shields.io/badge/python-3.5-green.svg) [English version](https://github.com/littlecodersh/translation/blob/master/README_EN.md) 4 | 5 | translation是一个基于网页端翻译的python翻译包。 6 | 7 | 提供基本的谷歌、有道、百度、金山翻译服务。 8 | 9 | 目前提供的谷歌的翻译服务暂时不需要使用代理。 10 | 11 | ## Installation 12 | 13 | ```bash 14 | pip install translation 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```python 20 | from translation import baidu, google, youdao, iciba 21 | 22 | print(google('hello world!', dst = 'zh-CN')) 23 | print(google('hello world!', dst = 'ru')) 24 | print(baidu('hello world!', dst = 'zh')) 25 | print(baidu('hello world!', dst = 'ru')) 26 | print(youdao('hello world!', dst = 'zh-CN')) 27 | print(iciba('hello world!', dst = 'zh')) 28 | print(bing('hello world!', dst = 'zh-CHS')) 29 | ``` 30 | 31 | ## Documents 32 | 33 | 你可以在[这里](http://translation.readthedocs.io/zh_CN/latest/)获取api的使用帮助。 34 | 35 | ## Advanced usage 36 | 37 | ### Proxies 38 | 39 | 你可能无法在国内使用谷歌或者在国外使用有道和金山的翻译。 40 | 41 | 这是你可以尝试使用代理。 42 | 43 | ```python 44 | from translation import google, ConnectError 45 | 46 | # 127.0.0.1:1080 is a vaild proxies 47 | try: 48 | print(google('hello world!', dst = 'zh-CN', proxies = {'http': '127.0.0.1:1080'})) 49 | except ConnectError: 50 | print('Invaild proxy') 51 | ``` 52 | 53 | ### Default 54 | 55 | 你可以更改默认的设置,可更改的设置包括: 56 | * 默认的源语言(不修改则会自动识别) 57 | * 默认的目标语言(不修改则为中文) 58 | * 默认的首选翻译(不修改则为有道) 59 | * 默认的代理(不修改则为不使用代理) 60 | 61 | ```python 62 | from translation import (set_default_translation, set_default_language, 63 | set_default_proxies, get, ConnectError) 64 | 65 | set_default_translation('google') 66 | set_default_language('auto', 'zh-CN') 67 | set_default_proxies({'http': '127.0.0.1:1080'}) 68 | try: 69 | print(get('hello world!')) 70 | except ConnectError: 71 | print('Invaild proxy') 72 | ``` 73 | 74 | ### More 75 | 76 | 更多的功能可以参考[文档](http://translation.readthedocs.io/zh_CN/latest/)。 77 | 78 | ## Language 79 | 80 | [文档](http://translation.readthedocs.io/zh_CN/latest/)中有详细的支持语言的列表,这里仅给出谷歌支持语言的标记列表。 81 | 82 | 其中金山词霸,有道翻译仅支持文档给出的语言翻译为中文。 83 | 84 | **Google** 85 | ``` 86 | el : Greek, 87 | eo : Esperanto, 88 | en : English, 89 | af : Afrikaans, 90 | sw : Swahili, 91 | ca : Catalan, 92 | it : Italian, 93 | iw : Hebrew, 94 | sv : Swedish, 95 | cs : Czech, 96 | cy : Welsh, 97 | ar : Arabic, 98 | ur : Urdu, 99 | ga : Irish, 100 | eu : Basque, 101 | et : Estonian, 102 | az : Azerbaijani, 103 | id : Indonesian, 104 | es : Spanish, 105 | ru : Russian, 106 | gl : Galician, 107 | nl : Dutch, 108 | pt : Portuguese, 109 | la : Latin, 110 | tr : Turkish, 111 | tl : Filipino, 112 | lv : Latvian, 113 | lt : Lithuanian, 114 | th : Thai, 115 | vi : Vietnamese, 116 | gu : Gujarati, 117 | ro : Romanian, 118 | is : Icelandic, 119 | pl : Polish, 120 | ta : Tamil, 121 | yi : Yiddish, 122 | be : Belarusian, 123 | fr : French, 124 | bg : Bulgarian, 125 | uk : Ukrainian, 126 | hr : Croatian, 127 | bn : Bengali, 128 | sl : Slovenian, 129 | ht : Haitian Creole, 130 | da : Danish, 131 | fa : Persian, 132 | hi : Hindi, 133 | fi : Finnish, 134 | hu : Hungarian, 135 | ja : Japanese, 136 | ka : Georgian, 137 | te : Telugu, 138 | zh-TW : Chinese Traditional, 139 | sq : Albanian, 140 | no : Norwegian, 141 | ko : Korean, 142 | kn : Kannada, 143 | mk : Macedonian, 144 | zh-CN : Chinese Simplified, 145 | sk : Slovak, 146 | mt : Maltese, 147 | de : German, 148 | ms : Malay, 149 | sr : Serbian 150 | ``` 151 | 152 | ## Comments 153 | 154 | 如果有什么问题或者建议都可以在这个[Issue](https://github.com/littlecodersh/translation/issues/1)和我讨论。 155 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # translation 2 | 3 | ![python27](https://img.shields.io/badge/python-2.7-ff69b4.svg) ![python35](https://img.shields.io/badge/python-3.5-green.svg) [English version](https://github.com/littlecodersh/translation/blob/master/README_EN.md) 4 | 5 | translation是一个基于网页端翻译的python翻译包。 6 | 7 | 提供基本的谷歌、有道、百度、金山翻译服务。 8 | 9 | 目前提供的谷歌的翻译服务暂时不需要使用代理。 10 | 11 | ## Installation 12 | 13 | ```bash 14 | pip install translation 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```python 20 | from translation import baidu, google, youdao, iciba 21 | 22 | print(google('hello world!', dst = 'zh-CN')) 23 | print(google('hello world!', dst = 'ru')) 24 | print(baidu('hello world!', dst = 'zh')) 25 | print(baidu('hello world!', dst = 'ru')) 26 | print(youdao('hello world!', dst = 'zh-CN')) 27 | print(iciba('hello world!', dst = 'zh')) 28 | print(bing('hello world!', dst = 'zh-CHS')) 29 | ``` 30 | 31 | ## Documents 32 | 33 | 你可以在[这里](http://translation.readthedocs.io/zh_CN/latest/)获取api的使用帮助。 34 | 35 | ## Advanced usage 36 | 37 | ### Proxies 38 | 39 | 你可能无法在国内使用谷歌或者在国外使用有道和金山的翻译。 40 | 41 | 这是你可以尝试使用代理。 42 | 43 | ```python 44 | from translation import google, ConnectError 45 | 46 | # 127.0.0.1:1080 is a vaild proxies 47 | try: 48 | print(google('hello world!', dst = 'zh-CN', proxies = {'http': '127.0.0.1:1080'})) 49 | except ConnectError: 50 | print('Invaild proxy') 51 | ``` 52 | 53 | ### Default 54 | 55 | 你可以更改默认的设置,可更改的设置包括: 56 | * 默认的源语言(不修改则会自动识别) 57 | * 默认的目标语言(不修改则为中文) 58 | * 默认的首选翻译(不修改则为有道) 59 | * 默认的代理(不修改则为不使用代理) 60 | 61 | ```python 62 | from translation import (set_default_translation, set_default_language, 63 | set_default_proxies, get, ConnectError) 64 | 65 | set_default_translation('google') 66 | set_default_language('auto', 'zh-CN') 67 | set_default_proxies({'http': '127.0.0.1:1080'}) 68 | try: 69 | print(get('hello world!')) 70 | except ConnectError: 71 | print('Invaild proxy') 72 | ``` 73 | 74 | ### More 75 | 76 | 更多的功能可以参考[文档](http://translation.readthedocs.io/zh_CN/latest/)。 77 | 78 | ## Language 79 | 80 | [文档](http://translation.readthedocs.io/zh_CN/latest/)中有详细的支持语言的列表,这里仅给出谷歌支持语言的标记列表。 81 | 82 | 其中金山词霸,有道翻译仅支持文档给出的语言翻译为中文。 83 | 84 | **Google** 85 | ``` 86 | el : Greek, 87 | eo : Esperanto, 88 | en : English, 89 | af : Afrikaans, 90 | sw : Swahili, 91 | ca : Catalan, 92 | it : Italian, 93 | iw : Hebrew, 94 | sv : Swedish, 95 | cs : Czech, 96 | cy : Welsh, 97 | ar : Arabic, 98 | ur : Urdu, 99 | ga : Irish, 100 | eu : Basque, 101 | et : Estonian, 102 | az : Azerbaijani, 103 | id : Indonesian, 104 | es : Spanish, 105 | ru : Russian, 106 | gl : Galician, 107 | nl : Dutch, 108 | pt : Portuguese, 109 | la : Latin, 110 | tr : Turkish, 111 | tl : Filipino, 112 | lv : Latvian, 113 | lt : Lithuanian, 114 | th : Thai, 115 | vi : Vietnamese, 116 | gu : Gujarati, 117 | ro : Romanian, 118 | is : Icelandic, 119 | pl : Polish, 120 | ta : Tamil, 121 | yi : Yiddish, 122 | be : Belarusian, 123 | fr : French, 124 | bg : Bulgarian, 125 | uk : Ukrainian, 126 | hr : Croatian, 127 | bn : Bengali, 128 | sl : Slovenian, 129 | ht : Haitian Creole, 130 | da : Danish, 131 | fa : Persian, 132 | hi : Hindi, 133 | fi : Finnish, 134 | hu : Hungarian, 135 | ja : Japanese, 136 | ka : Georgian, 137 | te : Telugu, 138 | zh-TW : Chinese Traditional, 139 | sq : Albanian, 140 | no : Norwegian, 141 | ko : Korean, 142 | kn : Kannada, 143 | mk : Macedonian, 144 | zh-CN : Chinese Simplified, 145 | sk : Slovak, 146 | mt : Maltese, 147 | de : German, 148 | ms : Malay, 149 | sr : Serbian 150 | ``` 151 | 152 | ## Comments 153 | 154 | 如果有什么问题或者建议都可以在这个[Issue](https://github.com/littlecodersh/translation/issues/1)和我讨论。 155 | -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | # translation 2 | 3 | ![python](https://img.shields.io/badge/python-2.7-ff69b4.svg) ![python35](https://img.shields.io/badge/python-3.5-green.svg) [English version](https://github.com/littlecodersh/translation/blob/master/README.md) 4 | 5 | translation is a python translation package based on website service. 6 | 7 | It provids google, youdao, baidu, iciba translation service. 8 | 9 | ## Installation 10 | 11 | ```bash 12 | pip install translation 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```python 18 | from translation import baidu, google, youdao, iciba 19 | 20 | print(google('hello world!', dst = 'zh-CN')) 21 | print(google('hello world!', dst = 'ru')) 22 | print(baidu('hello world!', dst = 'zh')) 23 | print(baidu('hello world!', dst = 'ru')) 24 | print(youdao('hello world!', dst = 'zh-CN')) 25 | print(iciba('hello world!', dst = 'zh')) 26 | print(bing('hello world!', dst = 'zh-CHS')) 27 | ``` 28 | 29 | ## Documents 30 | 31 | You may find document [here](http://translation.readthedocs.io/zh_CN/latest/). 32 | 33 | ## Advanced usage 34 | 35 | ### Proxies 36 | 37 | You may not use some of the function without proxies. 38 | 39 | Proxies can be set as following. 40 | 41 | ```python 42 | from translation import google, ConnectError 43 | 44 | # 127.0.0.1:1080 is a vaild proxies 45 | try: 46 | print(google('hello world!', dst = 'zh-CN', proxies = {'http': '127.0.0.1:1080'})) 47 | except ConnectError: 48 | print('Invaild proxy') 49 | ``` 50 | 51 | ### Default 52 | 53 | You may change default setting such as: 54 | * default source language (auto if not set) 55 | * default destination language (zh-CN if not set) 56 | * default translation (youdao if not set) 57 | * default proxies (no proxy if not set) 58 | 59 | ```python 60 | from translation import (set_default_translation, set_default_language, 61 | set_default_proxies, get, ConnectError) 62 | 63 | set_default_translation('google') 64 | set_default_language('auto', 'zh-CN') 65 | set_default_proxies({'http': '127.0.0.1:1080'}) 66 | try: 67 | print(get('hello world!')) 68 | except ConnectError: 69 | print('Invaild proxy') 70 | ``` 71 | 72 | ### More 73 | 74 | More functions are introduced in the [document](http://translation.readthedocs.io/zh_CN/latest/). 75 | 76 | ## Language 77 | 78 | Language list of all the translation are provided in [document](http://translation.readthedocs.io/zh_CN/latest/). 79 | 80 | Somehow, iciba and youdao can only provide English translation. 81 | 82 | **Google** 83 | ``` 84 | el : Greek, 85 | eo : Esperanto, 86 | en : English, 87 | af : Afrikaans, 88 | sw : Swahili, 89 | ca : Catalan, 90 | it : Italian, 91 | iw : Hebrew, 92 | sv : Swedish, 93 | cs : Czech, 94 | cy : Welsh, 95 | ar : Arabic, 96 | ur : Urdu, 97 | ga : Irish, 98 | eu : Basque, 99 | et : Estonian, 100 | az : Azerbaijani, 101 | id : Indonesian, 102 | es : Spanish, 103 | ru : Russian, 104 | gl : Galician, 105 | nl : Dutch, 106 | pt : Portuguese, 107 | la : Latin, 108 | tr : Turkish, 109 | tl : Filipino, 110 | lv : Latvian, 111 | lt : Lithuanian, 112 | th : Thai, 113 | vi : Vietnamese, 114 | gu : Gujarati, 115 | ro : Romanian, 116 | is : Icelandic, 117 | pl : Polish, 118 | ta : Tamil, 119 | yi : Yiddish, 120 | be : Belarusian, 121 | fr : French, 122 | bg : Bulgarian, 123 | uk : Ukrainian, 124 | hr : Croatian, 125 | bn : Bengali, 126 | sl : Slovenian, 127 | ht : Haitian Creole, 128 | da : Danish, 129 | fa : Persian, 130 | hi : Hindi, 131 | fi : Finnish, 132 | hu : Hungarian, 133 | ja : Japanese, 134 | ka : Georgian, 135 | te : Telugu, 136 | zh-TW : Chinese Traditional, 137 | sq : Albanian, 138 | no : Norwegian, 139 | ko : Korean, 140 | kn : Kannada, 141 | mk : Macedonian, 142 | zh-CN : Chinese Simplified, 143 | sk : Slovak, 144 | mt : Maltese, 145 | de : German, 146 | ms : Malay, 147 | sr : Serbian 148 | ``` 149 | 150 | ## Comments 151 | 152 | If you have any problem or suggestion, you may contact me in this [issue](https://github.com/littlecodersh/translation/issues/1). 153 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | translation 2 | =========== 3 | 4 | |python27| |python35| `Chinese Version `__ 5 | 6 | translation is a python translation package based on website service. 7 | 8 | It provids google, youdao, baidu, iciba translation service. 9 | 10 | **Installation** 11 | 12 | .. code:: bash 13 | 14 | pip install translation 15 | 16 | **Usage** 17 | 18 | .. code:: python 19 | 20 | from translation import baidu, google, youdao, iciba 21 | 22 | print(google('hello world!', dst = 'zh-CN')) 23 | print(google('hello world!', dst = 'ru')) 24 | print(baidu('hello world!', dst = 'zh')) 25 | print(baidu('hello world!', dst = 'ru')) 26 | print(youdao('hello world!', dst = 'zh-CN')) 27 | print(iciba('hello world!', dst = 'zh')) 28 | print(bing('hello world!', dst = 'zh-CHS')) 29 | 30 | **Documents** 31 | 32 | You may find document `here `__ 33 | 34 | **Advanced usage** 35 | 36 | *Proxies* 37 | 38 | You may not use some of the function without proxies. 39 | 40 | Proxies can be set as following. 41 | 42 | .. code:: python 43 | 44 | from translation import google, ConnectError 45 | 46 | # 127.0.0.1:1080 is a vaild proxies 47 | try: 48 | print(google('hello world!', dst = 'zh-CN', proxies = {'http': '127.0.0.1:1080'})) 49 | except ConnectError: 50 | print('Invaild proxy') 51 | 52 | *Default* 53 | 54 | You may change default setting such as: 55 | 56 | * default source language (auto if not set) 57 | * default destination language (zh-CN if not set) 58 | * default translation (youdao if not set) 59 | * default proxies (no proxy if not set) 60 | 61 | .. code:: python 62 | 63 | from translation import (set_default_translation, set_default_language, 64 | set_default_proxies, get, ConnectError) 65 | 66 | set_default_translation('google') 67 | set_default_language('auto', 'zh-CN') 68 | set_default_proxies({'http': '127.0.0.1:1080'}) 69 | try: 70 | print(get('hello world!')) 71 | except ConnectError: 72 | print('Invaild proxy') 73 | 74 | **More** 75 | 76 | More functions are introduced in the `document `__. 77 | 78 | **Language** 79 | 80 | Language list of all the translation are provided in `document `__. 81 | 82 | Somehow, iciba and youdao can only provide English translation. 83 | 84 | *Google* 85 | 86 | .. code:: bash 87 | 88 | el : Greek, 89 | eo : Esperanto, 90 | en : English, 91 | af : Afrikaans, 92 | sw : Swahili, 93 | ca : Catalan, 94 | it : Italian, 95 | iw : Hebrew, 96 | sv : Swedish, 97 | cs : Czech, 98 | cy : Welsh, 99 | ar : Arabic, 100 | ur : Urdu, 101 | ga : Irish, 102 | eu : Basque, 103 | et : Estonian, 104 | az : Azerbaijani, 105 | id : Indonesian, 106 | es : Spanish, 107 | ru : Russian, 108 | gl : Galician, 109 | nl : Dutch, 110 | pt : Portuguese, 111 | la : Latin, 112 | tr : Turkish, 113 | tl : Filipino, 114 | lv : Latvian, 115 | lt : Lithuanian, 116 | th : Thai, 117 | vi : Vietnamese, 118 | gu : Gujarati, 119 | ro : Romanian, 120 | is : Icelandic, 121 | pl : Polish, 122 | ta : Tamil, 123 | yi : Yiddish, 124 | be : Belarusian, 125 | fr : French, 126 | bg : Bulgarian, 127 | uk : Ukrainian, 128 | hr : Croatian, 129 | bn : Bengali, 130 | sl : Slovenian, 131 | ht : Haitian Creole, 132 | da : Danish, 133 | fa : Persian, 134 | hi : Hindi, 135 | fi : Finnish, 136 | hu : Hungarian, 137 | ja : Japanese, 138 | ka : Georgian, 139 | te : Telugu, 140 | zh-TW : Chinese Traditional, 141 | sq : Albanian, 142 | no : Norwegian, 143 | ko : Korean, 144 | kn : Kannada, 145 | mk : Macedonian, 146 | zh-CN : Chinese Simplified, 147 | sk : Slovak, 148 | mt : Maltese, 149 | de : German, 150 | ms : Malay, 151 | sr : Serbian 152 | 153 | **Comments** 154 | 155 | If you have any problem or suggestion, you may contact me in this `issue `__. 156 | 157 | .. |python27| image:: https://img.shields.io/badge/python-2.7-ff69b4.svg 158 | .. |python35| image:: https://img.shields.io/badge/python-3.5-green.svg 159 | --------------------------------------------------------------------------------