[Dicson] Handle non-Latin text and errors in google translate plugin. Fixes #5904
This commit is contained in:
parent
89d8147cc5
commit
896fe73945
|
@ -30,6 +30,7 @@ import re
|
||||||
import urllib2
|
import urllib2
|
||||||
import new
|
import new
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
|
from sys import getfilesystemencoding
|
||||||
|
|
||||||
from common import helpers
|
from common import helpers
|
||||||
from common import gajim
|
from common import gajim
|
||||||
|
@ -51,45 +52,49 @@ class GoogleTranslationPlugin(GajimPlugin):
|
||||||
def init(self):
|
def init(self):
|
||||||
self.config_dialog = None
|
self.config_dialog = None
|
||||||
#self.gui_extension_points = {}
|
#self.gui_extension_points = {}
|
||||||
self.config_default_values = {'from_lang' : (u'en', _(u'Language of text to be translated')),
|
self.config_default_values = {
|
||||||
'to_lang' : (u'fr', _(u'Language to which translation will be made')),
|
'from_lang' :
|
||||||
'user_agent' : (u'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.12) Gecko/20080213 Firefox/2.0.0.11',
|
(u'en', _(u'Language of text to be translated')),
|
||||||
_(u'User Agent data to be used with urllib2 when connecting to Google Translate service'))}
|
'to_lang' :
|
||||||
|
(u'fr', _(u'Language to which translation will be made')),
|
||||||
|
'user_agent' :
|
||||||
|
(u'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.12) '
|
||||||
|
'Gecko/20080213 Firefox/2.0.0.11',
|
||||||
|
_(u'User Agent data to be used with urllib2 '
|
||||||
|
'when connecting to Google Translate service'))}
|
||||||
|
|
||||||
#self.events_handlers = {}
|
#self.events_handlers = {}
|
||||||
|
|
||||||
self.events = [GoogleTranslateMessageReceivedEvent]
|
self.events = [GoogleTranslateMessageReceivedEvent]
|
||||||
|
|
||||||
self.translated_text_re = \
|
self.translated_text_re = re.compile(
|
||||||
re.compile(r'google.language.callbacks.id100\(\'22\',{"translatedText":"(?P<text>[^"]*)"}, 200, null, 200\)')
|
r'google.language.callbacks.id100\(\'22\','
|
||||||
|
'{"translatedText":"(?P<text>[^"]*)"}, 200, null, 200\)')
|
||||||
|
|
||||||
@log_calls('GoogleTranslationPlugin')
|
@log_calls('GoogleTranslationPlugin')
|
||||||
def translate_text(self, text, from_lang, to_lang):
|
def translate_text(self, text, from_lang, to_lang):
|
||||||
text = self.prepare_text_for_url(text)
|
# Converts text so it can be used within URL as query to Google
|
||||||
|
# Translate.
|
||||||
|
quoted_text = urllib2.quote(text.encode(getfilesystemencoding()))
|
||||||
|
# prepare url
|
||||||
headers = { 'User-Agent' : self.config['user_agent'] }
|
headers = { 'User-Agent' : self.config['user_agent'] }
|
||||||
translation_url = u'http://www.google.com/uds/Gtranslate?callback=google.language.callbacks.id100&context=22&q=%(text)s&langpair=%(from_lang)s%%7C%(to_lang)s&key=notsupplied&v=1.0'%locals()
|
translation_url = u'http://www.google.com/uds/Gtranslate?callback='\
|
||||||
|
'google.language.callbacks.id100&context=22&q=%(quoted_text)s&'\
|
||||||
|
'langpair=%(from_lang)s%%7C%(to_lang)s&key=notsupplied&v=1.0' % \
|
||||||
|
locals()
|
||||||
request = urllib2.Request(translation_url, headers=headers)
|
request = urllib2.Request(translation_url, headers=headers)
|
||||||
response = urllib2.urlopen(request)
|
|
||||||
results = response.read()
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = urllib2.urlopen(request)
|
||||||
|
except urllib2.URLError, e:
|
||||||
|
# print e
|
||||||
|
return text
|
||||||
|
|
||||||
|
results = response.read()
|
||||||
translated_text = self.translated_text_re.search(results).group('text')
|
translated_text = self.translated_text_re.search(results).group('text')
|
||||||
|
|
||||||
|
if translated_text:
|
||||||
return translated_text
|
return translated_text
|
||||||
|
|
||||||
@log_calls('GoogleTranslationPlugin')
|
|
||||||
def prepare_text_for_url(self, text):
|
|
||||||
'''
|
|
||||||
Converts text so it can be used within URL as query to Google Translate.
|
|
||||||
'''
|
|
||||||
|
|
||||||
# There should be more replacements for plugin to work in any case:
|
|
||||||
char_replacements = { ' ' : '%20',
|
|
||||||
'+' : '%2B'}
|
|
||||||
|
|
||||||
for char, replacement in char_replacements.iteritems():
|
|
||||||
text = text.replace(char, replacement)
|
|
||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
@log_calls('GoogleTranslationPlugin')
|
@log_calls('GoogleTranslationPlugin')
|
||||||
|
@ -114,5 +119,6 @@ class GoogleTranslateMessageReceivedEvent(nec.NetworkIncomingEvent):
|
||||||
self.base_event.xmpp_msg.kids[0].setData(
|
self.base_event.xmpp_msg.kids[0].setData(
|
||||||
self.plugin.translate_text(msg_text, from_lang, to_lang))
|
self.plugin.translate_text(msg_text, from_lang, to_lang))
|
||||||
|
|
||||||
return False # We only want to modify old event, not emit another,
|
# We only want to modify old event, not emit another, so we return False
|
||||||
# so we return False here.
|
# here.
|
||||||
|
return False
|
||||||
|
|
Loading…
Reference in New Issue