[Checkins] SVN: megrok.jinja/trunk/src/megrok/jinja/ Replace the full Extensions classes with just the contextfunctions needed to resolve translations and content providers.

Santiago Videla santiago.videla at gmail.com
Thu Apr 30 10:08:54 EDT 2009


Log message for revision 99610:
  Replace the full Extensions classes with just the contextfunctions needed to resolve translations and content providers.
  
  

Changed:
  U   megrok.jinja/trunk/src/megrok/jinja/extensions.py
  U   megrok.jinja/trunk/src/megrok/jinja/factory.py

-=-
Modified: megrok.jinja/trunk/src/megrok/jinja/extensions.py
===================================================================
--- megrok.jinja/trunk/src/megrok/jinja/extensions.py	2009-04-30 13:58:40 UTC (rev 99609)
+++ megrok.jinja/trunk/src/megrok/jinja/extensions.py	2009-04-30 14:08:54 UTC (rev 99610)
@@ -12,14 +12,11 @@
 #
 ##############################################################################
 
-from jinja2 import nodes
-from jinja2.ext import Extension, InternationalizationExtension
 from jinja2.utils import contextfunction
 
 from zope.component import getUtility, getMultiAdapter
 from zope.i18n.interfaces import ITranslationDomain
 from zope.contentprovider.interfaces import IContentProvider
-from zope.cachedescriptors import method
 
 class DomainNotDefined(Exception):
     def __str__(self):
@@ -34,101 +31,28 @@
     return context.resolve('gettext')(context, msgid, domain, mapping, ctx,
                                       target_language, default)
 
-class i18nExtension(InternationalizationExtension):
-    """
-    Jinja2 extension based on the `InternationalizationExtension`
-    extension from jinja2.ext.
-    """
-    # We use the same tag that InternationalizationExtension
-    # in order to be able to reuse the parser method
-    tags = set(['trans'])
+ at contextfunction
+def translator(context, msg, domain=None, mapping=None, ctx=None,
+               target_language=None, default=None):
 
-    def __init__(self, environment):
-        Extension.__init__(self, environment)
-        environment.globals['_'] = _translator_alias
-        environment.extend(
-            install_gettext_translations=self._install,
-            install_null_translations=self._install_null,
-            uninstall_gettext_translations=self._uninstall,
-            extract_translations=self._extract
-        )
+    ctx = ctx or context.resolve('view').request
+    domain = domain or context.resolve('i18n_domain')
+    if not domain:
+        raise DomainNotDefined
 
-    def _install(self):
-        """
-        We override this method to use a different translator
-        allowing dynamic domains using zope.i18n machinery.
-        """
-        self.environment.globals.update(gettext=self.translator)
+    utility = getUtility(ITranslationDomain, domain)
+    return utility.translate(msg,
+                             mapping=mapping,
+                             context=ctx,
+                             target_language=target_language,
+                             default=default)
 
-    @method.cachedIn('_cache')
-    def trans_domain(self, domain):
-        """
-        Domains names are cached in order to avoid
-        the getUtility call for each translation in the template.
-        """
-        return getUtility(ITranslationDomain, domain)
+ at contextfunction
+def _get_content_provider(context, name):
+    view = context.resolve('view')
 
-    @contextfunction
-    def translator(self, context, msg, domain=None, mapping=None, ctx=None,
-                   target_language=None, default=None):
-
-        ctx = ctx or context.resolve('view').request
-        domain = domain or context.resolve('i18n_domain')
-        if not domain:
-            raise DomainNotDefined
-
-        return self.trans_domain(domain).translate(msg,
-                                                   mapping=mapping,
-                                                   context=ctx,
-                                                   target_language=target_language,
-                                                   default=default)
-
-    def _make_node(self, singular, plural, variables, plural_expr):
-        """
-        This method it's called from the `parser` defined in
-        `jinja2.ext.InternationalizationExtension` class.
-
-        We need to override this method to handle the pluralize tag.
-        """
-
-        # singular only:
-        if plural_expr is None:
-            gettext = nodes.Name('gettext', 'load')
-            node = nodes.Call(gettext, [nodes.Const(singular)],
-                              [], None, None)
-
-        # singular and plural
-        else:
-            #TODO: implement {%pluralize%} tag.
-            raise NotImplementedError("{% pluralize %} was not implemented yet")
-
-        # mark the return value as safe if we are in an
-        # environment with autoescaping turned on
-        if self.environment.autoescape:
-            node = nodes.MarkSafe(node)
-
-        if variables:
-            node = nodes.Mod(node, variables)
-        return nodes.Output([node])
-
-class ContentProviderExtension(Extension):
-    """
-    Jinja2 extension to support the use of viewlets (content
-    providers).
-
-    It doesn't define any `tag`, just set the `provider` function name
-    in the `Environment.globals`
-    """
-    def __init__(self, environment):
-        Extension.__init__(self, environment)
-        environment.globals['provider'] = self._get_content_provider
-
-    @contextfunction
-    def _get_content_provider(self, context, name):
-        view = context.resolve('view')
-
-        provider = getMultiAdapter((view.context, view.request, view),
-                                    IContentProvider,
-                                    name=name)
-        provider.update()
-        return provider.render()
+    provider = getMultiAdapter((view.context, view.request, view),
+                                IContentProvider,
+                                name=name)
+    provider.update()
+    return provider.render()

Modified: megrok.jinja/trunk/src/megrok/jinja/factory.py
===================================================================
--- megrok.jinja/trunk/src/megrok/jinja/factory.py	2009-04-30 13:58:40 UTC (rev 99609)
+++ megrok.jinja/trunk/src/megrok/jinja/factory.py	2009-04-30 14:08:54 UTC (rev 99610)
@@ -13,7 +13,7 @@
 ##############################################################################
 
 from jinja2 import Environment, FileSystemLoader
-from extensions import i18nExtension, ContentProviderExtension
+from extensions import translator, _translator_alias, _get_content_provider
 
 import yaml, simplejson, os
 
@@ -27,12 +27,15 @@
 # absolute path templates.
 # By default, auto_reload = True, for production system
 # should be set to False for higher performance
-env = Environment(extensions=[i18nExtension, ContentProviderExtension],
+env = Environment(extensions=['jinja2.ext.i18n'],
                   loader=FileSystemLoader('/'))
 
-env.install_gettext_translations()
+#Just set the functions used to resolve translations and
+#contents providers instead of full Extension classes
+env.globals['_'] = _translator_alias
+env.globals.update(gettext=translator)
+env.globals['provider'] = _get_content_provider
 
-
 class JTemplate(object):
     """
     Base class for JinjaTemplate and JsonTemplate



More information about the Checkins mailing list