[mrdoctorwho]. Fix google_translation.
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
[info]
|
[info]
|
||||||
name: Google Translation
|
name: Google Translation
|
||||||
short_name: google_translation
|
short_name: google_translation
|
||||||
#version: 0.2
|
version: 0.3
|
||||||
#google closed API and plugin do not work
|
|
||||||
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>
|
||||||
|
mrDoctorWho <mrdoctorwho@gmail.com>
|
||||||
homepage = http://blog.bilinski.it
|
homepage = http://blog.bilinski.it
|
||||||
|
|
||||||
|
|||||||
@@ -23,10 +23,12 @@ Translates (currently only incoming) messages using Google Translate.
|
|||||||
:author: Mateusz Biliński <mateusz@bilinski.it>
|
:author: Mateusz Biliński <mateusz@bilinski.it>
|
||||||
:since: 25th August 2008
|
:since: 25th August 2008
|
||||||
:copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
|
:copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
|
||||||
|
: (2012) mrDoctorWho <mrdoctorwho@gmail.com>
|
||||||
:license: GPL
|
:license: GPL
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import urllib
|
||||||
import urllib2
|
import urllib2
|
||||||
import HTMLParser
|
import HTMLParser
|
||||||
import gtk
|
import gtk
|
||||||
@@ -127,32 +129,19 @@ class GoogleTranslationPlugin(GajimPlugin):
|
|||||||
|
|
||||||
@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):
|
||||||
# Converts text so it can be used within URL as query to Google
|
data = {"client": "x",
|
||||||
# Translate.
|
"tl": to_lang,
|
||||||
quoted_text = urllib2.quote(text.encode(getfilesystemencoding()))
|
"sl": from_lang,
|
||||||
# prepare url
|
"text": text.encode("utf-8")}
|
||||||
translation_url = u'https://ajax.googleapis.com/ajax/services/' \
|
url = "http://translate.google.ru/translate_a/t"
|
||||||
'language/translate?q=%(quoted_text)s&' \
|
url = u"%s?%s" % (url, urllib.urlencode(data))
|
||||||
'langpair=%(from_lang)s%%7C%(to_lang)s&key=notsupplied&v=1.0' % \
|
request = urllib2.Request(url)
|
||||||
locals()
|
request.add_header("User-Agent",
|
||||||
|
"Mozilla/5.0 (X11; Linux i686; rv:16.0) Gecko/20120815 Firefox/16.0")
|
||||||
results = helpers.download_image(account, {'src': translation_url})[0]
|
response = urllib2.urlopen(request)
|
||||||
if not results:
|
if response:
|
||||||
return text
|
data = json.load(response)
|
||||||
|
return data["sentences"][0]["trans"]
|
||||||
result = json.loads(results)
|
|
||||||
|
|
||||||
if result.get('responseStatus', '') != 200:
|
|
||||||
return text
|
|
||||||
|
|
||||||
translated_text = result['responseData'].get('translatedText', '')
|
|
||||||
if translated_text:
|
|
||||||
try:
|
|
||||||
htmlparser = HTMLParser.HTMLParser()
|
|
||||||
translated_text = htmlparser.unescape(translated_text)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
return translated_text
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
@log_calls('GoogleTranslationPlugin')
|
@log_calls('GoogleTranslationPlugin')
|
||||||
|
|||||||
Reference in New Issue
Block a user