Fixed translator

This commit is contained in:
Manuel Cortez 2015-04-10 09:36:25 -05:00
parent 5d87ce06b7
commit 7c65a96a2b
3 changed files with 7 additions and 53 deletions

View File

@ -72,6 +72,7 @@ setuptools install a script, called easy_install. You can find it in the python
* pygeocoder * pygeocoder
* suds * suds
* arrow * arrow
* goslate
* markdown * markdown
easy_install will automatically get the additional libraries that these packages need to work properly. easy_install will automatically get the additional libraries that these packages need to work properly.

View File

@ -37,10 +37,7 @@ class basicTweet(object):
text_to_translate = self.message.get_text().encode("utf-8") text_to_translate = self.message.get_text().encode("utf-8")
source = [x[0] for x in translator.translator.available_languages()][dlg.get("source_lang")] source = [x[0] for x in translator.translator.available_languages()][dlg.get("source_lang")]
dest = [x[0] for x in translator.translator.available_languages()][dlg.get("dest_lang")] dest = [x[0] for x in translator.translator.available_languages()][dlg.get("dest_lang")]
t = translator.translator.Translator() msg = translator.translator.translate(text_to_translate, source, dest)
t.from_lang = source
t.to_lang = dest
msg = t.translate(text_to_translate)
self.message.set_text(msg) self.message.set_text(msg)
self.message.text_focus() self.message.text_focus()
output.speak(_(u"Translated")) output.speak(_(u"Translated"))

View File

@ -1,53 +1,9 @@
# -*- coding: utf-8 -*- #!/usr/bin/env python
import re import goslate
try:
import urllib2 as request
from urllib import quote
except:
from urllib import request
from urllib.parse import quote
class Translator: def translate(text, source_lang, target_lang):
string_pattern = r"\"(([^\"\\]|\\.)*)\"" gs = goslate.Goslate()
match_string =re.compile( return gs.translate(text, target_lang, source_lang)
r"\,?\["
+ string_pattern + r"\,"
+ string_pattern + r"\,"
+ string_pattern + r"\,"
+ string_pattern
+r"\]")
def __init__(self):
self.from_lang = ""
self.to_lang = ""
def translate(self, source):
json5 = self._get_json5_from_google(source)
return self._unescape(self._get_translation_from_json5(json5))
def _get_translation_from_json5(self, content):
result = ""
pos = 2
while True:
m = self.match_string.match(content, pos)
if not m:
break
result += m.group(1)
pos = m.end()
return result
def _get_json5_from_google(self, source):
escaped_source = quote(source, '')
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.168 Safari/535.19'}
req = request.Request(
url="http://translate.google.com/translate_a/t?client=t&ie=UTF-8&oe=UTF-8"
+"&sl=%s&tl=%s&text=%s" % (self.from_lang, self.to_lang, escaped_source)
, headers = headers)
r = request.urlopen(req)
return r.read().decode('utf-8')
def _unescape(self, text):
return re.sub(r"\\.?", lambda x:eval('"%s"'%x.group(0)), text)
languages = { languages = {
"af": _(u"Afrikaans"), "af": _(u"Afrikaans"),