summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmin Ronacher <armin.ronacher@active-4.com>2013-05-19 12:45:48 +0100
committerArmin Ronacher <armin.ronacher@active-4.com>2013-05-19 12:45:48 +0100
commit9417d013111e8e7ab1276e0ce5c271d24e002c4b (patch)
treec3773ac381ffe04a04e61df101ede47f57753ba9
parent15e9eef3808443d492209a739c10ed9c904c1ca5 (diff)
parent79c8475f80257be4999a20d1432ff6a8e3f38815 (diff)
downloadjinja2-9417d013111e8e7ab1276e0ce5c271d24e002c4b.tar.gz
Merge remote-tracking branch 'apollo13/master'
-rw-r--r--jinja2/ext.py14
-rw-r--r--jinja2/testsuite/ext.py11
2 files changed, 23 insertions, 2 deletions
diff --git a/jinja2/ext.py b/jinja2/ext.py
index 639f159..c456dbf 100644
--- a/jinja2/ext.py
+++ b/jinja2/ext.py
@@ -222,6 +222,7 @@ class InternationalizationExtension(Extension):
# defined in the body of the trans block too, but this is checked at
# a later state.
plural_expr = None
+ plural_expr_assignment = None
variables = {}
while parser.stream.current.type != 'block_end':
if variables:
@@ -245,7 +246,13 @@ class InternationalizationExtension(Extension):
variables[name.value] = var = nodes.Name(name.value, 'load')
if plural_expr is None:
- plural_expr = var
+ if isinstance(var, nodes.Call):
+ plural_expr = nodes.Name('_trans', 'load')
+ variables[name.value] = plural_expr
+ plural_expr_assignment = nodes.Assign(
+ nodes.Name('_trans', 'store'), var)
+ else:
+ plural_expr = var
num_called_num = name.value == 'num'
parser.stream.expect('block_end')
@@ -295,7 +302,10 @@ class InternationalizationExtension(Extension):
bool(referenced),
num_called_num and have_plural)
node.set_lineno(lineno)
- return node
+ if plural_expr_assignment is not None:
+ return [plural_expr_assignment, node]
+ else:
+ return node
def _parse_block(self, parser, allow_pluralize):
"""Parse until the next block tag with a given name."""
diff --git a/jinja2/testsuite/ext.py b/jinja2/testsuite/ext.py
index 29f78b1..817b1d2 100644
--- a/jinja2/testsuite/ext.py
+++ b/jinja2/testsuite/ext.py
@@ -32,6 +32,8 @@ i18n_templates = {
'{% trans %}watch out{% endtrans %}{% endblock %}',
'plural.html': '{% trans user_count %}One user online{% pluralize %}'
'{{ user_count }} users online{% endtrans %}',
+ 'plural2.html': '{% trans user_count=get_user_count() %}{{ user_count }}s'
+ '{% pluralize %}{{ user_count }}p{% endtrans %}',
'stringformat.html': '{{ _("User: %(num)s")|format(num=user_count) }}'
}
@@ -253,6 +255,15 @@ class InternationalizationTestCase(JinjaTestCase):
assert tmpl.render(LANGUAGE='de', user_count=1) == 'Ein Benutzer online'
assert tmpl.render(LANGUAGE='de', user_count=2) == '2 Benutzer online'
+ def test_trans_plural_with_functions(self):
+ tmpl = i18n_env.get_template('plural2.html')
+ def get_user_count():
+ get_user_count.called += 1
+ return 1
+ get_user_count.called = 0
+ assert tmpl.render(LANGUAGE='de', get_user_count=get_user_count) == '1s'
+ assert get_user_count.called == 1
+
def test_complex_plural(self):
tmpl = i18n_env.from_string('{% trans foo=42, count=2 %}{{ count }} item{% '
'pluralize count %}{{ count }} items{% endtrans %}')