diff options
author | Florian Apolloner <florian@apolloner.eu> | 2012-01-18 17:08:48 +0100 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2012-01-18 17:08:48 +0100 |
commit | 5a25a47996e8a2ad93fa92217f9247dea9a3c424 (patch) | |
tree | f3522d8979f185c957149a49a6b01abc92d1fb7d /jinja2 | |
parent | 5145401fe5f9995a32757f34359623b87048ab8b (diff) | |
download | jinja2-5a25a47996e8a2ad93fa92217f9247dea9a3c424.tar.gz |
don't evaluate trans arguments twice.
Diffstat (limited to 'jinja2')
-rw-r--r-- | jinja2/ext.py | 5 | ||||
-rw-r--r-- | jinja2/testsuite/ext.py | 11 |
2 files changed, 15 insertions, 1 deletions
diff --git a/jinja2/ext.py b/jinja2/ext.py index 206756f..1d888a2 100644 --- a/jinja2/ext.py +++ b/jinja2/ext.py @@ -241,7 +241,10 @@ 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(name.value, 'load') + else: + plural_expr = var num_called_num = name.value == 'num' parser.stream.expect('block_end') diff --git a/jinja2/testsuite/ext.py b/jinja2/testsuite/ext.py index 6ca6c22..b9ffa66 100644 --- a/jinja2/testsuite/ext.py +++ b/jinja2/testsuite/ext.py @@ -38,6 +38,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 }}' + '{% pluralize %}{{ user_count }}{% endtrans %}', 'stringformat.html': '{{ _("User: %(num)s")|format(num=user_count) }}' } @@ -259,6 +261,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) == '1' + 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 %}') |