[Checkins] SVN: z3c.pt/trunk/ Extended the i18n handling to respect the passed in translation context to the template. Usually this is the request, which is passed on under the internal name of `_context` into the render functions. After extending the i18n tests to include a negotiator and message catalog the improvement is only at 4.5 anymore, as most of the time is spent inside the i18n machinery.

Hanno Schlichting plone at hannosch.info
Sun Jul 6 17:12:38 EDT 2008


Log message for revision 88077:
  Extended the i18n handling to respect the passed in translation context to the template. Usually this is the request, which is passed on under the internal name of `_context` into the render functions. After extending the i18n tests to include a negotiator and message catalog the improvement is only at 4.5 anymore, as most of the time is spent inside the i18n machinery.
  

Changed:
  U   z3c.pt/trunk/CHANGES.txt
  A   z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_z3c.pt
  A   z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_zope.pt
  U   z3c.pt/trunk/benchmark/benchmark/tests.py
  U   z3c.pt/trunk/buildout.cfg
  U   z3c.pt/trunk/src/z3c/pt/generation.py
  U   z3c.pt/trunk/src/z3c/pt/pagetemplate.py
  U   z3c.pt/trunk/src/z3c/pt/texttemplate.py
  U   z3c.pt/trunk/src/z3c/pt/translation.py

-=-
Modified: z3c.pt/trunk/CHANGES.txt
===================================================================
--- z3c.pt/trunk/CHANGES.txt	2008-07-06 17:42:54 UTC (rev 88076)
+++ z3c.pt/trunk/CHANGES.txt	2008-07-06 21:12:36 UTC (rev 88077)
@@ -4,6 +4,12 @@
 Version 0.8.x
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+- Extended the i18n handling to respect the passed in translation context to
+  the template. Usually this is the request, which is passed on under the
+  internal name of `_context` into the render functions. After extending the
+  i18n tests to include a negotiator and message catalog the improvement is
+  only at 4.5 anymore, as most of the time is spent inside the i18n machinery.
+
 - Added persistent file cache functionality. If the environment variable is
   set, each file system based template will add a directory to the cache
   (currently a SHA-1 of the file's absolute path is used as the folder name)

Added: z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_z3c.pt
===================================================================
--- z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_z3c.pt	                        (rev 0)
+++ z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_z3c.pt	2008-07-06 21:12:36 UTC (rev 88077)
@@ -0,0 +1,14 @@
+<table xmlns="http://www.w3.org/1999/xhtml"
+xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+xmlns:tal="http://xml.zope.org/namespaces/tal"
+i18n:domain="domain">
+<tr tal:repeat="row table">
+<span i18n:translate="label_default">Default</span>
+<td tal:repeat="c row.values()">
+<span tal:define="d c + 1"
+tal:attributes="class 'column-' + str(d)"
+tal:content="d" i18n:attributes="class" />
+<span i18n:translate="">Default</span>
+</td>
+</tr>
+</table>


Property changes on: z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_z3c.pt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_zope.pt
===================================================================
--- z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_zope.pt	                        (rev 0)
+++ z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_zope.pt	2008-07-06 21:12:36 UTC (rev 88077)
@@ -0,0 +1,14 @@
+<table xmlns="http://www.w3.org/1999/xhtml"
+xmlns:i18n="http://xml.zope.org/namespaces/i18n"
+xmlns:tal="http://xml.zope.org/namespaces/tal"
+i18n:domain="domain">
+<tr tal:repeat="row python: options['table']">
+<span i18n:translate="label_default">Default</span>
+<td tal:repeat="c python: row.values()">
+<span tal:define="d python: c + 1"
+tal:attributes="class python:'column-'+str(d)"
+tal:content="d" i18n:attributes="class" />
+<span i18n:translate="">Default</span>
+</td>
+</tr>
+</table>


Property changes on: z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_zope.pt
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: z3c.pt/trunk/benchmark/benchmark/tests.py
===================================================================
--- z3c.pt/trunk/benchmark/benchmark/tests.py	2008-07-06 17:42:54 UTC (rev 88076)
+++ z3c.pt/trunk/benchmark/benchmark/tests.py	2008-07-06 21:12:36 UTC (rev 88077)
@@ -217,13 +217,20 @@
 # Use a custom context to add real i18n lookup
 
 from zope.i18n import translate
+from zope.i18n.interfaces import INegotiator
+from zope.i18n.interfaces import ITranslationDomain
+from zope.i18n.negotiator import Negotiator
+from zope.i18n.simpletranslationdomain import SimpleTranslationDomain
+from zope.i18n.tests.test_negotiator import Env
 from zope.tales.tales import Context
 
 class ZopeI18NContext(Context):
 
-    def translate(self, msgid, domain=None, mapping=None, default=None):
+    def translate(self, msgid, domain=None, context=None,
+                  mapping=None, default=None):
+        context = self.vars['options']['env']
         return translate(msgid, domain, mapping,
-                         context=dict(), default=default)
+                         context=context, default=default)
 
 def _getContext(self, contexts=None, **kwcontexts):
     if contexts is not None:
@@ -241,48 +248,33 @@
 
 class I18NBenchmarkTestCase(BaseTestCase):
 
-    bigtable_i18n_z3c = z3c.pt.PageTemplate("""\
-    <table xmlns="http://www.w3.org/1999/xhtml"
-    xmlns:i18n="http://xml.zope.org/namespaces/i18n"
-    xmlns:tal="http://xml.zope.org/namespaces/tal"
-    i18n:domain="domain">
-    <tr tal:repeat="row table">
-    <span i18n:translate="label_default">Default</span>
-    <td tal:repeat="c row.values()">
-    <span tal:define="d c + 1"
-    tal:attributes="class 'column-' + str(d)"
-    tal:content="d" i18n:attributes="class" />
-    <span i18n:translate="">Default</span>
-    </td>
-    </tr>
-    </table>""")
+    def setUp(self):
+        BaseTestCase.setUp(self)
+        self.env = Env(('da', 'en', 'fr', 'no'))
+        zope.component.provideUtility(Negotiator(), INegotiator)
+        catalog = SimpleTranslationDomain('domain')
+        zope.component.provideUtility(catalog, ITranslationDomain, 'domain')
 
