]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
don't evaluate trans arguments twice.
authorFlorian Apolloner <florian@apolloner.eu>
Wed, 18 Jan 2012 16:08:48 +0000 (17:08 +0100)
committerFlorian Apolloner <florian@apolloner.eu>
Wed, 18 Jan 2012 16:08:48 +0000 (17:08 +0100)
jinja2/ext.py
jinja2/testsuite/ext.py

index 206756fe7921f13fcffdde556476d1b011aa2bd0..1d888a2ab20a1ab6eefcd0aaa53138d82adaee85 100644 (file)
@@ -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')
index 6ca6c228f847947b0497e73b4012e6206712f3d7..b9ffa66b54e68cd95560d05f72830bbb51caaa88 100644 (file)
@@ -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 %}')