Skip to content

Commit

Permalink
Fixed a bug in the i18n extension that caused the explicit pluralizat…
Browse files Browse the repository at this point in the history
…ion block to look up the wrong variable.

--HG--
branch : trunk
  • Loading branch information
mitsuhiko committed Sep 6, 2008
1 parent ce67710 commit 4720c36
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES
Expand Up @@ -9,6 +9,9 @@ Version 2.1
change an inner loop overwrote the loop variable from the outer one after change an inner loop overwrote the loop variable from the outer one after
iteration. iteration.


- fixed a bug with the i18n extension that caused the explicit pluralization
block to look up the wrong variable.

Version 2.0 Version 2.0
----------- -----------
(codename jinjavitus, released on July 17th 2008) (codename jinjavitus, released on July 17th 2008)
Expand Down
14 changes: 13 additions & 1 deletion jinja2/ext.py
Expand Up @@ -124,6 +124,13 @@ class InternationalizationExtension(Extension):
"""This extension adds gettext support to Jinja2.""" """This extension adds gettext support to Jinja2."""
tags = set(['trans']) tags = set(['trans'])


# TODO: the i18n extension is currently reevaluating values in a few
# situations. Take this example:
# {% trans count=something() %}{{ count }} foo{% pluralize
# %}{{ count }} fooss{% endtrans %}
# something is called twice here. One time for the gettext value and
# the other time for the n-parameter of the ngettext function.

def __init__(self, environment): def __init__(self, environment):
Extension.__init__(self, environment) Extension.__init__(self, environment)
environment.globals['_'] = _gettext_alias environment.globals['_'] = _gettext_alias
Expand Down Expand Up @@ -205,7 +212,12 @@ def parse(self, parser):
have_plural = True have_plural = True
parser.stream.next() parser.stream.next()
if parser.stream.current.type is not 'block_end': if parser.stream.current.type is not 'block_end':
plural_expr = parser.parse_expression() name = parser.stream.expect('name')
if name.value not in variables:
parser.fail('unknown variable %r for pluralization' %
name.value, name.lineno,
exc=TemplateAssertionError)
plural_expr = variables[name.value]
parser.stream.expect('block_end') parser.stream.expect('block_end')
plural_names, plural = self._parse_block(parser, False) plural_names, plural = self._parse_block(parser, False)
parser.stream.next() parser.stream.next()
Expand Down
10 changes: 10 additions & 0 deletions tests/test_i18n.py
Expand Up @@ -6,7 +6,9 @@
:copyright: 2007 by Armin Ronacher. :copyright: 2007 by Armin Ronacher.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
from py.test import raises
from jinja2 import Environment, DictLoader, contextfunction from jinja2 import Environment, DictLoader, contextfunction
from jinja2.exceptions import TemplateAssertionError


templates = { templates = {
'master.html': '<title>{{ page_title|default(_("missing")) }}</title>' 'master.html': '<title>{{ page_title|default(_("missing")) }}</title>'
Expand Down Expand Up @@ -66,6 +68,14 @@ def test_trans_plural():
assert tmpl.render(LANGUAGE='de', user_count=2) == '2 Benutzer online' assert tmpl.render(LANGUAGE='de', user_count=2) == '2 Benutzer online'




def test_complex_plural():
tmpl = i18n_env.from_string('{% trans foo=42, count=2 %}{{ count }} item{% '
'pluralize count %}{{ count }} items{% endtrans %}')
assert tmpl.render() == '2 items'
raises(TemplateAssertionError, i18n_env.from_string,
'{% trans foo %}...{% pluralize bar %}...{% endtrans %}')


def test_trans_stringformatting(): def test_trans_stringformatting():
tmpl = i18n_env.get_template('stringformat.html') tmpl = i18n_env.get_template('stringformat.html')
assert tmpl.render(LANGUAGE='de', user_count=5) == 'Benutzer: 5' assert tmpl.render(LANGUAGE='de', user_count=5) == 'Benutzer: 5'
Expand Down

0 comments on commit 4720c36

Please sign in to comment.