Skip to content

Instantly share code, notes, and snippets.

@ueno
Created March 8, 2012 03:53
Show Gist options
  • Save ueno/1998538 to your computer and use it in GitHub Desktop.
Save ueno/1998538 to your computer and use it in GitHub Desktop.
# Will cause pygobject2 be loaded.
import ibus
try:
# pygobject3 cannot be loaded at the same time pygobject2 is
# loaded, so this will raise an exception on Fedora 16 or later.
from gi.repository import Translit
Transliterator = Translit.Transliterator
except ImportError:
# Load libtranslit through ctypes, instead of gi. Maybe the code
# below could be removed once ibus-table switches to pygobject3.
import ctypes
class Transliterator(object):
__libtranslit = ctypes.CDLL("libtranslit.so",
mode=ctypes.RTLD_GLOBAL)
__get = __libtranslit.translit_transliterator_get
__get.argtypes = [ctypes.c_char_p,
ctypes.c_char_p]
__get.restype = ctypes.c_void_p
__transliterate = __libtranslit.translit_transliterator_transliterate
__transliterate.argtypes = [ctypes.c_void_p,
ctypes.c_char_p,
ctypes.c_void_p]
__transliterate.restype = ctypes.c_char_p
def __init__(self, trans):
self.__trans = trans
@staticmethod
def get(backend, name):
return Transliterator(Transliterator.__get(backend, name))
def transliterate(self, _input):
endpos = ctypes.c_ulong()
output = Transliterator.__transliterate(self.__trans,
_input,
ctypes.byref(endpos))
return (output, endpos.value)
if __name__ == "__main__":
trans = Transliterator.get("m17n", "hi-inscript")
print trans.transliterate("ab")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment