annotate jabberbot/i18n.py @ 2999:e94b940f3a84

add a comment about listen backlog to moin.fcg (port from 1.6)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 06 Jan 2008 17:32:33 +0100
parents af857f8c2e44
children 6f06c7d051a4
rev   line source
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
2 """
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
3 MoinMoin - jabber bot i18n routines
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
4
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
5 @copyright: 2007 by Karol Nowak <grywacz@gmail.com>
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
7 """
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
8 import logging, xmlrpclib
2640
af857f8c2e44 Import BotConfig in i18n module (makes testing easier...)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
9 from jabberbot.config import BotConfig
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
10
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2473
diff changeset
11 TRANSLATIONS = None
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
12
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
13
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2473
diff changeset
14 def get_text(original, lang="en"):
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
15 """ Return a translation of text in the user's language.
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
16
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
17 @type original: unicode
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
18 """
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
19 if original == u"":
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
20 return u""
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
21
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2473
diff changeset
22 global TRANSLATIONS
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2473
diff changeset
23 if not TRANSLATIONS:
2640
af857f8c2e44 Import BotConfig in i18n module (makes testing easier...)
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2504
diff changeset
24 init_i18n(BotConfig)
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
25
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
26 try:
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2473
diff changeset
27 return TRANSLATIONS[lang][original]
2413
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
28 except KeyError:
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
29 return original
61380308535d Added a dummy _ function so that string for translations can be gathered.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents:
diff changeset
30
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
31
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
32 def init_i18n(config):
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
33 """Prepare i18n
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
34
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
35 @type config: jabberbot.config.BotConfig
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
36
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
37 """
2504
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2473
diff changeset
38 global TRANSLATIONS
c27ee67397f0 Make pylint happy where it made sense, fix a few minor bugs.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2473
diff changeset
39 TRANSLATIONS = request_translations(config) or {'en': {}}
2461
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
40
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
41
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
42 def request_translations(config):
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
43 """Download translations from wiki using xml rpc
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
44
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
45 @type config: jabberbot.config.BotConfig
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
46
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
47 """
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
48
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
49 wiki = xmlrpclib.Server(config.wiki_url + "?action=xmlrpc2")
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
50 log = logging.getLogger("log")
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
51 log.debug("Initialising i18n...")
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
52
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
53 try:
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
54 translations = wiki.getBotTranslations()
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
55 return translations
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
56 except xmlrpclib.Fault, fault:
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
57 log.error("XML RPC fault occured while getting translations: %s" % (str(fault), ))
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
58 except xmlrpclib.Error, error:
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
59 log.error("XML RPC error occured while getting translations: %s" % (str(error), ))
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
60 except Exception, exc:
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
61 log.error("Unexpected exception occured while getting translations: %s" % (str(exc), ))
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
62
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
63 log.error("Translations could not be downloaded, is wiki is accesible?")
f456dc2048d1 i18n should now work. First step, no caching yet.
Karol 'grzywacz' Nowak <grzywacz@sul.uni.lodz.pl>
parents: 2415
diff changeset
64 return None