Merge remote-tracking branch 'apollo13/master'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parents 15e9eef, 79c8475 · Browse files · 2 files·+23 added−2 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+23 added−2 removed
- other:+23 added−2 removed
Provenance
- Commit
9417d01- Tree
c3773ac- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent 1
15e9eef- Parent 2
79c8475
2 files changed+23 added−2 removed
modified:jinja2/testsuite/ext.py+11 added
30 unmodified lines31 'child.html': '{% extends "master.html" %}{% block body %}'32 '{% trans %}watch out{% endtrans %}{% endblock %}',33 'plural.html': '{% trans user_count %}One user online{% pluralize %}'34 '{{ user_count }} users online{% endtrans %}',35+ 'plural2.html': '{% trans user_count=get_user_count() %}{{ user_count }}s'36+ '{% pluralize %}{{ user_count }}p{% endtrans %}',37 'stringformat.html': '{{ _("User: %(num)s")|format(num=user_count) }}'38 }39 40 newstyle_i18n_templates = {213 unmodified lines254 tmpl = i18n_env.get_template('plural.html')255 assert tmpl.render(LANGUAGE='de', user_count=1) == 'Ein Benutzer online'256 assert tmpl.render(LANGUAGE='de', user_count=2) == '2 Benutzer online'257 258+ def test_trans_plural_with_functions(self):259+ tmpl = i18n_env.get_template('plural2.html')260+ def get_user_count():261+ get_user_count.called += 1262+ return 1263+ get_user_count.called = 0264+ assert tmpl.render(LANGUAGE='de', get_user_count=get_user_count) == '1s'265+ assert get_user_count.called == 1266+267 def test_complex_plural(self):268 tmpl = i18n_env.from_string('{% trans foo=42, count=2 %}{{ count }} item{% '269 'pluralize count %}{{ count }} items{% endtrans %}')270 assert tmpl.render() == '2 items'190 unmodified linesmodified:jinja2/ext.py+12 added-2 removed
220 unmodified lines221 # find all the variables referenced. Additionally a variable can be222 # defined in the body of the trans block too, but this is checked at223 # a later state.224 plural_expr = None225+ plural_expr_assignment = None226 variables = {}227 while parser.stream.current.type != 'block_end':228 if variables:229 parser.stream.expect('comma')15 unmodified lines245 else:246 variables[name.value] = var = nodes.Name(name.value, 'load')247 248 if plural_expr is None:249+ if isinstance(var, nodes.Call):250+ plural_expr = nodes.Name('_trans', 'load')251+ variables[name.value] = plural_expr252+ plural_expr_assignment = nodes.Assign(253+ nodes.Name('_trans', 'store'), var)254+ else:248- plural_expr = var255+ plural_expr = var256 num_called_num = name.value == 'num'257 258 parser.stream.expect('block_end')259 41 unmodified lines301 node = self._make_node(singular, plural, variables, plural_expr,302 bool(referenced),303 num_called_num and have_plural)304 node.set_lineno(lineno)305+ if plural_expr_assignment is not None:306+ return [plural_expr_assignment, node]307+ else:298- return node308+ return node309 310 def _parse_block(self, parser, allow_pluralize):311 """Parse until the next block tag with a given name."""312 referenced = []325 unmodified lines