mirror of
https://github.com/MCV-Software/TWBlue.git
synced 2024-11-22 19:28:09 -06:00
Fixed translator
This commit is contained in:
parent
5d87ce06b7
commit
7c65a96a2b
@ -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.
|
||||
|
@ -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"))
|
||||
|
@ -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"),
|
||||
|
Loading…
Reference in New Issue
Block a user