Fixed a bug in the i18n extension that caused the explicit pluralization block to look up the wrong variable.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent ce67710 · Browse files · 3 files·+26 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+26 added−1 removed
- tests:+10 added−0 removed
- other:+16 added−1 removed
Provenance
- Commit
4720c36- Tree
94799d3- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
ce67710
3 files changed+26 added−1 removed
modified:jinja2/ext.py+13 added-1 removed
122 unmodified lines123 class InternationalizationExtension(Extension):124 """This extension adds gettext support to Jinja2."""125 tags = set(['trans'])126 127+ # TODO: the i18n extension is currently reevaluating values in a few128+ # situations. Take this example:129+ # {% trans count=something() %}{{ count }} foo{% pluralize130+ # %}{{ count }} fooss{% endtrans %}131+ # something is called twice here. One time for the gettext value and132+ # the other time for the n-parameter of the ngettext function.133+134 def __init__(self, environment):135 Extension.__init__(self, environment)136 environment.globals['_'] = _gettext_alias137 environment.extend(73 unmodified lines211 if parser.stream.current.test('name:pluralize'):212 have_plural = True213 parser.stream.next()214 if parser.stream.current.type is not 'block_end':208- plural_expr = parser.parse_expression()215+ name = parser.stream.expect('name')216+ if name.value not in variables:217+ parser.fail('unknown variable %r for pluralization' %218+ name.value, name.lineno,219+ exc=TemplateAssertionError)220+ plural_expr = variables[name.value]221 parser.stream.expect('block_end')222 plural_names, plural = self._parse_block(parser, False)223 parser.stream.next()224 referenced.update(plural_names)228 unmodified linesmodified:tests/test_i18n.py+10 added
4 unmodified lines5 6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8 """9+from py.test import raises10 from jinja2 import Environment, DictLoader, contextfunction11+from jinja2.exceptions import TemplateAssertionError12 13 templates = {14 'master.html': '<title>{{ page_title|default(_("missing")) }}</title>'15 '{% block body %}{% endblock %}',51 unmodified lines67 assert tmpl.render(LANGUAGE='de', user_count=1) == 'Ein Benutzer online'68 assert tmpl.render(LANGUAGE='de', user_count=2) == '2 Benutzer online'69 70 71+def test_complex_plural():72+ tmpl = i18n_env.from_string('{% trans foo=42, count=2 %}{{ count }} item{% '73+ 'pluralize count %}{{ count }} items{% endtrans %}')74+ assert tmpl.render() == '2 items'75+ raises(TemplateAssertionError, i18n_env.from_string,76+ '{% trans foo %}...{% pluralize bar %}...{% endtrans %}')77+78+79 def test_trans_stringformatting():80 tmpl = i18n_env.get_template('stringformat.html')81 assert tmpl.render(LANGUAGE='de', user_count=5) == 'Benutzer: 5'82 14 unmodified linesmodified:CHANGES+3 added
7 unmodified lines8 - fixed a bug with nested loops and the special loop variable. Before the9 change an inner loop overwrote the loop variable from the outer one after10 iteration.11 12+- fixed a bug with the i18n extension that caused the explicit pluralization13+ block to look up the wrong variable.14+15 Version 2.016 -----------17 (codename jinjavitus, released on July 17th 2008)18 32 unmodified lines