Skip to content
Myr

Added a small improvement for the code generation of newstyle gettext

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

calls. --HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent a4c7843 · Browse files · 2 files+38 added−11 removed
This commit

State

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

Diff stats

2 files+38 added−11 removed

  • other:+38 added−11 removed

2 files+38 added−11 removed

modified:jinja2/testsuite/ext.py+24 added-6 removed
36 unmodified lines
37 'child.html': '{% extends "master.html" %}{% block body %}'38 '{% trans %}watch out{% endtrans %}{% endblock %}',39 'plural.html': '{% trans user_count %}One user online{% pluralize %}'40 '{{ user_count }} users online{% endtrans %}',41 'stringformat.html': '{{ _("User: %(num)d")|format(num=user_count) }}'41 'stringformat.html': '{{ _("User: %(num)s")|format(num=user_count) }}'42}4344newstyle_i18n_templates = {45 'master.html': '<title>{{ page_title|default(_("missing")) }}</title>'
1 unmodified line
47 'child.html': '{% extends "master.html" %}{% block body %}'48 '{% trans %}watch out{% endtrans %}{% endblock %}',49 'plural.html': '{% trans user_count %}One user online{% pluralize %}'50 '{{ user_count }} users online{% endtrans %}',51 'stringformat.html': '{{ _("User: %(num)d", num=user_count) }}',52 'ngettext.html': '{{ ngettext("%(num)d apple", "%(num)d apples", apples) }}'51 'stringformat.html': '{{ _("User: %(num)s", num=user_count) }}',52 'ngettext.html': '{{ ngettext("%(num)s apple", "%(num)s apples", apples) }}',53 'ngettext_long.html': '{% trans num=apples %}{{ num }} apple{% pluralize %}'54 '{{ num }} apples{% endtrans %}'55}565758languages = {
1 unmodified line
60 'missing': u'fehlend',61 'watch out': u'pass auf',62 'One user online': u'Ein Benutzer online',63 '%(user_count)s users online': u'%(user_count)s Benutzer online',62 'User: %(num)d': u'Benutzer: %(num)d',63 '%(num)d apple': u'%(num)d Apfel',64 '%(num)d apples': u'%(num)d Äpfel'64 'User: %(num)s': u'Benutzer: %(num)s',65 '%(num)s apple': u'%(num)s Apfel',66 '%(num)s apples': u'%(num)s Äpfel'67 }68}6970
257 unmodified lines
328 '"<test>") }}{% endautoescape %}')329 assert t.render(ae=True) == '<strong>Wert: &lt;test&gt;</strong>'330 assert t.render(ae=False) == '<strong>Wert: <test></strong>'331332 def test_num_used_twice(self):333 tmpl = newstyle_i18n_env.get_template('ngettext_long.html')334 assert tmpl.render(apples=5, LANGUAGE='de') == u'5 Äpfel'335336 def test_num_called_num(self):337 source = newstyle_i18n_env.compile('''338 {% trans num=3 %}{{ num }} apple{% pluralize339 %}{{ num }} apples{% endtrans %}340 ''', raw=True)341 # quite hacky, but the only way to properly test that. The idea is342 # that the generated code does not pass num twice (although that343 # would work) for better performance. This only works on the344 # newstyle gettext of course345 assert re.search(r"l_ngettext, u?'\%\(num\)s apple', u?'\%\(num\)s "346 r"apples', 3", source) is not None347348349class AutoEscapeTestCase(JinjaTestCase):350351 def test_scoped_setting(self):352 env = Environment(extensions=['jinja2.ext.autoescape'],
80 unmodified lines
modified:jinja2/ext.py+14 added-5 removed
137 unmodified lines
138139140def _make_new_ngettext(func):141 @contextfunction142 def ngettext(__context, __singular, __plural, num, **variables):143 variables.setdefault('num', num)144 rv = __context.call(func, __singular, __plural, num)142 def ngettext(__context, __singular, __plural, __num, **variables):143 variables.setdefault('num', __num)144 rv = __context.call(func, __singular, __plural, __num)145 if __context.eval_ctx.autoescape:146 rv = Markup(rv)147 return rv % variables148 return ngettext
60 unmodified lines
209210 def parse(self, parser):211 """Parse a translatable tag."""212 lineno = next(parser.stream).lineno213 num_called_num = False214215 # find all the variables referenced. Additionally a variable can be216 # defined in the body of the trans block too, but this is checked at217 # a later state.
18 unmodified lines
236 next(parser.stream)237 variables[name.value] = var = parser.parse_expression()238 else:239 variables[name.value] = var = nodes.Name(name.value, 'load')240241 if plural_expr is None:242 plural_expr = var243 num_called_num = name.value == 'num'244245 parser.stream.expect('block_end')246247 plural = plural_names = None
5 unmodified lines
253 if singular_names:254 referenced.update(singular_names)255 if plural_expr is None:256 plural_expr = nodes.Name(singular_names[0], 'load')257 num_called_num = singular_names[0] == 'num'258259 # if we have a pluralize block, we parse that too260 if parser.stream.current.test('name:pluralize'):261 have_plural = True
4 unmodified lines
266 parser.fail('unknown variable %r for pluralization' %267 name.value, name.lineno,268 exc=TemplateAssertionError)269 plural_expr = variables[name.value]270 num_called_num = name.value == 'num'271 parser.stream.expect('block_end')272 plural_names, plural = self._parse_block(parser, False)273 next(parser.stream)274 referenced.update(plural_names)
10 unmodified lines
285 elif plural_expr is None:286 parser.fail('pluralize without variables', lineno)287288 node = self._make_node(singular, plural, variables, plural_expr,284 bool(referenced))289 bool(referenced), num_called_num)290 node.set_lineno(lineno)291 return node292293 def _parse_block(self, parser, allow_pluralize):
28 unmodified lines
322323 return referenced, concat(buf)324325 def _make_node(self, singular, plural, variables, plural_expr,321 vars_referenced):326 vars_referenced, num_called_num):327 """Generates a useful node from the data provided."""328 # no variables referenced? no need to escape for old style329 # gettext invocations330 if not vars_referenced and not self.environment.newstyle_gettext:
20 unmodified lines
351 # enough to handle the variable expansion and autoescape352 # handling itself353 if self.environment.newstyle_gettext:354 for key, value in variables.iteritems():355 # the function adds that later anyways in case num was356 # called num, so just skip it.357 if num_called_num and key == 'num':358 continue359 node.kwargs.append(nodes.Keyword(key, value))360361 # otherwise do that here362 else:
242 unmodified lines