Make translate.py more customizable, and hide it for now

This commit is contained in:
mathieui 2012-05-09 19:19:17 +02:00
parent 5c327ef232
commit e1a817607b

View file

@ -7,7 +7,7 @@ import json
TARGET_LANG = 'en'
def translate(s, target=TARGET_LANG, source=''):
f = urllib.request.urlopen('http://ajax.googleapis.com/ajax/services/language/translate', urlencode({ 'v': '1.0', 'q': s, 'langpair': '%s|%s' % (source, target) }))
f = urllib.request.urlopen('http://ajax.googleapis.com/ajax/services/language/translate', urlencode({ 'v': '1.0', 'q': s, 'langpair': '%s|%s' % (source, target) }).encode('utf-8'))
response = json.loads(str(f.read(), 'utf-8'))['responseData']
return (response['translatedText'], response['detectedSourceLanguage'])
@ -21,11 +21,12 @@ class Plugin(BasePlugin):
if message['type'] == 'error':
return
if room_from == 'poezio@muc.poezio.eu':
if room_from in self.config.options():
target_lang = self.config.get(room_from, self.config.get('default', TARGET_LANG))
nick_from = message['mucnick']
body = xhtml.get_body_from_message_stanza(message)
room = self.core.get_room_by_name(room_from)
text, lang = translate(body)
room = self.core.get_tab_by_name(room_from)
text, lang = translate(body, target=target_lang)
if lang != TARGET_LANG:
room.add_message(text, nickname=nick_from)
self.core.refresh_window()