From 7c65a96a2b3b6da2ecd3666075fc5ebe40bfc366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Cort=C3=A9z?= Date: Fri, 10 Apr 2015 09:36:25 -0500 Subject: [PATCH] Fixed translator --- README.md | 1 + src/controller/messages.py | 5 +-- src/extra/translator/translator.py | 54 +++--------------------------- 3 files changed, 7 insertions(+), 53 deletions(-) diff --git a/README.md b/README.md index 0da3f5a0..cc8bcd77 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ setuptools install a script, called easy_install. You can find it in the python * pygeocoder * suds * arrow +* goslate * markdown easy_install will automatically get the additional libraries that these packages need to work properly. diff --git a/src/controller/messages.py b/src/controller/messages.py index 94993875..a9631070 100644 --- a/src/controller/messages.py +++ b/src/controller/messages.py @@ -37,10 +37,7 @@ class basicTweet(object): text_to_translate = self.message.get_text().encode("utf-8") 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")] - t = translator.translator.Translator() - t.from_lang = source - t.to_lang = dest - msg = t.translate(text_to_translate) + msg = translator.translator.translate(text_to_translate, source, dest) self.message.set_text(msg) self.message.text_focus() output.speak(_(u"Translated")) diff --git a/src/extra/translator/translator.py b/src/extra/translator/translator.py index 8e8186d4..3c66214b 100644 --- a/src/extra/translator/translator.py +++ b/src/extra/translator/translator.py @@ -1,53 +1,9 @@ -# -*- coding: utf-8 -*- -import re -try: - import urllib2 as request - from urllib import quote -except: - from urllib import request - from urllib.parse import quote +#!/usr/bin/env python +import goslate -class Translator: - string_pattern = r"\"(([^\"\\]|\\.)*)\"" - match_string =re.compile( - 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) +def translate(text, source_lang, target_lang): + gs = goslate.Goslate() + return gs.translate(text, target_lang, source_lang) languages = { "af": _(u"Afrikaans"),