def __init__(self, expressionCompiler=None, xml=1, source_file=None): if not expressionCompiler: from zope.tal.dummyengine import DummyEngine expressionCompiler = DummyEngine() self.expressionCompiler = expressionCompiler self.CompilerError = expressionCompiler.getCompilerError() # This holds the emitted opcodes representing the input self.program = [] # The program stack for when we need to do some sub-evaluation for an # intermediate result. E.g. in an i18n:name tag for which the # contents describe the ${name} value. self.stack = [] # Another stack of postponed actions. Elements on this stack are a # dictionary; key/values contain useful information that # emitEndElement needs to finish its calculations self.todoStack = [] self.macros = {} # {slot-name --> default content program} self.slots = {} self.slotStack = [] self.xml = xml # true --> XML, false --> HTML self.emit("version", TAL_VERSION) self.emit("mode", xml and "xml" or "html") if source_file is not None: self.source_file = source_file self.emit("setSourceFile", source_file) self.i18nContext = TranslationContext() self.i18nLevel = 0
def __init__(self, expressionCompiler=None, xml=1, source_file=None): """ :keyword expressionCompiler: The implementation of :class:`zope.tal.interfaces.ITALExpressionCompiler` to use. If not given, we'll use a simple, undocumented, compiler. """ if not expressionCompiler: from zope.tal.dummyengine import DummyEngine expressionCompiler = DummyEngine() self.expressionCompiler = expressionCompiler self.CompilerError = expressionCompiler.getCompilerError() # This holds the emitted opcodes representing the input self.program = [] # The program stack for when we need to do some sub-evaluation for an # intermediate result. E.g. in an i18n:name tag for which the # contents describe the ${name} value. self.stack = [] # Another stack of postponed actions. Elements on this stack are a # dictionary; key/values contain useful information that # emitEndElement needs to finish its calculations self.todoStack = [] self.macros = {} # {slot-name --> default content program} self.slots = {} self.slotStack = [] self.xml = xml # true --> XML, false --> HTML self.emit("version", TAL_VERSION) self.emit("mode", xml and "xml" or "html") if source_file is not None: self.source_file = source_file self.emit("setSourceFile", source_file) self.i18nContext = TranslationContext() self.i18nLevel = 0
def time_tal(fn, count): p = HTMLTALParser() p.parseFile(fn) program, macros = p.getCode() engine = DummyEngine(macros) engine.globals = data tal = TALInterpreter(program, macros, engine, StringIO(), wrap=0, tal=1, strictinsert=0) return time_apply(tal, (), {}, count)
def profile_tal(fn, count, profiler): p = HTMLTALParser() p.parseFile(fn) program, macros = p.getCode() engine = DummyEngine(macros) engine.globals = data tal = TALInterpreter(program, macros, engine, StringIO(), wrap=0, tal=1, strictinsert=0) for i in range(4): tal() r = [None] * count for i in r: profiler.runcall(tal)
def setUp(self): program = [] macros = {} engine = DummyEngine() self.interpreter = TALInterpreter(program, macros, engine) self.sio = self.interpreter.stream = StringIO() self.interpreter._pending_source_annotation = True
def test_div_in_p_using_macro(self): dummy, macros = self._compile('<p metal:define-macro="M">Booh</p>') engine = DummyEngine(macros) program, dummy = self._compile( '<p metal:use-macro="M"><div>foo</div></p>') interpreter = TALInterpreter(program, {}, engine) output = interpreter() self.assertEqual(output, '<p><div>foo</div></p>')
def evaluatePathOrVar(self, expr): if expr == 'here/currentTime': return {'hours' : 6, 'minutes': 59, 'ampm' : 'PM', } elif expr == 'context/@@object_name': return '7' elif expr == 'request/submitter': return '*****@*****.**' return DummyEngine.evaluatePathOrVar(self, expr)
def test(): import sys p = TALParser() file = "tests/input/test01.xml" if sys.argv[1:]: file = sys.argv[1] p.parseFile(file) program, macros = p.getCode() from zope.tal.talinterpreter import TALInterpreter from zope.tal.dummyengine import DummyEngine engine = DummyEngine(macros) TALInterpreter(program, macros, engine, sys.stdout, wrap=0)()
def setUp(self): s = self._read(('input', 'pnome_template.pt')) self.pnome_program, pnome_macros = self._compile(s) s = self._read(('input', 'acme_template.pt')) self.acme_program, acme_macros = self._compile(s) s = self._read(('input', 'document_list.pt')) self.doclist_program, _doclist_macros = self._compile(s) macros = { 'pnome_macros_page': pnome_macros['page'], 'acme_macros_page': acme_macros['page'], } self.engine = DummyEngine(macros)
def test_div_in_p_using_macro(self): # We have not found a solution for this # and it is a deep and undocumented HTML parser issue. # Fred is looking into this. dummy, macros = self._compile('<p metal:define-macro="M">Booh</p>') engine = DummyEngine(macros) program, dummy = self._compile( '<p metal:use-macro="M"><div>foo</div></p>') interpreter = TALInterpreter(program, {}, engine) output = interpreter() self.assertEqual(output, '<p><div>foo</div></p>')
def evaluatePathOrVar(self, expr): if expr == 'here/currentTime': return { 'hours': 6, 'minutes': 59, 'ampm': 'PM', } elif expr == 'context/@@object_name': return '7' elif expr == 'request/submitter': return '*****@*****.**' return DummyEngine.evaluatePathOrVar(self, expr)
def test_source_positions(self): # Ensure source file and position are set correctly by TAL macros = {} eng = DummyEngine(macros) page1_program, page1_macros = self.parse(eng, page1, 'page1') main_template_program, main_template_macros = self.parse( eng, main_template, 'main_template') footer_program, footer_macros = self.parse(eng, footer, 'footer') macros['main'] = main_template_macros['main'] macros['foot'] = footer_macros['foot'] stream = StringIO() interp = TALInterpreter(page1_program, macros, eng, stream) interp() self.assertEqual(stream.getvalue().strip(), expected.strip(), "Got result:\n%s\nExpected:\n%s" % (stream.getvalue(), expected))
def test_define_slot_restores_source_file_if_no_exception(self): _m_program, m_macros = self._compile(""" <div metal:define-macro="amacro"> <div metal:define-slot="aslot"> </div> <tal:x replace="no_such_thing" /> </div> """, source_file='macros.pt') p_program, _p_macros = self._compile(""" <div metal:use-macro="amacro"> <div metal:fill-slot="aslot"> </div> </div> """, source_file='page.pt') engine = DummyEngine(macros=m_macros) interp = TALInterpreter(p_program, {}, engine, StringIO()) # Expect TALExpressionError: unknown variable: 'no_such_thing' self.assertRaises(TALExpressionError, interp) # Now the engine should know where the error occurred self.assertEqual(engine.source_file, 'macros.pt') self.assertEqual(engine.position, (5, 14))
def interpretit(it, engine=None, stream=None, tal=1, showtal=-1, strictinsert=1, i18nInterpolate=1, sourceAnnotations=0): from zope.tal.talinterpreter import TALInterpreter program, macros = it assert zope.tal.taldefs.isCurrentVersion(program) if engine is None: engine = DummyEngine(macros) TALInterpreter(program, macros, engine, stream, wrap=0, tal=tal, showtal=showtal, strictinsert=strictinsert, i18nInterpolate=i18nInterpolate, sourceAnnotations=sourceAnnotations)()
def __init__(self, macros=None): self.catalog = {} DummyEngine.__init__(self, macros)
class I18NCornerTestCaseBase(TestCaseBase): def factory(self, msgid, default, mapping={}): raise NotImplementedError("abstract method") def setUp(self): self.engine = DummyEngine() # Make sure we'll translate the msgid not its unicode representation self.engine.setLocal('foo', self.factory('FoOvAlUe${empty}', 'default', {'empty': ''})) self.engine.setLocal('bar', 'BaRvAlUe') def _check(self, program, expected): result = StringIO() self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() self.assertEqual(expected, result.getvalue()) def test_simple_messageid_translate(self): # This test is mainly here to make sure our DummyEngine works # correctly. program, macros = self._compile( '<span i18n:translate="" tal:content="foo"/>') self._check(program, '<span>FOOVALUE</span>\n') program, macros = self._compile( '<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="" >' '<span i18n:translate="" tal:replace="foo" i18n:name="foo_name"/>' '</div>') self._check(program, '<div>FOOVALUE</div>\n') def test_pythonexpr_replace_with_messageid_and_i18nname(self): program, macros = self._compile( '<div i18n:translate="" >' '<span i18n:translate="" tal:replace="python: foo"' ' i18n:name="foo_name"/>' '</div>') self._check(program, '<div>FOOVALUE</div>\n') def test_structure_replace_with_messageid_and_i18nname(self): program, macros = self._compile( '<div i18n:translate="" >' '<span i18n:translate="" tal:replace="structure foo"' ' i18n:name="foo_name"/>' '</div>') self._check(program, '<div>FOOVALUE</div>\n') def test_complex_replace_with_messageid_and_i18nname(self): program, macros = self._compile( '<div i18n:translate="" >' '<em tal:omit-tag="" i18n:name="foo_name">' '<span i18n:translate="" tal:replace="foo"/>' '</em>' '</div>') self._check(program, '<div>FOOVALUE</div>\n') def test_content_with_messageid_and_i18nname(self): program, macros = self._compile( '<div i18n:translate="" >' '<span i18n:translate="" tal:content="foo" i18n:name="foo_name"/>' '</div>') self._check(program, '<div><span>FOOVALUE</span></div>\n') def test_content_with_messageid_and_i18nname_and_i18ntranslate(self): # Let's tell the user this is incredibly silly! self.assertRaises( I18NError, self._compile, '<span i18n:translate="" tal:content="foo" i18n:name="foo_name"/>') def test_content_with_explicit_messageid(self): # Let's tell the user this is incredibly silly! self.assertRaises( I18NError, self._compile, '<span i18n:translate="ID" tal:content="foo" />') def test_content_with_plaintext_and_i18nname_and_i18ntranslate(self): # Let's tell the user this is incredibly silly! self.assertRaises( I18NError, self._compile, '<span i18n:translate="" i18n:name="color_name">green</span>') def test_translate_static_text_as_dynamic(self): program, macros = self._compile( '<div i18n:translate="">This is text for ' '<span tal:content="bar" i18n:name="bar_name"/>.' '</div>') self._check(program, '<div>THIS IS TEXT FOR <span>BaRvAlUe</span>.</div>\n') program, macros = self._compile( '<div i18n:translate="">This is text for ' '<span i18n:translate="" tal:content="bar" i18n:name="bar_name"/>.' '</div>') self._check(program, '<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>\n') def test_translate_static_text_as_dynamic_from_bytecode(self): program = [('version', TAL_VERSION), ('mode', 'html'), ('setPosition', (1, 0)), ('beginScope', {'i18n:translate': ''}), ('startTag', ('div', [('i18n:translate', '', 'i18n')])), ('insertTranslation', ('', [('rawtextOffset', ('This is text for ', 17)), ('setPosition', (1, 40)), ('beginScope', {'tal:content': 'bar', 'i18n:name': 'bar_name', 'i18n:translate': ''}), ('i18nVariable', ('bar_name', [('startTag', ('span', [('i18n:translate', '', 'i18n'), ('tal:content', 'bar', 'tal'), ('i18n:name', 'bar_name', 'i18n')])), ('insertTranslation', ('', [('insertText', ('$bar$', []))])), ('rawtextOffset', ('</span>', 7))], None, 0)), ('endScope', ()), ('rawtextOffset', ('.', 1))])), ('endScope', ()), ('rawtextOffset', ('</div>', 6)) ] self._check(program, '<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>\n') def test_for_correct_msgids(self): self.engine.translationDomain.clearMsgids() result = StringIO() #GChapelle: #I have the feeling the i18n:translate with the i18n:name is wrong # #program, macros = self._compile( # '<div i18n:translate="">This is text for ' # '<span i18n:translate="" tal:content="bar" ' # 'i18n:name="bar_name"/>.</div>') program, macros = self._compile( '<div i18n:translate="">This is text for ' '<span tal:content="bar" ' 'i18n:name="bar_name"/>.</div>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(1, len(msgids)) self.assertEqual('This is text for ${bar_name}.', msgids[0][0]) self.assertEqual({'bar_name': '<span>BaRvAlUe</span>'}, msgids[0][1]) self.assertEqual( '<div>THIS IS TEXT FOR <span>BaRvAlUe</span>.</div>\n', result.getvalue()) def test_for_correct_msgids_translate_name(self): self.engine.translationDomain.clearMsgids() result = StringIO() program, macros = self._compile( '<div i18n:translate="">This is text for ' '<span i18n:translate="" tal:content="bar" ' 'i18n:name="bar_name"/>.</div>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual('This is text for ${bar_name}.', msgids[1][0]) self.assertEqual({'bar_name': '<span>BARVALUE</span>'}, msgids[1][1]) self.assertEqual( '<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>\n', result.getvalue()) def test_i18ntranslate_i18nname_and_attributes(self): # Test for Issue 301: Bug with i18n:name and i18n:translate # on the same element self.engine.translationDomain.clearMsgids() result = StringIO() program, macros = self._compile( '<p i18n:translate="">' 'Some static text and a <a tal:attributes="href string:url"' ' i18n:name="link" i18n:translate="">link text</a>.</p>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual('Some static text and a ${link}.', msgids[0][0]) self.assertEqual({'link': '<a href="url">LINK TEXT</a>'}, msgids[0][1]) self.assertEqual('link text', msgids[1][0]) self.assertEqual( '<p>SOME STATIC TEXT AND A <a href="url">LINK TEXT</a>.</p>\n', result.getvalue()) def test_for_raw_msgids(self): # Test for Issue 314: i18n:translate removes line breaks from # <pre>...</pre> contents # HTML mode self.engine.translationDomain.clearMsgids() result = StringIO() program, macros = self._compile( '<div i18n:translate=""> This is text\n' ' \tfor\n div. </div>' '<pre i18n:translate=""> This is text\n' ' <b>\tfor</b>\n pre. </pre>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual(' This is text\n <b>\tfor</b>\n pre. ', msgids[0][0]) self.assertEqual('This is text for div.', msgids[1][0]) self.assertEqual( '<div>THIS IS TEXT FOR DIV.</div>' '<pre> THIS IS TEXT\n <B>\tFOR</B>\n PRE. </pre>\n', result.getvalue()) # XML mode self.engine.translationDomain.clearMsgids() result = StringIO() parser = TALParser() parser.parseString( '<?xml version="1.0"?>\n' '<pre xmlns:i18n="http://xml.zope.org/namespaces/i18n"' ' i18n:translate=""> This is text\n' ' <b>\tfor</b>\n barvalue. </pre>') program, macros = parser.getCode() self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(1, len(msgids)) self.assertEqual('This is text <b> for</b> barvalue.', msgids[0][0]) self.assertEqual( '<?xml version="1.0"?>\n' '<pre>THIS IS TEXT <B> FOR</B> BARVALUE.</pre>\n', result.getvalue()) def test_raw_msgids_and_i18ntranslate_i18nname(self): self.engine.translationDomain.clearMsgids() result = StringIO() program, macros = self._compile( '<div i18n:translate=""> This is text\n \tfor\n' '<pre i18n:name="bar" i18n:translate=""> \tbar\n </pre>.</div>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual(' \tbar\n ', msgids[0][0]) self.assertEqual('This is text for ${bar}.', msgids[1][0]) self.assertEqual({'bar': '<pre> \tBAR\n </pre>'}, msgids[1][1]) self.assertEqual( u'<div>THIS IS TEXT FOR <pre> \tBAR\n </pre>.</div>\n', result.getvalue()) def test_for_handling_unicode_vars(self): # Make sure that non-ASCII Unicode is substituted correctly. # http://collector.zope.org/Zope3-dev/264 program, macros = self._compile( "<div i18n:translate='' tal:define='bar python:unichr(0xC0)'>" "Foo <span tal:replace='bar' i18n:name='bar' /></div>") self._check(program, u"<div>FOO \u00C0</div>\n")
def setUp(self): self.engine = DummyEngine() # Make sure we'll translate the msgid not its unicode representation self.engine.setLocal('foo', self.factory('FoOvAlUe${empty}', 'default', {'empty': ''})) self.engine.setLocal('bar', 'BaRvAlUe')
def compare(self, INPUT, EXPECTED): program, _macros = self._compile(INPUT) sio = StringIO() interp = TALInterpreter(program, {}, DummyEngine(), sio, wrap=60) interp() self.assertEqual(sio.getvalue(), EXPECTED)
def setUp(self): self.engine = DummyEngine()
def setUp(self): dummy, macros = self._compile('<p metal:define-macro="M">Booh</p>') self.macro = macros['M'] self.engine = DummyEngine(macros) program, dummy = self._compile('<p metal:use-macro="M">Bah</p>') self.interpreter = TALInterpreter(program, {}, self.engine)
def __init__(self, macros=None): DummyEngine.__init__(self, macros) self.translationDomain = TestTranslations()
class I18NCornerTestCaseMessage(TestCaseBase): interpreter = None def factory(self, msgid, default=None, mapping=None, domain=None): return Message(msgid, domain=domain, default=default, mapping=mapping or {}) def setUp(self): self.engine = DummyEngine() # Make sure we'll translate the msgid not its unicode representation self.engine.setLocal( 'foo', self.factory('FoOvAlUe${empty}', 'default', {'empty': ''})) self.engine.setLocal('bar', 'BaRvAlUe') def _check(self, program, expected): result = StringIO() self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() self.assertEqual(expected, result.getvalue()) def test_simple_messageid_translate(self): # This test is mainly here to make sure our DummyEngine works # correctly. program, _macros = self._compile( '<span i18n:translate="" tal:content="foo"/>') self._check(program, '<span>FOOVALUE</span>') program, _macros = self._compile( '<span i18n:translate="" tal:replace="foo"/>') self._check(program, 'FOOVALUE') # i18n messages defined in Python are translated automatically # (no i18n:translate necessary) program, _macros = self._compile('<span tal:content="foo" />') self._check(program, '<span>FOOVALUE</span>') program, _macros = self._compile('<span tal:replace="foo" />') self._check(program, 'FOOVALUE') def test_attributes_translation(self): program, _macros = self._compile('<span tal:attributes="test bar"/>') self._check(program, '<span test="BaRvAlUe" />') program, _macros = self._compile( '<span test="bar" i18n:attributes="test"/>') self._check(program, '<span test="BAR" />') program, _macros = self._compile( '<span tal:attributes="test bar" i18n:attributes="test"/>') self._check(program, '<span test="BARVALUE" />') # i18n messages defined in Python are translated automatically # (no i18n:attributes necessary) program, _macros = self._compile('<span tal:attributes="test foo"/>') self._check(program, '<span test="FOOVALUE" />') def test_text_variable_translate(self): program, _macros = self._compile('<span tal:content="bar"/>') self._check(program, '<span>BaRvAlUe</span>') program, _macros = self._compile( '<span i18n:translate="" tal:content="bar"/>') self._check(program, '<span>BARVALUE</span>') program, _macros = self._compile( '<span i18n:translate="" tal:replace="bar"/>') self._check(program, 'BARVALUE') def test_text_translate(self): program, _macros = self._compile('<span tal:content="string:BaR"/>') self._check(program, '<span>BaR</span>') program, _macros = self._compile( '<span i18n:translate="" tal:content="string:BaR"/>') self._check(program, '<span>BAR</span>') program, _macros = self._compile( '<span i18n:translate="" tal:replace="string:BaR"/>') self._check(program, 'BAR') def test_structure_text_variable_translate(self): program, _macros = self._compile('<span tal:content="structure bar"/>') self._check(program, '<span>BaRvAlUe</span>') program, _macros = self._compile( '<span i18n:translate="" tal:content="structure bar"/>') self._check(program, '<span>BARVALUE</span>') program, _macros = self._compile( '<span i18n:translate="" tal:replace="structure bar"/>') self._check(program, 'BARVALUE') # i18n messages defined in Python are translated automatically # (no i18n:translate necessary) program, _macros = self._compile('<span tal:content="structure foo"/>') self._check(program, '<span>FOOVALUE</span>') program, _macros = self._compile('<span tal:replace="structure foo"/>') self._check(program, 'FOOVALUE') def test_structure_text_translate(self): program, _macros = self._compile( '<span tal:content="structure string:BaR"/>') self._check(program, '<span>BaR</span>') program, _macros = self._compile( '<span i18n:translate="" tal:content="structure string:BaR"/>') self._check(program, '<span>BAR</span>') program, _macros = self._compile( '<span i18n:translate="" tal:replace="structure string:BaR"/>') self._check(program, 'BAR') def test_replace_with_messageid_and_i18nname(self): program, _macros = self._compile( '<div i18n:translate="" >' '<span i18n:translate="" tal:replace="foo" i18n:name="foo_name"/>' '</div>') self._check(program, '<div>FOOVALUE</div>') def test_pythonexpr_replace_with_messageid_and_i18nname(self): program, _macros = self._compile( '<div i18n:translate="" >' '<span i18n:translate="" tal:replace="python: foo"' ' i18n:name="foo_name"/>' '</div>') self._check(program, '<div>FOOVALUE</div>') def test_structure_replace_with_messageid_and_i18nname(self): program, _macros = self._compile( '<div i18n:translate="" >' '<span i18n:translate="" tal:replace="structure foo"' ' i18n:name="foo_name"/>' '</div>') self._check(program, '<div>FOOVALUE</div>') def test_complex_replace_with_messageid_and_i18nname(self): program, _macros = self._compile( '<div i18n:translate="" >' '<em tal:omit-tag="" i18n:name="foo_name">' '<span i18n:translate="" tal:replace="foo"/>' '</em>' '</div>') self._check(program, '<div>FOOVALUE</div>') def test_content_with_messageid_and_i18nname(self): program, _macros = self._compile( '<div i18n:translate="" >' '<span i18n:translate="" tal:content="foo" i18n:name="foo_name"/>' '</div>') self._check(program, '<div><span>FOOVALUE</span></div>') def test_content_with_messageid_and_i18nname_and_i18ntranslate(self): # Let's tell the user this is incredibly silly! self.assertRaises( I18NError, self._compile, '<span i18n:translate="" tal:content="foo" i18n:name="foo_name"/>') def test_content_with_explicit_messageid(self): # Let's tell the user this is incredibly silly! self.assertRaises(I18NError, self._compile, '<span i18n:translate="ID" tal:content="foo" />') def test_content_with_plaintext_and_i18nname_and_i18ntranslate(self): # Let's tell the user this is incredibly silly! self.assertRaises( I18NError, self._compile, '<span i18n:translate="" i18n:name="color_name">green</span>') def test_translate_static_text_as_dynamic(self): program, _macros = self._compile( '<div i18n:translate="">This is text for ' '<span tal:content="bar" i18n:name="bar_name"/>.' '</div>') self._check(program, '<div>THIS IS TEXT FOR <span>BaRvAlUe</span>.</div>') program, _macros = self._compile( '<div i18n:translate="">This is text for ' '<span i18n:translate="" tal:content="bar" i18n:name="bar_name"/>.' '</div>') self._check(program, '<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>') def test_translate_static_text_as_dynamic_from_bytecode(self): program = [('version', TAL_VERSION), ('mode', 'html'), ('setPosition', (1, 0)), ('beginScope', { 'i18n:translate': '' }), ('startTag', ('div', [('i18n:translate', '', 'i18n')])), ('insertTranslation', ('', [ ('rawtextOffset', ('This is text for ', 17)), ('setPosition', (1, 40)), ('beginScope', { 'tal:content': 'bar', 'i18n:name': 'bar_name', 'i18n:translate': '' }), ('i18nVariable', ('bar_name', [ ('startTag', ('span', [('i18n:translate', '', 'i18n'), ('tal:content', 'bar', 'tal'), ('i18n:name', 'bar_name', 'i18n')])), ('insertTranslation', ('', [('insertText', ('$bar$', []))])), ('rawtextOffset', ('</span>', 7)) ], None, 0)), ('endScope', ()), ('rawtextOffset', ('.', 1)) ])), ('endScope', ()), ('rawtextOffset', ('</div>', 6))] self._check(program, '<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>') def test_for_correct_msgids(self): self.engine.translationDomain.clearMsgids() result = StringIO() #GChapelle: #I have the feeling the i18n:translate with the i18n:name is wrong # #program, macros = self._compile( # '<div i18n:translate="">This is text for ' # '<span i18n:translate="" tal:content="bar" ' # 'i18n:name="bar_name"/>.</div>') program, _macros = self._compile( '<div i18n:translate="">This is text for ' '<span tal:content="bar" ' 'i18n:name="bar_name"/>.</div>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(1, len(msgids)) self.assertEqual('This is text for ${bar_name}.', msgids[0][0]) self.assertEqual({'bar_name': '<span>BaRvAlUe</span>'}, msgids[0][1]) self.assertEqual('<div>THIS IS TEXT FOR <span>BaRvAlUe</span>.</div>', result.getvalue()) def test_for_correct_msgids_translate_name(self): self.engine.translationDomain.clearMsgids() result = StringIO() program, _macros = self._compile( '<div i18n:translate="">This is text for ' '<span i18n:translate="" tal:content="bar" ' 'i18n:name="bar_name"/>.</div>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual('This is text for ${bar_name}.', msgids[1][0]) self.assertEqual({'bar_name': '<span>BARVALUE</span>'}, msgids[1][1]) self.assertEqual('<div>THIS IS TEXT FOR <span>BARVALUE</span>.</div>', result.getvalue()) def test_i18ntranslate_i18nname_and_attributes(self): # Test for Issue 301: Bug with i18n:name and i18n:translate # on the same element self.engine.translationDomain.clearMsgids() result = StringIO() program, _macros = self._compile( '<p i18n:translate="">' 'Some static text and a <a tal:attributes="href string:url"' ' i18n:name="link" i18n:translate="">link text</a>.</p>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual('Some static text and a ${link}.', msgids[0][0]) self.assertEqual({'link': '<a href="url">LINK TEXT</a>'}, msgids[0][1]) self.assertEqual('link text', msgids[1][0]) self.assertEqual( '<p>SOME STATIC TEXT AND A <a href="url">LINK TEXT</a>.</p>', result.getvalue()) def test_for_raw_msgids(self): # Test for Issue 314: i18n:translate removes line breaks from # <pre>...</pre> contents # HTML mode self.engine.translationDomain.clearMsgids() result = StringIO() program, _macros = self._compile( '<div i18n:translate=""> This is text\n' ' \tfor\n div. </div>' '<pre i18n:translate=""> This is text\n' ' <b>\tfor</b>\n pre. </pre>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual(' This is text\n <b>\tfor</b>\n pre. ', msgids[0][0]) self.assertEqual('This is text for div.', msgids[1][0]) self.assertEqual( '<div>THIS IS TEXT FOR DIV.</div>' '<pre> THIS IS TEXT\n <B>\tFOR</B>\n PRE. </pre>', result.getvalue()) # XML mode self.engine.translationDomain.clearMsgids() result = StringIO() parser = TALParser() parser.parseString( '<?xml version="1.0"?>\n' '<pre xmlns:i18n="http://xml.zope.org/namespaces/i18n"' ' i18n:translate=""> This is text\n' ' <b>\tfor</b>\n barvalue. </pre>') program, _macros = parser.getCode() self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(1, len(msgids)) self.assertEqual('This is text <b> for</b> barvalue.', msgids[0][0]) self.assertEqual( '<?xml version="1.0"?>\n' '<pre>THIS IS TEXT <B> FOR</B> BARVALUE.</pre>', result.getvalue()) def test_raw_msgids_and_i18ntranslate_i18nname(self): self.engine.translationDomain.clearMsgids() result = StringIO() program, _macros = self._compile( '<div i18n:translate=""> This is text\n \tfor\n' '<pre i18n:name="bar" i18n:translate=""> \tbar\n </pre>.</div>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = self.engine.translationDomain.getMsgids('default') msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual(' \tbar\n ', msgids[0][0]) self.assertEqual('This is text for ${bar}.', msgids[1][0]) self.assertEqual({'bar': '<pre> \tBAR\n </pre>'}, msgids[1][1]) self.assertEqual( (u'<div>THIS IS TEXT FOR <pre> \tBAR\n </pre>.</div>'), result.getvalue()) def test_for_handling_unicode_vars(self): # Make sure that non-ASCII Unicode is substituted correctly. # http://collector.zope.org/Zope3-dev/264 program, _macros = self._compile( r'''<div i18n:translate='' tal:define='bar python:u"\u00C0"'>''' r'''Foo <span tal:replace='bar' i18n:name='bar' /></div>''') self._check(program, (u"<div>FOO \u00C0</div>"))
def setUp(self): self.engine = DummyEngine() # Make sure we'll translate the msgid not its unicode representation self.engine.setLocal( 'foo', self.factory('FoOvAlUe${empty}', 'default', {'empty': ''})) self.engine.setLocal('bar', 'BaRvAlUe')