X-Chat plugin

From Apertium
Revision as of 15:38, 19 December 2007 by Francis Tyers (talk | contribs)
Jump to navigation Jump to search

Save this to a Python file and load it with XChat.

__module_name__ = "translate"
__module_version__ = "0.1"
__module_description__ = "Uses Apertium to translate users' text"
__module_author__ = "Wynand Winterbach <wynand.winterbach@gmail.com"

import xchat
import dbus

active_pairs = set()

translator = dbus.Interface(dbus.SessionBus().get_object("org.apertium.mode", "/"), "org.apertium.Translate")
info  = dbus.Interface(dbus.SessionBus().get_object("org.apertium.info", "/"), "org.apertium.Info")
modes = info.modes()

def translate(word, word_eol, userdata):
    for pair in active_pairs:
        print "[%s]: %s" %(pair, translator.translate(pair, {}, word[1]))

    return xchat.EAT_NONE

def add_pair(word, word_eol, userdata):
    if len(word) != 2:
        print "usage: /add_pair <language_pair"
        return xchat.EAT_NONE

    _, pair = word

    if pair not in modes:
        print "invalid language pair"
        return xchat.EAT_NONE

    print "Adding %s" % pair
    active_pairs.add(pair)
    return xchat.EAT_NONE

def remove_pair(word, word_eol, userdata):
    print "Removing %s" % word[1]
    active_pairs.remove(word[1])


xchat.hook_print("Channel Message", translate) 
xchat.hook_command("add_pair", add_pair, help="/add_pair <pair> will start using the language pair to translate IRC text")
xchat.hook_command("remove_pair", remove_pair, help="/remove_pair <pair> will stop using the language pair to translate IRC text")

print "Plugin translate loaded!"

If you want Apertium to start translating a language pair (let's say fr-es), then type

/add_pair fr-es

You can stop translation of this pair by typing

/remove_pair fr-es