ability to enable and choose languages for google translation plugin per jid.
This commit is contained in:
parent
04d1b80691
commit
715ae88480
|
@ -30,9 +30,13 @@ import re
|
||||||
import urllib2
|
import urllib2
|
||||||
import HTMLParser
|
import HTMLParser
|
||||||
import new
|
import new
|
||||||
|
import gtk
|
||||||
from pprint import pformat
|
from pprint import pformat
|
||||||
from sys import getfilesystemencoding
|
from sys import getfilesystemencoding
|
||||||
|
|
||||||
|
import chat_control
|
||||||
|
import groupchat_control
|
||||||
|
|
||||||
from common import helpers
|
from common import helpers
|
||||||
from common import gajim
|
from common import gajim
|
||||||
|
|
||||||
|
@ -41,6 +45,67 @@ from plugins.helpers import log_calls, log
|
||||||
from common import ged
|
from common import ged
|
||||||
from common import nec
|
from common import nec
|
||||||
|
|
||||||
|
languages = {
|
||||||
|
_('Afrikaans'): 'af',
|
||||||
|
_('Albanian'): 'sq',
|
||||||
|
_('Armenian'): 'hy',
|
||||||
|
_('Azerbaijani'): 'az',
|
||||||
|
_('Arabic'): 'ar',
|
||||||
|
_('Basque'): 'eu',
|
||||||
|
_('Belarusian'): 'be',
|
||||||
|
_('Bulgarian'): 'bg',
|
||||||
|
_('Catalan'): 'ca',
|
||||||
|
_('Chinese (Simplified)'): 'zh-cn',
|
||||||
|
_('Chinese (Traditional)'): 'zh-tw',
|
||||||
|
_('Croatian'): 'hr',
|
||||||
|
_('Czech'): 'cs',
|
||||||
|
_('Danish'): 'da',
|
||||||
|
_('Dutch'): 'nl',
|
||||||
|
_('English'): 'en',
|
||||||
|
_('Estonian'): 'et',
|
||||||
|
_('Filipino'): 'tl',
|
||||||
|
_('Finnish'): 'fi',
|
||||||
|
_('French'): 'fr',
|
||||||
|
_('Galician'): 'gl',
|
||||||
|
_('Georgian'): 'ka',
|
||||||
|
_('German'): 'de',
|
||||||
|
_('Greek'): 'el',
|
||||||
|
_('Haitian Creole'): 'ht',
|
||||||
|
_('Hebrew'): 'iw',
|
||||||
|
_('Hindi'): 'hi',
|
||||||
|
_('Hungarian'): 'hu',
|
||||||
|
_('Icelandic'): 'is',
|
||||||
|
_('Indonesian'): 'id',
|
||||||
|
_('Italian'): 'it',
|
||||||
|
_('Irish'): 'da',
|
||||||
|
_('Japanese'): 'ja',
|
||||||
|
_('Korean'): 'ko',
|
||||||
|
_('Latvian'): 'lv',
|
||||||
|
_('Lithuanian'): 'lt',
|
||||||
|
_('Macedonian'): 'mk',
|
||||||
|
_('Malay'): 'ml',
|
||||||
|
_('Maltese'): 'mt',
|
||||||
|
_('Norwegian'): 'no',
|
||||||
|
_('Persian'): 'fa',
|
||||||
|
_('Polish'): 'pl',
|
||||||
|
_('Portuguese'): 'pt-BR',
|
||||||
|
_('Romanian'): 'ro',
|
||||||
|
_('Russian'): 'ru',
|
||||||
|
_('Serbian'): 'sr',
|
||||||
|
_('Slovak'): 'sk',
|
||||||
|
_('Slovenian'): 'sl',
|
||||||
|
_('Spanish'): 'es',
|
||||||
|
_('Swahili'): 'sw',
|
||||||
|
_('Swedish'): 'sv',
|
||||||
|
_('Thai'): 'th',
|
||||||
|
_('Turkish'): 'tr',
|
||||||
|
_('Ukrainian'): 'uk',
|
||||||
|
_('Urdu'): 'ur',
|
||||||
|
_('Vietnamese'): 'vi',
|
||||||
|
_('Welsh'): 'cy',
|
||||||
|
_('Yiddish'): 'yi',
|
||||||
|
}
|
||||||
|
|
||||||
class GoogleTranslationPlugin(GajimPlugin):
|
class GoogleTranslationPlugin(GajimPlugin):
|
||||||
|
|
||||||
@log_calls('GoogleTranslationPlugin')
|
@log_calls('GoogleTranslationPlugin')
|
||||||
|
@ -50,10 +115,7 @@ class GoogleTranslationPlugin(GajimPlugin):
|
||||||
self.config_dialog = None
|
self.config_dialog = None
|
||||||
|
|
||||||
self.config_default_values = {
|
self.config_default_values = {
|
||||||
'from_lang' :
|
'per_jid_config': ({}, ''),
|
||||||
(u'en', u'Language of text to be translated'),
|
|
||||||
'to_lang' :
|
|
||||||
(u'fr', u'Language to which translation will be made'),
|
|
||||||
'user_agent' :
|
'user_agent' :
|
||||||
(u'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.12) '
|
(u'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.12) '
|
||||||
'Gecko/20080213 Firefox/2.0.0.11',
|
'Gecko/20080213 Firefox/2.0.0.11',
|
||||||
|
@ -63,6 +125,12 @@ class GoogleTranslationPlugin(GajimPlugin):
|
||||||
self.events_handlers = {'decrypted-message-received': (ged.PREGUI,
|
self.events_handlers = {'decrypted-message-received': (ged.PREGUI,
|
||||||
self._nec_decrypted_message_received)}
|
self._nec_decrypted_message_received)}
|
||||||
|
|
||||||
|
self.gui_extension_points = {
|
||||||
|
'chat_control_base' : (self.connect_with_control,
|
||||||
|
self.disconnect_from_control),
|
||||||
|
}
|
||||||
|
|
||||||
|
self.controls = []
|
||||||
self.translated_text_re = re.compile(
|
self.translated_text_re = re.compile(
|
||||||
r'google.language.callbacks.id100\(\'22\', '
|
r'google.language.callbacks.id100\(\'22\', '
|
||||||
'{"translatedText":"(?P<text>[^"]*)"}, 200, null, 200\)')
|
'{"translatedText":"(?P<text>[^"]*)"}, 200, null, 200\)')
|
||||||
|
@ -103,11 +171,16 @@ class GoogleTranslationPlugin(GajimPlugin):
|
||||||
def _nec_decrypted_message_received(self, obj):
|
def _nec_decrypted_message_received(self, obj):
|
||||||
if not obj.msgtxt:
|
if not obj.msgtxt:
|
||||||
return
|
return
|
||||||
from_lang = self.config['from_lang']
|
if obj.jid not in self.config['per_jid_config']:
|
||||||
to_lang = self.config['to_lang']
|
return
|
||||||
|
if not self.config['per_jid_config'][obj.jid]['enabled']:
|
||||||
|
return
|
||||||
|
from_lang = self.config['per_jid_config'][obj.jid]['from']
|
||||||
|
to_lang = self.config['per_jid_config'][obj.jid]['to']
|
||||||
translated_text = self.translate_text(obj.msgtxt, from_lang, to_lang)
|
translated_text = self.translate_text(obj.msgtxt, from_lang, to_lang)
|
||||||
if translated_text:
|
if translated_text:
|
||||||
obj.msgtxt = translated_text
|
obj.msgtxt = translated_text + '\n/' + _('Original text:') + '/ ' +\
|
||||||
|
obj.msgtxt
|
||||||
|
|
||||||
@log_calls('GoogleTranslationPlugin')
|
@log_calls('GoogleTranslationPlugin')
|
||||||
def activate(self):
|
def activate(self):
|
||||||
|
@ -116,3 +189,107 @@ class GoogleTranslationPlugin(GajimPlugin):
|
||||||
@log_calls('GoogleTranslationPlugin')
|
@log_calls('GoogleTranslationPlugin')
|
||||||
def deactivate(self):
|
def deactivate(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@log_calls('GoogleTranslationPlugin')
|
||||||
|
def connect_with_control(self, control):
|
||||||
|
base = Base(self, control)
|
||||||
|
self.controls.append(base)
|
||||||
|
|
||||||
|
@log_calls('GoogleTranslationPlugin')
|
||||||
|
def disconnect_from_control(self, chat_control):
|
||||||
|
for base in self.controls:
|
||||||
|
base.disconnect_from_control()
|
||||||
|
self.controls = []
|
||||||
|
|
||||||
|
class Base(object):
|
||||||
|
def __init__(self, plugin, control):
|
||||||
|
self.plugin = plugin
|
||||||
|
self.control = control
|
||||||
|
self.contact = control.contact
|
||||||
|
self.account = control.account
|
||||||
|
self.jid = self.contact.jid
|
||||||
|
if self.jid in self.plugin.config['per_jid_config']:
|
||||||
|
self.config = self.plugin.config['per_jid_config'][self.jid]
|
||||||
|
else:
|
||||||
|
self.config = {'from': '', 'to': 'en', 'enabled': False}
|
||||||
|
self.create_buttons()
|
||||||
|
|
||||||
|
def create_buttons(self):
|
||||||
|
if isinstance(self.control, chat_control.ChatControl):
|
||||||
|
vbox = self.control.xml.get_object('vbox106')
|
||||||
|
elif isinstance(self.control, groupchat_control.GroupchatControl):
|
||||||
|
vbox = self.control.xml.get_object('gc_textviews_vbox')
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.expander = gtk.Expander(_('Google translation'))
|
||||||
|
hbox = gtk.HBox(spacing=6)
|
||||||
|
self.expander.add(hbox)
|
||||||
|
label = gtk.Label(_('Translate from'))
|
||||||
|
hbox.pack_start(label, False, False)
|
||||||
|
liststore1 = gtk.ListStore(str, str)
|
||||||
|
liststore2 = gtk.ListStore(str, str)
|
||||||
|
cb1 = gtk.ComboBox(liststore1)
|
||||||
|
cb2 = gtk.ComboBox(liststore2)
|
||||||
|
cell = gtk.CellRendererText()
|
||||||
|
cb1.pack_start(cell, True)
|
||||||
|
cb1.add_attribute(cell, 'text', 0)
|
||||||
|
cell = gtk.CellRendererText()
|
||||||
|
cb2.pack_start(cell, True)
|
||||||
|
cb2.add_attribute(cell, 'text', 0)
|
||||||
|
#Language to translate from
|
||||||
|
liststore1.append([_('Auto'), ''])
|
||||||
|
if self.config['from'] == '':
|
||||||
|
cb1.set_active(0)
|
||||||
|
if self.config['from'] == '':
|
||||||
|
cb1.set_active(0)
|
||||||
|
i = 0
|
||||||
|
ls = languages.items()
|
||||||
|
ls.sort()
|
||||||
|
for l in ls:
|
||||||
|
liststore1.append(l)
|
||||||
|
if l[1] == self.config['from']:
|
||||||
|
cb1.set_active(i+1)
|
||||||
|
liststore2.append(l)
|
||||||
|
if l[1] == self.config['to']:
|
||||||
|
cb2.set_active(i)
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
hbox.pack_start(cb1, False, False)
|
||||||
|
label = gtk.Label(_('to'))
|
||||||
|
hbox.pack_start(label, False, False)
|
||||||
|
hbox.pack_start(cb2, False, False)
|
||||||
|
|
||||||
|
cb = gtk.CheckButton(_('enable'))
|
||||||
|
if self.config['enabled']:
|
||||||
|
cb.set_active(True)
|
||||||
|
hbox.pack_start(cb, False, False)
|
||||||
|
vbox.pack_start(self.expander, False, False)
|
||||||
|
vbox.reorder_child(self.expander, 1)
|
||||||
|
|
||||||
|
cb1.connect('changed', self.on_cb_changed, 'from')
|
||||||
|
cb2.connect('changed', self.on_cb_changed, 'to')
|
||||||
|
cb.connect('toggled', self.on_cb_toggled)
|
||||||
|
self.expander.show_all()
|
||||||
|
|
||||||
|
def on_cb_changed(self, widget, option):
|
||||||
|
model = widget.get_model()
|
||||||
|
it = widget.get_active_iter()
|
||||||
|
self.config[option] = model[it][1]
|
||||||
|
self.plugin.config['per_jid_config'][self.jid] = self.config
|
||||||
|
self.plugin.config.save()
|
||||||
|
|
||||||
|
def on_cb_toggled(self, widget):
|
||||||
|
self.config['enabled'] = widget.get_active()
|
||||||
|
self.plugin.config['per_jid_config'][self.jid] = self.config
|
||||||
|
self.plugin.config.save()
|
||||||
|
|
||||||
|
def disconnect_from_control(self):
|
||||||
|
if isinstance(self.control, chat_control.ChatControl):
|
||||||
|
vbox = self.control.xml.get_object('vbox106')
|
||||||
|
elif isinstance(self.control, groupchat_control.GroupchatControl):
|
||||||
|
vbox = self.control.xml.get_object('gc_textviews_vbox')
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
|
vbox.remove(self.expander)
|
||||||
|
|
Loading…
Reference in New Issue