[Checkins] SVN: z3c.pt/trunk/ Added language negotiation; changed method signature for _pt_get_context in order to avoid symbol clashes.

Malthe Borch mborch at gmail.com
Tue Nov 18 06:49:04 EST 2008


Log message for revision 93078:
  Added language negotiation; changed method signature for _pt_get_context in order to avoid symbol clashes.

Changed:
  U   z3c.pt/trunk/CHANGES.txt
  U   z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_z3c.pt
  U   z3c.pt/trunk/benchmark/benchmark/tests.py
  U   z3c.pt/trunk/src/z3c/pt/pagetemplate.py

-=-
Modified: z3c.pt/trunk/CHANGES.txt
===================================================================
--- z3c.pt/trunk/CHANGES.txt	2008-11-18 11:16:25 UTC (rev 93077)
+++ z3c.pt/trunk/CHANGES.txt	2008-11-18 11:49:04 UTC (rev 93078)
@@ -4,6 +4,8 @@
 Head
 ~~~~
 
+- Added language negotiation. [malthe]
+
 - Simplified template class inheritance. [malthe]
 
 - Added support for the question-mark operator in path-expressions. [malthe]

Modified: z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_z3c.pt
===================================================================
--- z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_z3c.pt	2008-11-18 11:16:25 UTC (rev 93077)
+++ z3c.pt/trunk/benchmark/benchmark/input/bigtable_i18n_z3c.pt	2008-11-18 11:49:04 UTC (rev 93078)
@@ -2,12 +2,12 @@
 xmlns:i18n="http://xml.zope.org/namespaces/i18n"
 xmlns:tal="http://xml.zope.org/namespaces/tal"
 i18n:domain="domain">
-<tr tal:repeat="row table">
+<tr tal:repeat="row python: options['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" />
+<td tal:repeat="c python: row.values()">
+<span tal:define="d python: c + 1"
+tal:attributes="class python: 'column-' + str(d)"
+tal:content="python: d" i18n:attributes="class" />
 <span i18n:translate="">Default</span>
 </td>
 </tr>

Modified: z3c.pt/trunk/benchmark/benchmark/tests.py
===================================================================
--- z3c.pt/trunk/benchmark/benchmark/tests.py	2008-11-18 11:16:25 UTC (rev 93077)
+++ z3c.pt/trunk/benchmark/benchmark/tests.py	2008-11-18 11:49:04 UTC (rev 93078)
@@ -65,7 +65,7 @@
 
 class BenchmarkTestCase(BaseTestCase):
 
-    helloworld_z3c = pagetemplate.ZopePageTemplate("""\
+    helloworld_z3c = pagetemplate.PageTemplate("""\
     <div xmlns="http://www.w3.org/1999/xhtml">
     Hello World!
     </div>""")
@@ -88,11 +88,11 @@
     </tr>
     </table>""")
 
-    bigtable_path_z3c = pagetemplate.ZopePageTemplate("""\
+    bigtable_path_z3c = pagetemplate.PageTemplate("""\
     <table xmlns="http://www.w3.org/1999/xhtml"
     xmlns:tal="http://xml.zope.org/namespaces/tal"
     tal:default-expression="path">
-    <tr tal:repeat="row table">
+    <tr tal:repeat="row options/table">
     <td tal:repeat="c row/values">
     <span tal:define="d python: c + 1"
     tal:attributes="class string:column-${d}"
@@ -231,7 +231,7 @@
     def testBigTablePathFile(self):
         table = self.table
 
-        z3cfile = pagetemplate.ZopePageTemplateFile(
+        z3cfile = pagetemplate.PageTemplateFile(
             self._testfile('bigtable_path_z3c.pt'))
 
         zopefile = zope.pagetemplate.pagetemplatefile.PageTemplateFile(
@@ -285,11 +285,9 @@
         catalog = SimpleTranslationDomain('domain')
         zope.component.provideUtility(catalog, ITranslationDomain, 'domain')
         self.files = os.path.abspath(os.path.join(__file__, '..', 'input'))
-        self.disable = config.DISABLE_I18N
 
     def tearDown(self):
         BaseTestCase.tearDown(self)
-        config.DISABLE_I18N = self.disable
 
     def _testfile(self, name):
         return os.path.join(self.files, name)
@@ -298,7 +296,7 @@
     def testI18N(self):
         table = self.table
 
-        z3cfile = zpt.template.PageTemplateFile(
+        z3cfile = pagetemplate.PageTemplateFile(
             self._testfile('bigtable_i18n_z3c.pt'))
 
         zopefile = zope.pagetemplate.pagetemplatefile.PageTemplateFile(
@@ -309,7 +307,7 @@
 
         assert config.SYMBOLS.i18n_context=='_i18n_context'
         
-        t_z3c = timing(z3cfile, table=table, _i18n_context=self.env)
+        t_z3c = timing(z3cfile, table=table, target_language='klingon')
         t_zope = timing(zopefile, table=table, env=self.env)
 
         print "z3c.pt:            %.2f" % t_z3c
@@ -320,7 +318,7 @@
     def testDisabledI18N(self):
         table = self.table
 
-        z3cfile = zpt.template.PageTemplateFile(
+        z3cfile = pagetemplate.PageTemplateFile(
             self._testfile('bigtable_i18n_z3c.pt'))
 
         zopefile = zope.pagetemplate.pagetemplatefile.PageTemplateFile(
@@ -328,10 +326,7 @@
 
         zopefile.pt_getEngineContext = _pt_getEngineContext
 
-        # Let's disable i18n for this test
-        config.DISABLE_I18N = True
-
-        t_z3c = timing(z3cfile, table=table, _context=self.env)
+        t_z3c = timing(z3cfile, table=table)
         t_zope = timing(zopefile, table=table, env=self.env)
 
         print "z3c.pt:            %.2f" % t_z3c

Modified: z3c.pt/trunk/src/z3c/pt/pagetemplate.py
===================================================================
--- z3c.pt/trunk/src/z3c/pt/pagetemplate.py	2008-11-18 11:16:25 UTC (rev 93077)
+++ z3c.pt/trunk/src/z3c/pt/pagetemplate.py	2008-11-18 11:49:04 UTC (rev 93078)
@@ -4,12 +4,23 @@
 import chameleon.zpt.template
 import chameleon.zpt.language
 
+from zope import i18n
+
 class BaseTemplate(chameleon.zpt.template.PageTemplate):
     default_parser = chameleon.zpt.language.Parser(default_expression='path')
     
     def bind(self, ob, request=None, macro=None, global_scope=True):
-        def render(**kwargs):
-            context = self._pt_get_context(ob, request, **kwargs)
+        def render(target_language=None, **kwargs):
+            context = self._pt_get_context(ob, request, kwargs)
+
+            if target_language is None:
+                try:
+                    target_language = i18n.negotiate(
+                        request or context.get('request'))
+                except:
+                    target_language = None
+                    
+            context['target_language'] = target_language
             
             if macro is None:
                 return self.render(**context)
@@ -23,7 +34,7 @@
         bound_pt = self.bind(_ob)
         return bound_pt(**kwargs)
 
-    def _pt_get_context(self, instance, request, **kwargs):
+    def _pt_get_context(self, instance, request, kwargs):
         return dict(
             options=kwargs,
             request=request,
@@ -85,7 +96,7 @@
     dictionary. Note that the default expression type for this class
     is 'path' (standard Zope traversal)."""
 
-    def _pt_get_context(self, view, request, **kwargs):
+    def _pt_get_context(self, view, request, kwargs):
         return dict(
             view=view,
             context=view.context,



More information about the Checkins mailing list