diff jabberbot/i18n.py @ 2461:f456dc2048d1

i18n should now work. First step, no caching yet.
author Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
date Fri, 20 Jul 2007 00:24:21 +0200
parents 1007128cb089
children 754e3eedb9e5
line wrap: on
line diff
--- a/jabberbot/i18n.py	Fri Jul 20 00:18:41 2007 +0200
+++ b/jabberbot/i18n.py	Fri Jul 20 00:24:21 2007 +0200
@@ -5,20 +5,61 @@
     @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
     @license: GNU GPL, see COPYING for details.
 """
+import logging, xmlrpclib
 
 translations = None
 
+
 def getText(original, lang="en"):
+    """ Return a translation of text in the user's language.
+
+        @type original: unicode
+    """
+    if original == u"":
+        return u""
+
     global translations
-
     if not translations:
         init_i18n()
 
+    # get the matching entry in the mapping table
+    translated = original
     try:
         return translations[lang][original]
     except KeyError:
         return original
 
-def init_i18n():
+
+def init_i18n(config):
+    """Prepare i18n
+
+    @type config: jabberbot.config.BotConfig
+
+    """
     global translations
-    translations = {'en': {}}
+    translations = request_translations(config) or {'en': {}}
+
+
+def request_translations(config):
+    """Download translations from wiki using xml rpc
+
+    @type config: jabberbot.config.BotConfig
+
+    """
+
+    wiki = xmlrpclib.Server(config.wiki_url + "?action=xmlrpc2")
+    log = logging.getLogger("log")
+    log.debug("Initialising i18n...")
+
+    try:
+        translations =  wiki.getBotTranslations()
+        return translations
+    except xmlrpclib.Fault, fault:
+        log.error("XML RPC fault occured while getting translations: %s" % (str(fault), ))
+    except xmlrpclib.Error, error:
+        log.error("XML RPC error occured while getting translations: %s" % (str(error), ))
+    except Exception, exc:
+        log.error("Unexpected exception occured while getting translations: %s" % (str(exc), ))
+
+    log.error("Translations could not be downloaded, is wiki is accesible?")
+    return None