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
Provenance
- Commit
4f77a30- Tree
0f28a42- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
5a8d496
4 files changed+26 added−13 removed
modified:jinja2/testsuite/ext.py+5 added-1 removed
51 unmodified lines52 '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 }59 60 61 languages = {2 unmodified lines64 '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 lines351 352 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'359 360 361 class AutoEscapeTestCase(JinjaTestCase):362 82 unmodified linesmodified:jinja2/ext.py+17 added-11 removed
11 unmodified lines12 """13 from collections import deque14 from jinja2 import nodes15 from jinja2.defaults import *16-from jinja2.environment import get_spontaneous_environment16+from jinja2.environment import Environment17 from jinja2.runtime import Undefined, concat18 from jinja2.exceptions import TemplateAssertionError, TemplateSyntaxError19 from jinja2.utils import contextfunction, import_string, Markup, next20 264 unmodified lines285 elif plural_expr is None:286 parser.fail('pluralize without variables', lineno)287 288 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 node293 294 def _parse_block(self, parser, allow_pluralize):250 unmodified lines545 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.548 549+ .. versionchanged:: 2.5.1550+ The `newstyle_gettext` flag can be set to `True` to enable newstyle551+ gettext calls.552+553 :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 include10 unmodified lines567 extensions.add(import_string(extension))568 if InternationalizationExtension not in extensions:569 extensions.add(InternationalizationExtension)570 571+ def getbool(options, key, default=False):572+ options.get(key, str(default)).lower() in ('1', 'on', 'yes', 'true')573+566- 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 )588 589+ if getbool(options, 'newstyle_gettext'):590+ environment.newstyle_gettext = True591+592 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 linesmodified:CHANGES+3 added
8 unmodified lines9 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`.16 17 Version 2.518 -----------19 (codename Incoherence, relased on May 29th 2010)178 unmodified linesmodified:setup.py+1 added-1 removed
53 unmodified lines54 55 56 setup(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