[Zope3-checkins] CVS: Zope3/src/zope/tal - driver.py:1.3 dummyengine.py:1.6 talinterpreter.py:1.9
Fred L. Drake, Jr.
fred@zope.com
Thu, 3 Apr 2003 14:42:38 -0500
Update of /cvs-repository/Zope3/src/zope/tal
In directory cvs.zope.org:/tmp/cvs-serv22769
Modified Files:
driver.py dummyengine.py talinterpreter.py
Log Message:
Pass the default text to the translation service so intepolations can be
performed if necessary. Added a test.
=== Zope3/src/zope/tal/driver.py 1.2 => 1.3 ===
--- Zope3/src/zope/tal/driver.py:1.2 Wed Dec 25 09:15:29 2002
+++ Zope3/src/zope/tal/driver.py Thu Apr 3 14:42:08 2003
@@ -53,7 +53,7 @@
class TestTranslations(DummyTranslationService):
def translate(self, domain, msgid, mapping=None, context=None,
- target_language=None):
+ target_language=None, default=None):
if msgid == 'timefmt':
return '%(minutes)s minutes after %(hours)s %(ampm)s' % mapping
elif msgid == 'jobnum':
@@ -67,7 +67,8 @@
return '%(name)s was born in %(country)s' % mapping
return DummyTranslationService.translate(self, domain, msgid,
mapping, context,
- target_language)
+ target_language,
+ default=default)
class TestEngine(DummyEngine):
def __init__(self, macros=None):
=== Zope3/src/zope/tal/dummyengine.py 1.5 => 1.6 ===
--- Zope3/src/zope/tal/dummyengine.py:1.5 Tue Mar 25 18:25:16 2003
+++ Zope3/src/zope/tal/dummyengine.py Thu Apr 3 14:42:08 2003
@@ -189,8 +189,9 @@
def getDefault(self):
return Default
- def translate(self, domain, msgid, mapping):
- return self.translationService.translate(domain, msgid, mapping)
+ def translate(self, domain, msgid, mapping, default=None):
+ return self.translationService.translate(domain, msgid, mapping,
+ default=default)
class Iterator:
@@ -216,7 +217,7 @@
__implements__ = ITranslationService
def translate(self, domain, msgid, mapping=None, context=None,
- target_language=None):
+ target_language=None, default=None):
# This is a fake translation service which simply uppercases non
# ${name} placeholder text in the message id.
#
@@ -225,10 +226,12 @@
# things back together.
# simulate an unknown msgid by returning None
- if msgid=="don't translate me":
- return None
-
+ if msgid == "don't translate me":
+ text = default
+ else:
+ text = msgid.upper()
+
def repl(m):
return mapping[m.group(m.lastindex).lower()]
- cre = re.compile(r'\$(?:([_A-Z]\w*)|\{([_A-Z]\w*)\})')
- return cre.sub(repl, msgid.upper())
+ cre = re.compile(r'\$(?:([_A-Za-z][-\w]*)|\{([_A-Za-z][-\w]*)\})')
+ return cre.sub(repl, text)
=== Zope3/src/zope/tal/talinterpreter.py 1.8 => 1.9 ===
--- Zope3/src/zope/tal/talinterpreter.py:1.8 Thu Apr 3 11:18:38 2003
+++ Zope3/src/zope/tal/talinterpreter.py Thu Apr 3 14:42:08 2003
@@ -336,7 +336,7 @@
value = evalue
if ok:
if xlat:
- translated = self.i18n_attribute(msgid or value)
+ translated = self.translate(msgid or value, value, {})
if translated is not None:
value = translated
if value is None:
@@ -344,12 +344,6 @@
value = "%s=%s" % (name, quote(value))
return ok, name, value
- def i18n_attribute(self, s):
- # s is the value of an attribute before translation
- # it may have been computed
- return self.translate(s, {})
-
-
bytecode_handlers["<attrAction>"] = attrAction
def no_tag(self, start, program):
@@ -536,33 +530,34 @@
# See if there is was an i18n:data for msgid
if len(stuff) > 2:
obj = self.engine.evaluate(stuff[2])
- xlated_msgid = self.translate(msgid, i18ndict, obj)
+ xlated_msgid = self.translate(msgid, default, i18ndict, obj)
# XXX I can't decide whether we want to cgi escape the translated
# string or not. OT1H not doing this could introduce a cross-site
# scripting vector by allowing translators to sneak JavaScript into
# translations. OTOH, for implicit interpolation values, we don't
# want to escape stuff like ${name} <= "<b>Timmy</b>".
#s = escape(xlated_msgid)
- s = xlated_msgid
+ #s = xlated_msgid
# Watch out for unknown translation message id. In this case, and
# when both an explicit message id and default text are given, the
# Plone people want the default text, so by Papal Edict this is what
# we return. For example:
#
- # <span i18n:translate="explict id">default text</span>
+ # <span i18n:translate="explicit id">default text</span>
#
# returns
#
# <span>default text</span>
- if s is None:
- s = default or msgid
- # log that an unknown id was found
- logging.warn('TAL/i18n: Message id %s was not found in the '
- 'translation table; using default text: %s' %
- (msgid, default))
+ assert xlated_msgid is not None
+## if xlated_msgid is None:
+## s = default or msgid
+## # log that an unknown id was found
+## logging.warn('TAL/i18n: Message id %s was not found in the '
+## 'translation table; using default text: %s' %
+## (msgid, default))
# If there are i18n variables to interpolate into this string, better
# do it now.
- self._stream_write(s)
+ self._stream_write(xlated_msgid)
bytecode_handlers['insertTranslation'] = do_insertTranslation
def do_insertStructure(self, stuff):
@@ -616,20 +611,14 @@
self.interpret(block)
bytecode_handlers["loop"] = do_loop
- def translate(self, msgid, i18ndict=None, obj=None):
- # XXX is this right?
- if i18ndict is None:
- i18ndict = {}
+ def translate(self, msgid, default, i18ndict, obj=None):
if obj:
i18ndict.update(obj)
if not self.i18nInterpolate:
return msgid
- # XXX Mmmh, it seems that sometimes the msgid is None; is that really
- # possible?
- if msgid is None:
- return None
# XXX We need to pass in one of context or target_language
- return self.engine.translate(self.i18nContext.domain, msgid, i18ndict)
+ return self.engine.translate(self.i18nContext.domain,
+ msgid, i18ndict, default=default)
def do_rawtextColumn(self, (s, col)):
self._stream_write(s)