Skip to content

Commit 0cc33fb

Browse files
author
Alexander Smishlajev
committed
TranslationService for Roundup templates
1 parent f273e50 commit 0cc33fb

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

roundup/cgi/TranslationService.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
#
15+
16+
__version__ = "$Revision: 1.1 $"[11:-2]
17+
__date__ = "$Date: 2004-07-11 14:17:17 $"[7:-2]
18+
19+
from roundup import i18n
20+
from roundup.cgi.PageTemplates import Expressions, PathIterator, TALES
21+
from roundup.cgi.TAL import TALInterpreter
22+
23+
### Translation classes
24+
25+
class TranslationServiceMixin:
26+
27+
OUTPUT_ENCODING = "utf-8"
28+
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
36+
37+
def gettext(self, msgid):
38+
return self.ugettext(msgid).encode(self.OUTPUT_ENCODING)
39+
40+
def ngettext(self, singular, plural, number):
41+
return self.ungettext(singular, plural, number).encode(
42+
self.OUTPUT_ENCODING)
43+
44+
class TranslationService(TranslationServiceMixin, i18n.RoundupTranslations):
45+
pass
46+
47+
class NullTranslationService(TranslationServiceMixin,
48+
i18n.RoundupNullTranslations
49+
):
50+
pass
51+
52+
### TAL patching
53+
#
54+
# Template Attribute Language (TAL) uses only global translation service,
55+
# which is not thread-safe. We will use context variable 'i18n'
56+
# to access request-dependent transalation service (with domain
57+
# and target language set during initializations of the roundup
58+
# client interface.
59+
#
60+
61+
class Context(TALES.Context):
62+
63+
def __init__(self, compiler, contexts):
64+
TALES.Context.__init__(self, compiler, contexts)
65+
if not self.contexts.get('i18n', None):
66+
# if the context contains no TranslationService,
67+
# create default one
68+
self.contexts['i18n'] = get_translation()
69+
self.i18n = self.contexts['i18n']
70+
71+
def translate(self, domain, msgid, mapping=None,
72+
context=None, target_language=None, default=None):
73+
if context is None:
74+
context = self.contexts.get('here')
75+
return self.i18n.translate(domain, msgid,
76+
mapping=mapping, context=context, default=default,
77+
target_language=target_language)
78+
79+
class Engine(TALES.Engine):
80+
81+
def getContext(self, contexts=None, **kwcontexts):
82+
if contexts is not None:
83+
if kwcontexts:
84+
kwcontexts.update(contexts)
85+
else:
86+
kwcontexts = contexts
87+
return Context(self, kwcontexts)
88+
89+
# patching TAL like this is a dirty hack,
90+
# but i see no other way to specify different Context class
91+
Expressions._engine = Engine(PathIterator.Iterator)
92+
Expressions.installHandlers(Expressions._engine)
93+
94+
### main API function
95+
96+
def get_translation(language=None, domain=i18n.DOMAIN,
97+
translation_class=TranslationService,
98+
null_translation_class=NullTranslationService
99+
):
100+
"""Return Translation object for given language and domain
101+
102+
Arguments 'translation_class' and 'null_translation_class'
103+
specify the classes that are instantiated for existing
104+
and non-existing translations, respectively.
105+
"""
106+
return i18n.get_translation(language=language, domain=domain,
107+
translation_class=translation_class,
108+
null_translation_class=null_translation_class)
109+
110+
# vim: set et sts=4 sw=4 :

0 commit comments

Comments
 (0)