changeset b4316d026a26 in /home/hg/repos/gajim
details:http://hg.gajim.org/gajim?cmd=changeset;node=b4316d026a26 description: make google translation plugin work with unicode char
diffstat:
plugins/google_translation/manifest.ini | 2 +- plugins/google_translation/plugin.py | 23 +++++++---------------- 2 files changed, 8 insertions(+), 17 deletions(-)
diffs (80 lines):
diff -r 4a42553b0be4 -r b4316d026a26 plugins/google_translation/manifest.ini --- a/plugins/google_translation/manifest.ini Wed Nov 16 17:52:19 2011 +0100 +++ b/plugins/google_translation/manifest.ini Wed Nov 16 20:11:25 2011 +0100 @@ -1,7 +1,7 @@ [info] name: Google Translation short_name: google_translation -version: 0.1 +version: 0.2 description: Translates (currently only incoming) messages using Google Translate. authors = Mateusz BiliĆski mateusz@bilinski.it homepage = http://blog.bilinski.it diff -r 4a42553b0be4 -r b4316d026a26 plugins/google_translation/plugin.py --- a/plugins/google_translation/plugin.py Wed Nov 16 17:52:19 2011 +0100 +++ b/plugins/google_translation/plugin.py Wed Nov 16 20:11:25 2011 +0100 @@ -26,12 +26,10 @@ :license: GPL '''
-import re +import json import urllib2 import HTMLParser -import new import gtk -from pprint import pformat from sys import getfilesystemencoding
import chat_control @@ -43,7 +41,6 @@ from plugins import GajimPlugin from plugins.helpers import log_calls, log from common import ged -from common import nec
languages = { _('Afrikaans'): 'af', @@ -127,9 +124,6 @@ }
self.controls = [] - self.translated_text_re = re.compile( - r'google.language.callbacks.id100('22', ' - '{(?P<text>[^}]*)}, 200, null, 200)')
@log_calls('GoogleTranslationPlugin') def translate_text(self, account, text, from_lang, to_lang): @@ -137,8 +131,8 @@ # Translate. quoted_text = urllib2.quote(text.encode(getfilesystemencoding())) # prepare url - translation_url = u'http://www.google.com/uds/Gtranslate?callback=%27%5C - 'google.language.callbacks.id100&context=22&q=%(quoted_text)s&'\ + translation_url = u'https://ajax.googleapis.com/ajax/services/' \ + 'language/translate?q=%(quoted_text)s&' \ 'langpair=%(from_lang)s%%7C%(to_lang)s&key=notsupplied&v=1.0' % \ locals()
@@ -146,17 +140,14 @@ if not results: return text
- result = self.translated_text_re.search(results) - if not result: + result = json.loads(results) + + if result.get('responseStatus', '') != 200: return text
- dict_ = eval('{' + result.group('text') + '}') - - translated_text = dict_.get('translatedText', '') - + translated_text = result['responseData'].get('translatedText', '') if translated_text: try: - translated_text = unicode(translated_text, 'unicode_escape') htmlparser = HTMLParser.HTMLParser() translated_text = htmlparser.unescape(translated_text) except Exception: