Skip to content
Snippets Groups Projects
Commit 1bde3e44 authored by Yann Leboulanger's avatar Yann Leboulanger
Browse files

make google translation plugin work with unicode char

parent 6cc7ca01
No related branches found
No related tags found
No related merge requests found
[info] [info]
name: Google Translation name: Google Translation
short_name: google_translation short_name: google_translation
version: 0.1 version: 0.2
description: Translates (currently only incoming) messages using Google Translate. description: Translates (currently only incoming) messages using Google Translate.
authors = Mateusz Biliński <mateusz@bilinski.it> authors = Mateusz Biliński <mateusz@bilinski.it>
homepage = http://blog.bilinski.it homepage = http://blog.bilinski.it
......
...@@ -26,12 +26,10 @@ Translates (currently only incoming) messages using Google Translate. ...@@ -26,12 +26,10 @@ Translates (currently only incoming) messages using Google Translate.
:license: GPL :license: GPL
''' '''
import re import json
import urllib2 import urllib2
import HTMLParser import HTMLParser
import new
import gtk import gtk
from pprint import pformat
from sys import getfilesystemencoding from sys import getfilesystemencoding
import chat_control import chat_control
...@@ -43,7 +41,6 @@ from common import gajim ...@@ -43,7 +41,6 @@ from common import gajim
from plugins import GajimPlugin from plugins import GajimPlugin
from plugins.helpers import log_calls, log from plugins.helpers import log_calls, log
from common import ged from common import ged
from common import nec
languages = { languages = {
_('Afrikaans'): 'af', _('Afrikaans'): 'af',
...@@ -127,9 +124,6 @@ class GoogleTranslationPlugin(GajimPlugin): ...@@ -127,9 +124,6 @@ class GoogleTranslationPlugin(GajimPlugin):
} }
self.controls = [] self.controls = []
self.translated_text_re = re.compile(
r'google.language.callbacks.id100\(\'22\', '
'{(?P<text>[^\}]*)}, 200, null, 200\)')
@log_calls('GoogleTranslationPlugin') @log_calls('GoogleTranslationPlugin')
def translate_text(self, account, text, from_lang, to_lang): def translate_text(self, account, text, from_lang, to_lang):
...@@ -137,8 +131,8 @@ class GoogleTranslationPlugin(GajimPlugin): ...@@ -137,8 +131,8 @@ class GoogleTranslationPlugin(GajimPlugin):
# Translate. # Translate.
quoted_text = urllib2.quote(text.encode(getfilesystemencoding())) quoted_text = urllib2.quote(text.encode(getfilesystemencoding()))
# prepare url # prepare url
translation_url = u'http://www.google.com/uds/Gtranslate?callback='\ translation_url = u'https://ajax.googleapis.com/ajax/services/' \
'google.language.callbacks.id100&context=22&q=%(quoted_text)s&'\ 'language/translate?q=%(quoted_text)s&' \
'langpair=%(from_lang)s%%7C%(to_lang)s&key=notsupplied&v=1.0' % \ 'langpair=%(from_lang)s%%7C%(to_lang)s&key=notsupplied&v=1.0' % \
locals() locals()
...@@ -146,17 +140,14 @@ class GoogleTranslationPlugin(GajimPlugin): ...@@ -146,17 +140,14 @@ class GoogleTranslationPlugin(GajimPlugin):
if not results: if not results:
return text return text
result = self.translated_text_re.search(results) result = json.loads(results)
if not result:
return text
dict_ = eval('{' + result.group('text') + '}')
translated_text = dict_.get('translatedText', '') if result.get('responseStatus', '') != 200:
return text
translated_text = result['responseData'].get('translatedText', '')
if translated_text: if translated_text:
try: try:
translated_text = unicode(translated_text, 'unicode_escape')
htmlparser = HTMLParser.HTMLParser() htmlparser = HTMLParser.HTMLParser()
translated_text = htmlparser.unescape(translated_text) translated_text = htmlparser.unescape(translated_text)
except Exception: except Exception:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment