1 # -*- coding: utf-8 -*-
3 ## This file is part of Gajim.
5 ## Gajim is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
9 ## Gajim is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
14 ## You should have received a copy of the GNU General Public License
15 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
18 Google Translation plugin.
20 Translates (currently only incoming) messages using Google Translate.
22 :note: consider this as proof-of-concept
23 :author: Mateusz Biliński <mateusz@bilinski.it>
24 :since: 25th August 2008
25 :copyright: Copyright (2008) Mateusz Biliński <mateusz@bilinski.it>
32 from pprint
import pformat
33 from sys
import getfilesystemencoding
35 from common
import helpers
36 from common
import gajim
38 from plugins
import GajimPlugin
39 from plugins
.helpers
import log_calls
, log
40 from common
import ged
41 from common
import nec
43 class GoogleTranslationPlugin(GajimPlugin
):
45 @log_calls('GoogleTranslationPlugin')
47 self
.config_dialog
= None
48 #self.gui_extension_points = {}
49 self
.config_default_values
= {
51 (u
'en', _(u
'Language of text to be translated')),
53 (u
'fr', _(u
'Language to which translation will be made')),
55 (u
'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.12) '
56 'Gecko/20080213 Firefox/2.0.0.11',
57 _(u
'User Agent data to be used with urllib2 '
58 'when connecting to Google Translate service'))}
60 #self.events_handlers = {}
62 self
.events
= [GoogleTranslateMessageReceivedEvent
]
64 self
.translated_text_re
= re
.compile(
65 r
'google.language.callbacks.id100\(\'22\','
66 '{"translatedText":"(?P<text>[^"]*)"}, 200, null, 200\)')
68 @log_calls('GoogleTranslationPlugin')
69 def translate_text(self, text, from_lang, to_lang):
70 # Converts text so it can be used within URL as query to Google
72 quoted_text = urllib2.quote(text.encode(getfilesystemencoding()))
74 headers = { 'User-Agent' : self.config['user_agent'] }
75 translation_url = u'http://www.google.com/uds/Gtranslate?callback='\
76 'google.language.callbacks.id100&context=22&q=%(quoted_text)s&'\
77 'langpair=%(from_lang)s%%7C%(to_lang)s&key=notsupplied&v=1.0' % \
79 request = urllib2.Request(translation_url, headers=headers)
82 response = urllib2.urlopen(request)
83 except urllib2.URLError, e:
87 results = response.read()
88 translated_text = self.translated_text_re.search(results).group('text')
91 return translated_text
94 @log_calls('GoogleTranslationPlugin')
98 @log_calls('GoogleTranslationPlugin')
102 class GoogleTranslateMessageReceivedEvent(nec.NetworkIncomingEvent):
103 name = 'google-translate-message-received'
104 base_network_events = ['raw-message-received']
107 msg_type = self.base_event.stanza.attrs.get('type', None)
108 if msg_type == u'chat':
109 msg_text = "".join(self.base_event.stanza.kids[0].data)
111 from_lang = self.plugin.config['from_lang']
112 to_lang = self.plugin.config['to_lang']
113 self.base_event.stanza.kids[0].setData(
114 self.plugin.translate_text(msg_text, from_lang, to_lang))
116 # We only want to modify old event, not emit another, so we return False