Skip to content

Commit

Permalink
update py-googletrans and other minor modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
wizyoung committed Sep 10, 2017
1 parent c2ec3b1 commit 56144d5
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
- 界面: 弹窗显示界面使用了[cocoaDialog](http://mstratman.github.io/cocoadialog/)
- 图标: 感谢[linivor](https://github.com/linivor)[lfcxlfcx](https://github.com/lfcxlfcx)同学对改插件图标提出中肯意见并帮忙修改,同时感谢[linivor](https://github.com/linivor)同学提供的软件测试服务。

### 3. 附件说明
### 3. 附加说明

本人同时制作了官方API版本的谷歌翻译popclip插件,需要的可Email联系本人。

Expand Down
4 changes: 2 additions & 2 deletions src/googletrans/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Free Google Translate API for Python. Translates totally free of charge."""
__all__ = 'Translator',
__version__ = '2.1.3'
__version__ = '2.1.4'


from googletrans.client import Translator
from googletrans.constants import LANGUAGES
from googletrans.constants import LANGCODES, LANGUAGES
Binary file modified src/googletrans/__init__.pyc
Binary file not shown.
45 changes: 27 additions & 18 deletions src/googletrans/client.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from googletrans import urls, utils
from googletrans.compat import PY3
from googletrans.gtoken import TokenAcquirer
from googletrans.constants import DEFAULT_USER_AGENT, LANGUAGES, SPECIAL_CASES
from googletrans.constants import DEFAULT_USER_AGENT, LANGCODES, LANGUAGES, SPECIAL_CASES
from googletrans.models import Translated, Detected


Expand Down Expand Up @@ -50,19 +50,7 @@ def _pick_service_url(self):
return self.service_urls[0]
return random.choice(self.service_urls)

def _translate(self, text, dest='en', src='auto'):
if src != 'auto':
if src not in LANGUAGES.keys() and src in SPECIAL_CASES.keys():
src = SPECIAL_CASES[src]
elif src not in LANGUAGES.keys():
raise ValueError('invalid source language')

if dest not in LANGUAGES.keys():
if dest in SPECIAL_CASES.keys():
dest = SPECIAL_CASES[dest]
else:
raise ValueError('invalid destination language')

def _translate(self, text, dest, src):
if not PY3 and isinstance(text, str): # pragma: nocover
text = text.decode('utf-8')

Expand All @@ -82,11 +70,13 @@ def translate(self, text, dest='en', src='auto'):
:type text: UTF-8 :class:`str`; :class:`unicode`; string sequence (list, tuple, iterator, generator)
:param dest: The language to translate the source text into.
The value should be one of the language codes listed in :const:`googletrans.LANGUAGES`.
The value should be one of the language codes listed in :const:`googletrans.LANGUAGES`
or one of the language names listed in :const:`googletrans.LANGCODES`.
:param dest: :class:`str`; :class:`unicode`
:param src: The language of the source text.
The value should be one of the language codes listed in :const:`googletrans.LANGUAGES`.
The value should be one of the language codes listed in :const:`googletrans.LANGUAGES`
or one of the language names listed in :const:`googletrans.LANGCODES`.
If a language is not specified,
the system will attempt to identify the source language automatically.
:param src: :class:`str`; :class:`unicode`
Expand All @@ -112,6 +102,25 @@ def translate(self, text, dest='en', src='auto'):
jumps over -> 이상 점프
the lazy dog -> 게으른 개
"""
dest = dest.lower().split('_', 1)[0]
src = src.lower().split('_', 1)[0]

if src != 'auto' and src not in LANGUAGES:
if src in SPECIAL_CASES:
src = SPECIAL_CASES[src]
elif src in LANGCODES:
src = LANGCODES[src]
else:
raise ValueError('invalid source language')

if dest not in LANGUAGES:
if dest in SPECIAL_CASES:
dest = SPECIAL_CASES[dest]
elif dest in LANGCODES:
dest = LANGCODES[dest]
else:
raise ValueError('invalid destination language')

if isinstance(text, list):
result = []
for item in text:
Expand All @@ -128,13 +137,13 @@ def translate(self, text, dest='en', src='auto'):
# actual source language that will be recognized by Google Translator when the
# src passed is equal to auto.
try:
src = data[-1][0][0]
src = data[2]
except Exception: # pragma: nocover
pass

pron = origin
try:
pron = data[0][1][-1]
pron = data[0][1][-2]
except Exception: # pragma: nocover
pass
if not PY3 and isinstance(pron, unicode) and isinstance(origin, str): # pragma: nocover
Expand Down
Binary file modified src/googletrans/client.pyc
Binary file not shown.
Empty file modified src/googletrans/compat.py
100644 → 100755
Empty file.
Binary file modified src/googletrans/compat.pyc
Binary file not shown.
58 changes: 55 additions & 3 deletions src/googletrans/constants.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@
LANGUAGES = {
'af': 'afrikaans',
'sq': 'albanian',
'am': 'amharic',
'ar': 'arabic',
'hy': 'armenian',
'az': 'azerbaijani',
'eu': 'basque',
'be': 'belarusian',
'bn': 'bengali',
'bs': 'bosnian',
'bg': 'bulgarian',
'ca': 'catalan',
'zh-CN': 'chinese_simplified',
'zh-TW': 'chinese_traditional',
'ceb': 'cebuano',
'ny': 'chichewa',
'zh-cn': 'chinese (simplified)',
'zh-tw': 'chinese (traditional)',
'co': 'corsican',
'hr': 'croatian',
'cs': 'czech',
'da': 'danish',
Expand All @@ -23,40 +32,83 @@
'tl': 'filipino',
'fi': 'finnish',
'fr': 'french',
'fy': 'frisian',
'gl': 'galician',
'ka': 'georgian',
'de': 'german',
'el': 'greek',
'gu': 'gujarati',
'ht': 'haitian creole',
'ha': 'hausa',
'haw': 'hawaiian',
'iw': 'hebrew',
'hi': 'hindi',
'hmn': 'hmong',
'hu': 'hungarian',
'is': 'icelandic',
'ig': 'igbo',
'id': 'indonesian',
'ga': 'irish',
'it': 'italian',
'ja': 'japanese',
'jw': 'javanese',
'kn': 'kannada',
'kk': 'kazakh',
'km': 'khmer',
'ko': 'korean',
'ku': 'kurdish (kurmanji)',
'ky': 'kyrgyz',
'lo': 'lao',
'la': 'latin',
'lv': 'latvian',
'lt': 'lithuanian',
'lb': 'luxembourgish',
'mk': 'macedonian',
'mg': 'malagasy',
'ms': 'malay',
'ml': 'malayalam',
'mt': 'maltese',
'mi': 'maori',
'mr': 'marathi',
'mn': 'mongolian',
'my': 'myanmar (burmese)',
'ne': 'nepali',
'no': 'norwegian',
'ps': 'pashto',
'fa': 'persian',
'pl': 'polish',
'pt': 'portuguese',
'pa': 'punjabi',
'ro': 'romanian',
'ru': 'russian',
'sm': 'samoan',
'gd': 'scots gaelic',
'sr': 'serbian',
'st': 'sesotho',
'sn': 'shona',
'sd': 'sindhi',
'si': 'sinhala',
'sk': 'slovak',
'sl': 'slovenian',
'so': 'somali',
'es': 'spanish',
'su': 'sundanese',
'sw': 'swahili',
'sv': 'swedish',
'tg': 'tajik',
'ta': 'tamil',
'te': 'telugu',
'th': 'thai',
'tr': 'turkish',
'uk': 'ukrainian',
'ur': 'urdu',
'uz': 'uzbek',
'vi': 'vietnamese',
'cy': 'welsh',
'xh': 'xhosa',
'yi': 'yiddish',
}
'yo': 'yoruba',
'zu': 'zulu'
}

LANGCODES = dict(map(reversed, LANGUAGES.items()))
Binary file modified src/googletrans/constants.pyc
Binary file not shown.
Empty file modified src/googletrans/gtoken.py
100644 → 100755
Empty file.
Binary file modified src/googletrans/gtoken.pyc
Binary file not shown.
Empty file modified src/googletrans/models.py
100644 → 100755
Empty file.
Binary file modified src/googletrans/models.pyc
Binary file not shown.
Empty file modified src/googletrans/urls.py
100644 → 100755
Empty file.
Binary file modified src/googletrans/urls.pyc
Binary file not shown.
Empty file modified src/googletrans/utils.py
100644 → 100755
Empty file.
Binary file modified src/googletrans/utils.pyc
Binary file not shown.
5 changes: 0 additions & 5 deletions src/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
toclipboard = args.toclipboard
location = args.location

# 这里几个坑
# 接受选中的文字最好用$POPCLIP_URLENCODED_TEXT,编码为urlcode形式
# 因为$POPCLIP_TEXT不能接受空格,不然报错,坑
# 然后用urllib的unquote反向解码一下
# 最后decode('utf-8'),不然后面的函数无法判断是否为中文
query = unquote(args.query)
query = query.decode('utf-8')

Expand Down

0 comments on commit 56144d5

Please sign in to comment.