X-Chat plugin

From Apertium
Revision as of 22:11, 7 March 2018 by Francis Tyers (talk | contribs)
Jump to navigation Jump to search
WARNING

This page is out of date as a result of the migration to GitHub. Please update this page with new documentation and remove this warning. If you are unsure how to proceed, please contact the GitHub migration team.

(See also the newer Plugin for XChat)


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

Pre-requisites

SVN version

This module is being further developed in our SVN repository. It can be found under apertium-tools/xchat-modules.

Script

__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