Back to index

moin  1.9.0~rc2
i18n.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - jabber bot i18n routines
00004 
00005     @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 import logging, xmlrpclib
00009 from jabberbot.config import BotConfig
00010 
00011 TRANSLATIONS = None
00012 
00013 
00014 def get_text(original, lang="en"):
00015     """ Return a translation of text in the user's language.
00016 
00017         @type original: unicode
00018     """
00019     if original == u"":
00020         return u""
00021 
00022     global TRANSLATIONS
00023     if not TRANSLATIONS:
00024         init_i18n(BotConfig)
00025 
00026     try:
00027         return TRANSLATIONS[lang][original]
00028     except KeyError:
00029         return original
00030 
00031 
00032 def init_i18n(config):
00033     """Prepare i18n
00034 
00035     @type config: jabberbot.config.BotConfig
00036 
00037     """
00038     global TRANSLATIONS
00039     TRANSLATIONS = request_translations(config) or {'en': {}}
00040 
00041 
00042 def request_translations(config):
00043     """Download translations from wiki using xml rpc
00044 
00045     @type config: jabberbot.config.BotConfig
00046 
00047     """
00048 
00049     wiki = xmlrpclib.Server(config.wiki_url + "?action=xmlrpc2")
00050     log = logging.getLogger(__name__)
00051     log.debug("Initialising i18n...")
00052 
00053     try:
00054         translations =  wiki.getBotTranslations()
00055         return translations
00056     except xmlrpclib.Fault, fault:
00057         log.error("XML RPC fault occured while getting translations: %s" % (str(fault), ))
00058     except xmlrpclib.Error, error:
00059         log.error("XML RPC error occured while getting translations: %s" % (str(error), ))
00060     except Exception, exc:
00061         log.error("Unexpected exception occured while getting translations: %s" % (str(exc), ))
00062 
00063     log.error("Translations could not be downloaded, is wiki is accesible?")
00064     return None