Skip to content
Myr

Tip is now 2.5. Started work on newstyle gettext translations.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 7db9872 · Browse files · 5 files+88 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+88 added−18 removed

  • other:+88 added−18 removed

5 files+88 added−18 removed

modified:jinja2/compiler.py+5 added
1580 unmodified lines
1581 self.write('Markup(')1582 self.visit(node.expr, frame)1583 self.write(')')15841585 def visit_MarkSafeIfAutoescape(self, node, frame):1586 self.write('(context.eval_ctx.autoescape and Markup or identity)(')1587 self.visit(node.expr, frame)1588 self.write(')')15891590 def visit_EnvironmentAttribute(self, node, frame):1591 self.write('environment.' + node.name)15921593 def visit_ExtensionAttribute(self, node, frame):
45 unmodified lines
modified:jinja2/ext.py+57 added-14 removed
121 unmodified lines
122 dyn_args, dyn_kwargs, lineno=lineno)123124125@contextfunction126def _gettext_alias(context, string):127 return context.resolve('gettext')(string)126def _gettext_alias(__context, *args, **kwargs):127 return __context.resolve('gettext')(*args, **kwargs)128129130def _make_new_gettext(func):131 @contextfunction132 def gettext(__context, __string, **variables):133 rv = func(__string)134 if __context.eval_ctx.autoescape:135 rv = Markup(rv)136 return rv % variables137 return gettext138139140def _make_new_ngettext(func):141 @contextfunction142 def ngettext(__context, __singular, __plural, num, **variables):143 variables.setdefault('num', num)144 rv = func(__singular, __plural, num)145 if __context.eval_ctx.autoescape:146 rv = Markup(rv)147 return rv % variables148 return ngettext149150151class InternationalizationExtension(Extension):152 """This extension adds gettext support to Jinja2."""153 tags = set(['trans'])154
9 unmodified lines
164 environment.globals['_'] = _gettext_alias165 environment.extend(166 install_gettext_translations=self._install,167 install_null_translations=self._install_null,168 install_gettext_callables=self._install_callables,169 uninstall_gettext_translations=self._uninstall,148 extract_translations=self._extract170 extract_translations=self._extract,171 newstyle_gettext=False172 )173151 def _install(self, translations):174 def _install(self, translations, newstyle=None):175 gettext = getattr(translations, 'ugettext', None)176 if gettext is None:177 gettext = translations.gettext178 ngettext = getattr(translations, 'ungettext', None)179 if ngettext is None:180 ngettext = translations.ngettext158 self.environment.globals.update(gettext=gettext, ngettext=ngettext)181 self._install_callables(gettext, ngettext, newstyle)182160 def _install_null(self):183 def _install_null(self, newstyle=None):184 self._install_callables(185 lambda x: x,186 lambda s, p, n: (n != 1 and (p,) or (s,))[0],187 newstyle188 )189190 def _install_callables(self, gettext, ngettext, newstyle=None):191 if newstyle is not None:192 self.environment.newstyle_gettext = newstyle193 if self.environment.newstyle_gettext:194 gettext = _make_new_gettext(gettext)195 ngettext = _make_new_ngettext(ngettext)196 self.environment.globals.update(162 gettext=lambda x: x,163 ngettext=lambda s, p, n: (n != 1 and (p,) or (s,))[0]197 gettext=gettext,198 ngettext=ngettext199 )200201 def _uninstall(self, translations):202 for key in 'gettext', 'ngettext':
141 unmodified lines
344 nodes.Const(plural),345 plural_expr346 ], [], None, None)347313 # mark the return value as safe if we are in an314 # environment with autoescaping turned on315 if self.environment.autoescape:316 node = nodes.MarkSafe(node)348 # in case newstyle gettext is used, the method is powerful349 # enough to handle the variable expansion and autoescape350 # handling itself351 if self.environment.newstyle_gettext:352 if variables is None:353 variables = nodes.Dict([])354 node.kwargs = variables355356 # otherwise do that here357 else:358 # mark the return value as safe if we are in an359 # environment with autoescaping turned on360 node = nodes.MarkSafeIfAutoescape(node)318 if variables:319 node = nodes.Mod(node, variables)361 if variables:362 node = nodes.Mod(node, variables)363 return nodes.Output([node])364365366class ExprStmtExtension(Extension):
230 unmodified lines
modified:jinja2/nodes.py+16 added
827 unmodified lines
828 eval_ctx = get_eval_context(self, eval_ctx)829 return Markup(self.expr.as_const(eval_ctx))830831832class MarkSafeIfAutoescape(Expr):833 """Mark the wrapped expression as safe (wrap it as `Markup`) but834 only if autoescaping is active.835836 .. versionadded:: 2.5837 """838 fields = ('expr',)839840 def as_const(self, eval_ctx=None):841 eval_ctx = get_eval_context(self, eval_ctx)842 expr = self.expr.as_const(eval_ctx)843 if eval_ctx.autoescape:844 return Markup(expr)845 return expr846847848class ContextReference(Expr):849 """Returns the current template context. It can be used like a850 :class:`Name` node, with a ``'load'`` ctx and will return the851 current :class:`~jinja2.runtime.Context` object.
45 unmodified lines
modified:jinja2/runtime.py+4 added-1 removed
18 unmodified lines
1920# these variables are exported to the template runtime21__all__ = ['LoopContext', 'TemplateReference', 'Macro', 'Markup',22 'TemplateRuntimeError', 'missing', 'concat', 'escape',23 'markup_join', 'unicode_join', 'to_string',23 'markup_join', 'unicode_join', 'to_string', 'identity',24 'TemplateNotFound']2526#: the name of the function that is used to convert something into27#: a string. 2to3 will adopt that automatically and the generated28#: code can take advantage of it.29to_string = unicode3031#: the identity function. Useful for certain things in the environment32identity = lambda x: x333435def markup_join(seq):36 """Concatenation that escapes if necessary and converts to unicode."""37 buf = []38 iterator = imap(soft_unicode, seq)
497 unmodified lines
modified:CHANGES+6 added-3 removed
1Jinja2 Changelog2================34Version 2.4.25-------------6(bugfix release, release date to be announced)4Version 2.55-----------6(codename Incoherence, relased on May 29th 2010)78- improved the sort filter (should have worked like this for a9 long time) by adding support for case insensitive searches.10- fixed a bug for getattribute constant folding.11- support for newstyle gettext translations which result in a12 nicer in-template user interface and more consistent13 catalogs.1415Version 2.4.116-------------17(bugfix release, released on April 20th 2010)
165 unmodified lines