summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Vasiliev <dima@hlabs.spb.ru>2005-11-03 14:23:04 +0000
committerDmitry Vasiliev <dima@hlabs.spb.ru>2005-11-03 14:23:04 +0000
commit8ff10f8437077615a2113a418b558d3cf83de11b (patch)
treeb322860b6a9881ff0570092e7a00b4e550f9d9c1
parent5c8164388da1e64f8789f90a390b9b090b0bc069 (diff)
downloadzope-tal-8ff10f8437077615a2113a418b558d3cf83de11b.tar.gz
Fixed issue #455: tal:content="" does not get translated by i18n:translate=""
-rw-r--r--talinterpreter.py54
-rw-r--r--tests/test_talinterpreter.py52
2 files changed, 77 insertions, 29 deletions
diff --git a/talinterpreter.py b/talinterpreter.py
index 0a79f71..1149ee8 100644
--- a/talinterpreter.py
+++ b/talinterpreter.py
@@ -20,8 +20,6 @@ import operator
import sys
import warnings
-from types import StringType, UnicodeType
-
# Do not use cStringIO here! It's not unicode aware. :(
from StringIO import StringIO
@@ -37,9 +35,12 @@ from zope.tal.taldefs import getProgramVersion, getProgramMode
from zope.tal.talgenerator import TALGenerator
from zope.tal.translationcontext import TranslationContext
+
# Avoid constructing this tuple over and over
I18nMessageTypes = (MessageID, Message)
+TypesToTranslate = I18nMessageTypes + (str, unicode)
+
# TODO: In Python 2.4 we can use frozenset() instead of dict.fromkeys()
BOOLEAN_HTML_ATTRS = dict.fromkeys([
# List of Boolean attributes in HTML that should be rendered in
@@ -650,16 +651,13 @@ class TALInterpreter(object):
def do_insertI18nText_tal(self, stuff):
# TODO: Code duplication is BAD, we need to fix it later
text = self.engine.evaluateText(stuff[0])
- if text is None:
- return
- if text is self.Default:
- self.interpret(stuff[1])
- return
- if isinstance(text, I18nMessageTypes):
- text = self.translate(text)
- elif isinstance(text, (StringType, UnicodeType)):
- text = self.translate(text)
- self._writeText(text)
+ if text is not None:
+ if text is self.Default:
+ self.interpret(stuff[1])
+ else:
+ if isinstance(text, TypesToTranslate):
+ text = self.translate(text)
+ self._writeText(text)
def do_i18nVariable(self, stuff):
varname, program, expression, structure = stuff
@@ -782,23 +780,21 @@ class TALInterpreter(object):
def do_insertI18nStructure_tal(self, (expr, repldict, block)):
# TODO: Code duplication is BAD, we need to fix it later
structure = self.engine.evaluateStructure(expr)
- if structure is None:
- return
- if structure is self.Default:
- self.interpret(block)
- return
- if isinstance(structure, I18nMessageTypes):
- text = self.translate(structure)
- else:
- text = unicode(structure)
- if not (repldict or self.strictinsert):
- # Take a shortcut, no error checking
- self.stream_write(text)
- return
- if self.html:
- self.insertHTMLStructure(text, repldict)
- else:
- self.insertXMLStructure(text, repldict)
+ if structure is not None:
+ if structure is self.Default:
+ self.interpret(block)
+ else:
+ if isinstance(structure, TypesToTranslate):
+ text = self.translate(structure)
+ else:
+ text = unicode(structure)
+ if not (repldict or self.strictinsert):
+ # Take a shortcut, no error checking
+ self.stream_write(text)
+ elif self.html:
+ self.insertHTMLStructure(text, repldict)
+ else:
+ self.insertXMLStructure(text, repldict)
def insertHTMLStructure(self, text, repldict):
from zope.tal.htmltalparser import HTMLTALParser
diff --git a/tests/test_talinterpreter.py b/tests/test_talinterpreter.py
index 4985816..adf8166 100644
--- a/tests/test_talinterpreter.py
+++ b/tests/test_talinterpreter.py
@@ -159,6 +159,58 @@ class I18NCornerTestCaseBase(TestCaseBase):
'<span i18n:translate="" tal:replace="foo"/>')
self._check(program, 'FOOVALUE\n')
+ def test_text_variable_translate(self):
+ program, macros = self._compile(
+ '<span tal:content="bar"/>')
+ self._check(program, '<span>BaRvAlUe</span>\n')
+
+ program, macros = self._compile(
+ '<span i18n:translate="" tal:content="bar"/>')
+ self._check(program, '<span>BARVALUE</span>\n')
+
+ program, macros = self._compile(
+ '<span i18n:translate="" tal:replace="bar"/>')
+ self._check(program, 'BARVALUE\n')
+
+ def test_text_translate(self):
+ program, macros = self._compile(
+ '<span tal:content="string:BaR"/>')
+ self._check(program, '<span>BaR</span>\n')
+
+ program, macros = self._compile(
+ '<span i18n:translate="" tal:content="string:BaR"/>')
+ self._check(program, '<span>BAR</span>\n')
+
+ program, macros = self._compile(
+ '<span i18n:translate="" tal:replace="string:BaR"/>')
+ self._check(program, 'BAR\n')
+
+ def test_structure_text_variable_translate(self):
+ program, macros = self._compile(
+ '<span tal:content="structure bar"/>')
+ self._check(program, '<span>BaRvAlUe</span>\n')
+
+ program, macros = self._compile(
+ '<span i18n:translate="" tal:content="structure bar"/>')
+ self._check(program, '<span>BARVALUE</span>\n')
+
+ program, macros = self._compile(
+ '<span i18n:translate="" tal:replace="structure bar"/>')
+ self._check(program, 'BARVALUE\n')
+
+ def test_structure_text_translate(self):
+ program, macros = self._compile(
+ '<span tal:content="structure string:BaR"/>')
+ self._check(program, '<span>BaR</span>\n')
+
+ program, macros = self._compile(
+ '<span i18n:translate="" tal:content="structure string:BaR"/>')
+ self._check(program, '<span>BAR</span>\n')
+
+ program, macros = self._compile(
+ '<span i18n:translate="" tal:replace="structure string:BaR"/>')
+ self._check(program, 'BAR\n')
+
def test_replace_with_messageid_and_i18nname(self):
program, macros = self._compile(
'<div i18n:translate="" >'