From: Armin Ronacher Date: Thu, 1 Jul 2010 10:15:39 +0000 (+0200) Subject: babel extraction can now properly extract newstyle gettext calls. X-Git-Tag: 2.5.1~10 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=4f77a305df3340ec74126b1d00d082bee4fc7179;p=jinja2.git babel extraction can now properly extract newstyle gettext calls. using the variable `num` in newstyle gettext for something else than the pluralize count will no longer raise a :exc:`KeyError`. --HG-- branch : trunk --- diff --git a/CHANGES b/CHANGES index 133a913..111e89d 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,9 @@ Version 2.5.1 lot of debugging grief. (StopIteration is used internally to abort template execution) - improved performance of macro calls slightly. +- babel extraction can now properly extract newstyle gettext calls. +- using the variable `num` in newstyle gettext for something else + than the pluralize count will no longer raise a :exc:`KeyError`. Version 2.5 ----------- diff --git a/jinja2/ext.py b/jinja2/ext.py index e46287b..ddab4fb 100644 --- a/jinja2/ext.py +++ b/jinja2/ext.py @@ -13,7 +13,7 @@ from collections import deque from jinja2 import nodes from jinja2.defaults import * -from jinja2.environment import get_spontaneous_environment +from jinja2.environment import Environment from jinja2.runtime import Undefined, concat from jinja2.exceptions import TemplateAssertionError, TemplateSyntaxError from jinja2.utils import contextfunction, import_string, Markup, next @@ -286,7 +286,8 @@ class InternationalizationExtension(Extension): parser.fail('pluralize without variables', lineno) node = self._make_node(singular, plural, variables, plural_expr, - bool(referenced), num_called_num) + bool(referenced), + num_called_num and have_plural) node.set_lineno(lineno) return node @@ -545,6 +546,10 @@ def babel_extract(fileobj, keywords, comment_tags, options): gettext call in one line of code and the matching comment in the same line or the line before. + .. versionchanged:: 2.5.1 + The `newstyle_gettext` flag can be set to `True` to enable newstyle + gettext calls. + :param fileobj: the file-like object the messages should be extracted from :param keywords: a list of keywords (i.e. function names) that should be recognized as translation functions @@ -563,7 +568,10 @@ def babel_extract(fileobj, keywords, comment_tags, options): if InternationalizationExtension not in extensions: extensions.add(InternationalizationExtension) - environment = get_spontaneous_environment( + def getbool(options, key, default=False): + options.get(key, str(default)).lower() in ('1', 'on', 'yes', 'true') + + environment = Environment( options.get('block_start_string', BLOCK_START_STRING), options.get('block_end_string', BLOCK_END_STRING), options.get('variable_start_string', VARIABLE_START_STRING), @@ -572,17 +580,15 @@ def babel_extract(fileobj, keywords, comment_tags, options): options.get('comment_end_string', COMMENT_END_STRING), options.get('line_statement_prefix') or LINE_STATEMENT_PREFIX, options.get('line_comment_prefix') or LINE_COMMENT_PREFIX, - str(options.get('trim_blocks', TRIM_BLOCKS)).lower() in \ - ('1', 'on', 'yes', 'true'), + getbool(options, 'trim_blocks', TRIM_BLOCKS), NEWLINE_SEQUENCE, frozenset(extensions), - # fill with defaults so that environments are shared - # with other spontaneus environments. The rest of the - # arguments are optimizer, undefined, finalize, autoescape, - # loader, cache size, auto reloading setting and the - # bytecode cache - True, Undefined, None, False, None, 0, False, None + cache_size=0, + auto_reload=False ) + if getbool(options, 'newstyle_gettext'): + environment.newstyle_gettext = True + source = fileobj.read().decode(options.get('encoding', 'utf-8')) try: node = environment.parse(source) diff --git a/jinja2/testsuite/ext.py b/jinja2/testsuite/ext.py index f252d67..6a88834 100644 --- a/jinja2/testsuite/ext.py +++ b/jinja2/testsuite/ext.py @@ -53,7 +53,8 @@ newstyle_i18n_templates = { 'ngettext_long.html': '{% trans num=apples %}{{ num }} apple{% pluralize %}' '{{ num }} apples{% endtrans %}', 'transvars1.html': '{% trans %}User: {{ num }}{% endtrans %}', - 'transvars2.html': '{% trans num=count %}User: {{ num }}{% endtrans %}' + 'transvars2.html': '{% trans num=count %}User: {{ num }}{% endtrans %}', + 'transvars3.html': '{% trans count=num %}User: {{ count }}{% endtrans %}' } @@ -64,6 +65,7 @@ languages = { 'One user online': u'Ein Benutzer online', '%(user_count)s users online': u'%(user_count)s Benutzer online', 'User: %(num)s': u'Benutzer: %(num)s', + 'User: %(count)s': u'Benutzer: %(count)s', '%(num)s apple': u'%(num)s Apfel', '%(num)s apples': u'%(num)s Äpfel' } @@ -350,8 +352,10 @@ class NewstyleInternationalizationTestCase(JinjaTestCase): def test_trans_vars(self): t1 = newstyle_i18n_env.get_template('transvars1.html') t2 = newstyle_i18n_env.get_template('transvars2.html') + t3 = newstyle_i18n_env.get_template('transvars3.html') assert t1.render(num=1, LANGUAGE='de') == 'Benutzer: 1' assert t2.render(count=23, LANGUAGE='de') == 'Benutzer: 23' + assert t3.render(num=42, LANGUAGE='de') == 'Benutzer: 42' class AutoEscapeTestCase(JinjaTestCase): diff --git a/setup.py b/setup.py index e97afcf..e409e55 100644 --- a/setup.py +++ b/setup.py @@ -55,7 +55,7 @@ if sys.version_info >= (3, 0): setup( name='Jinja2', - version='2.6', + version='2.5.1', url='http://jinja.pocoo.org/', license='BSD', author='Armin Ronacher',