-    bigtable_i18n_zope = zope.pagetemplate.pagetemplate.PageTemplate()
-    # In order to have a fair comparision, we need real zope.i18n handling
-    bigtable_i18n_zope.pt_getEngineContext = _pt_getEngineContext
-    bigtable_i18n_zope.pt_edit("""\
-    <table xmlns="http://www.w3.org/1999/xhtml"
-    xmlns:i18n="http://xml.zope.org/namespaces/i18n"
-    xmlns:tal="http://xml.zope.org/namespaces/tal"
-    i18n:domain="domain">
-    <tr tal:repeat="row python: options['table']">
-    <span i18n:translate="label_default">Default</span>
-    <td tal:repeat="c python: row.values()">
-    <span tal:define="d python: c + 1"
-    tal:attributes="class python:'column-'+str(d)"
-    tal:content="d" i18n:attributes="class" />
-    <span i18n:translate="">Default</span>
-    </td>
-    </tr>
-    </table>""", 'text/xhtml')
-
     @benchmark(u"Internationalization")
     def testI18N(self):
         table = self.table
 
-        t_z3c = timing(self.bigtable_i18n_z3c, table=table)
-        t_zope = timing(self.bigtable_i18n_zope, table=table)
+        files = os.path.abspath(os.path.join(__file__, '..', 'input'))
+        def testfile(name):
+            return os.path.join(files, name)
 
+        z3cfile = z3c.pt.PageTemplateFile(
+            testfile('bigtable_i18n_z3c.pt'))
+
+        zopefile = zope.pagetemplate.pagetemplatefile.PageTemplateFile(
+            testfile('bigtable_i18n_zope.pt'))
+
+        # In order to have a fair comparision, we need real zope.i18n handling
+        zopefile.pt_getEngineContext = _pt_getEngineContext
+
+        t_z3c = timing(z3cfile, table=table, _context=self.env)
+        t_zope = timing(zopefile, table=table, env=self.env)
+
         print "z3c.pt:            %.2f" % t_z3c
         print "zope.pagetemplate: %.2f" % t_zope
         print "                   %.2fX" % (t_zope/t_z3c)

Modified: z3c.pt/trunk/buildout.cfg
===================================================================
--- z3c.pt/trunk/buildout.cfg	2008-07-06 17:42:54 UTC (rev 88076)
+++ z3c.pt/trunk/buildout.cfg	2008-07-06 21:12:36 UTC (rev 88077)
@@ -26,3 +26,4 @@
 [benchmark-environment]
 Z3C_PT_DEBUG = False
 Z3C_PT_FILECACHE = ${buildout:directory}/cache
+zope_i18n_allowed_languages = en, da, de

Modified: z3c.pt/trunk/src/z3c/pt/generation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/generation.py	2008-07-06 17:42:54 UTC (rev 88076)
+++ z3c.pt/trunk/src/z3c/pt/generation.py	2008-07-06 21:12:36 UTC (rev 88077)
@@ -49,11 +49,17 @@
 
     def __call__(self):
         # prepare template arguments
-        args = ', '.join(self.params)
-        if args: args += ', '
+        args = self.params
+        # We need to ensure we have _context for the i18n handling in the
+        # arguments. The default template implementations pass this in
+        # explicitly.
+        if '_context' not in args:
+            args = args + ('_context=None', )
+        args = ', '.join(args)
+        if args:
+            args += ', '
 
         code = self.stream.getvalue()
-
         return wrapper % (args, code), {'generation': z3c.pt.generation}
 
 class BufferIO(list):

Modified: z3c.pt/trunk/src/z3c/pt/pagetemplate.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/pagetemplate.py	2008-07-06 17:42:54 UTC (rev 88076)
+++ z3c.pt/trunk/src/z3c/pt/pagetemplate.py	2008-07-06 21:12:36 UTC (rev 88077)
@@ -24,6 +24,7 @@
             return self.template.render(view=view,
                                         context=view.context,
                                         request=view.request,
+                                        _context=view.request,
                                         options=kwargs)
         return template        
     

Modified: z3c.pt/trunk/src/z3c/pt/texttemplate.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/texttemplate.py	2008-07-06 17:42:54 UTC (rev 88076)
+++ z3c.pt/trunk/src/z3c/pt/texttemplate.py	2008-07-06 21:12:36 UTC (rev 88077)
@@ -21,6 +21,7 @@
             return self.template.render(view=view,
                                         context=view.context,
                                         request=view.request,
+                                        _context=view.request,
                                         options=kwargs)
         return template        
     

Modified: z3c.pt/trunk/src/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/translation.py	2008-07-06 17:42:54 UTC (rev 88076)
+++ z3c.pt/trunk/src/z3c/pt/translation.py	2008-07-06 21:12:36 UTC (rev 88077)
@@ -441,9 +441,8 @@
     return translate_etree(xml, *args, **kwargs)
     
 def _translate(value, mapping=None, default=None):
-    format = ("_translate(%s, domain=_domain, mapping=%s, "
+    format = ("_translate(%s, domain=_domain, mapping=%s, context=_context, "
               "target_language=_target_language, default=%s)")
-    
     return types.value(format % (value, mapping, default))
 
 def _not(value):



More information about the Checkins mailing list