Code

- refactor: move import_journal to hyperdb -- it was reimplemented in
[roundup.git] / roundup / cgi / TranslationService.py
1 # TranslationService for Roundup templates
2 #
3 # This module is free software, you may redistribute it
4 # and/or modify under the same terms as Python.
5 #
6 # This module provides National Language Support
7 # for Roundup templating - much like roundup.i18n
8 # module for Roundup command line interface.
9 # The only difference is that translator objects
10 # returned by get_translation() have one additional
11 # method which is used by TAL engines:
12 #
13 #   translate(domain, msgid, mapping, context, target_language, default)
14 #
16 __version__ = "$Revision: 1.6 $"[11:-2]
17 __date__ = "$Date: 2008-08-18 05:04:01 $"[7:-2]
19 from roundup import i18n
20 from roundup.cgi.PageTemplates import Expressions, PathIterator, TALES
21 from roundup.cgi.TAL import TALInterpreter
23 ### Translation classes
25 class TranslationServiceMixin:
27     OUTPUT_ENCODING = "utf-8"
29     def translate(self, domain, msgid, mapping=None,
30         context=None, target_language=None, default=None
31     ):
32         _msg = self.gettext(msgid)
33         #print ("TRANSLATE", msgid, _msg, mapping, context)
34         _msg = TALInterpreter.interpolate(_msg, mapping)
35         return _msg
37     def gettext(self, msgid):
38         if not isinstance(msgid, unicode):
39             msgid = unicode(msgid, 'utf8')
40         msgtrans=self.ugettext(msgid)
41         return msgtrans.encode(self.OUTPUT_ENCODING)
43     def ngettext(self, singular, plural, number):
44         if not isinstance(singular, unicode):
45             singular = unicode(singular, 'utf8')
46         if not isinstance(plural, unicode):
47             plural = unicode(plural, 'utf8')
48         msgtrans=self.ungettext(singular, plural, number)
49         return msgtrans.encode(self.OUTPUT_ENCODING)
51 class TranslationService(TranslationServiceMixin, i18n.RoundupTranslations):
52     pass
54 class NullTranslationService(TranslationServiceMixin,
55         i18n.RoundupNullTranslations):
56     def ugettext(self, message):
57         if self._fallback:
58             return self._fallback.ugettext(message)
59         # Sometimes the untranslatable message is a UTF-8 encoded string
60         # (thanks to PageTemplate's internals).
61         if not isinstance(message, unicode):
62             return unicode(message, 'utf8')
63         return message
65 ### TAL patching
66 #
67 # Template Attribute Language (TAL) uses only global translation service,
68 # which is not thread-safe.  We will use context variable 'i18n'
69 # to access request-dependent transalation service (with domain
70 # and target language set during initializations of the roundup
71 # client interface.
72 #
74 class Context(TALES.Context):
76     def __init__(self, compiler, contexts):
77         TALES.Context.__init__(self, compiler, contexts)
78         if not self.contexts.get('i18n', None):
79             # if the context contains no TranslationService,
80             # create default one
81             self.contexts['i18n'] = get_translation()
82         self.i18n = self.contexts['i18n']
84     def translate(self, domain, msgid, mapping=None,
85                   context=None, target_language=None, default=None):
86         if context is None:
87             context = self.contexts.get('here')
88         return self.i18n.translate(domain, msgid,
89             mapping=mapping, context=context, default=default,
90             target_language=target_language)
92 class Engine(TALES.Engine):
94     def getContext(self, contexts=None, **kwcontexts):
95         if contexts is not None:
96             if kwcontexts:
97                 kwcontexts.update(contexts)
98             else:
99                 kwcontexts = contexts
100         return Context(self, kwcontexts)
102 # patching TAL like this is a dirty hack,
103 # but i see no other way to specify different Context class
104 Expressions._engine = Engine(PathIterator.Iterator)
105 Expressions.installHandlers(Expressions._engine)
107 ### main API function
109 def get_translation(language=None, tracker_home=None,
110     translation_class=TranslationService,
111     null_translation_class=NullTranslationService
112 ):
113     """Return Translation object for given language and domain
115     Arguments 'translation_class' and 'null_translation_class'
116     specify the classes that are instantiated for existing
117     and non-existing translations, respectively.
118     """
119     return i18n.get_translation(language=language,
120         tracker_home=tracker_home,
121         translation_class=translation_class,
122         null_translation_class=null_translation_class)
124 # vim: set et sts=4 sw=4 :