Skip to content
Myr

babel extraction can now properly extract newstyle gettext calls.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

using the variable `num` in newstyle gettext for something else than the pluralize count will no longer raise a :exc:`KeyError`. --HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 5a8d496 · Browse files · 4 files+26 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+26 added−13 removed

  • other:+26 added−13 removed

4 files+26 added−13 removed

modified:jinja2/testsuite/ext.py+5 added-1 removed
51 unmodified lines
52 'ngettext.html': '{{ ngettext("%(num)s apple", "%(num)s apples", apples) }}',53 'ngettext_long.html': '{% trans num=apples %}{{ num }} apple{% pluralize %}'54 '{{ num }} apples{% endtrans %}',55 'transvars1.html': '{% trans %}User: {{ num }}{% endtrans %}',56 'transvars2.html': '{% trans num=count %}User: {{ num }}{% endtrans %}'56 'transvars2.html': '{% trans num=count %}User: {{ num }}{% endtrans %}',57 'transvars3.html': '{% trans count=num %}User: {{ count }}{% endtrans %}'58}596061languages = {
2 unmodified lines
64 'watch out': u'pass auf',65 'One user online': u'Ein Benutzer online',66 '%(user_count)s users online': u'%(user_count)s Benutzer online',67 'User: %(num)s': u'Benutzer: %(num)s',68 'User: %(count)s': u'Benutzer: %(count)s',69 '%(num)s apple': u'%(num)s Apfel',70 '%(num)s apples': u'%(num)s Äpfel'71 }72}
278 unmodified lines
351352 def test_trans_vars(self):353 t1 = newstyle_i18n_env.get_template('transvars1.html')354 t2 = newstyle_i18n_env.get_template('transvars2.html')355 t3 = newstyle_i18n_env.get_template('transvars3.html')356 assert t1.render(num=1, LANGUAGE='de') == 'Benutzer: 1'357 assert t2.render(count=23, LANGUAGE='de') == 'Benutzer: 23'358 assert t3.render(num=42, LANGUAGE='de') == 'Benutzer: 42'359360361class AutoEscapeTestCase(JinjaTestCase):362
82 unmodified lines
modified:jinja2/ext.py+17 added-11 removed
11 unmodified lines
12"""13from collections import deque14from jinja2 import nodes15from jinja2.defaults import *16from jinja2.environment import get_spontaneous_environment16from jinja2.environment import Environment17from jinja2.runtime import Undefined, concat18from jinja2.exceptions import TemplateAssertionError, TemplateSyntaxError19from jinja2.utils import contextfunction, import_string, Markup, next20
264 unmodified lines
285 elif plural_expr is None:286 parser.fail('pluralize without variables', lineno)287288 node = self._make_node(singular, plural, variables, plural_expr,289 bool(referenced), num_called_num)289 bool(referenced),290 num_called_num and have_plural)291 node.set_lineno(lineno)292 return node293294 def _parse_block(self, parser, allow_pluralize):
250 unmodified lines
545 keywords. For best results, make sure to not have more than one546 gettext call in one line of code and the matching comment in the547 same line or the line before.548549 .. versionchanged:: 2.5.1550 The `newstyle_gettext` flag can be set to `True` to enable newstyle551 gettext calls.552553 :param fileobj: the file-like object the messages should be extracted from554 :param keywords: a list of keywords (i.e. function names) that should be555 recognized as translation functions556 :param comment_tags: a list of translator tags to search for and include
10 unmodified lines
567 extensions.add(import_string(extension))568 if InternationalizationExtension not in extensions:569 extensions.add(InternationalizationExtension)570571 def getbool(options, key, default=False):572 options.get(key, str(default)).lower() in ('1', 'on', 'yes', 'true')573566 environment = get_spontaneous_environment(574 environment = Environment(575 options.get('block_start_string', BLOCK_START_STRING),576 options.get('block_end_string', BLOCK_END_STRING),577 options.get('variable_start_string', VARIABLE_START_STRING),578 options.get('variable_end_string', VARIABLE_END_STRING),579 options.get('comment_start_string', COMMENT_START_STRING),580 options.get('comment_end_string', COMMENT_END_STRING),581 options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX,582 options.get('line_comment_prefix') or LINE_COMMENT_PREFIX,575 str(options.get('trim_blocks', TRIM_BLOCKS)).lower() in \576 ('1', 'on', 'yes', 'true'),583 getbool(options, 'trim_blocks', TRIM_BLOCKS),584 NEWLINE_SEQUENCE, frozenset(extensions),578 # fill with defaults so that environments are shared579 # with other spontaneus environments. The rest of the580 # arguments are optimizer, undefined, finalize, autoescape,581 # loader, cache size, auto reloading setting and the582 # bytecode cache583 True, Undefined, None, False, None, 0, False, None585 cache_size=0,586 auto_reload=False587 )588589 if getbool(options, 'newstyle_gettext'):590 environment.newstyle_gettext = True591592 source = fileobj.read().decode(options.get('encoding', 'utf-8'))593 try:594 node = environment.parse(source)595 tokens = list(environment.lex(environment.preprocess(source)))
15 unmodified lines
modified:CHANGES+3 added
8 unmodified lines
9 are now intercepted and converted to undefineds. This solves a10 lot of debugging grief. (StopIteration is used internally to11 abort template execution)12- improved performance of macro calls slightly.13- babel extraction can now properly extract newstyle gettext calls.14- using the variable `num` in newstyle gettext for something else15 than the pluralize count will no longer raise a :exc:`KeyError`.1617Version 2.518-----------19(codename Incoherence, relased on May 29th 2010)
178 unmodified lines
modified:setup.py+1 added-1 removed
53 unmodified lines
545556setup(57 name='Jinja2',58 version='2.6',58 version='2.5.1',59 url='http://jinja.pocoo.org/',60 license='BSD',61 author='Armin Ronacher',62 author_email='armin.ronacher@active-4.com',
35 unmodified lines