Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 4f77a305df3340ec74126b1d00d082bee4fc7179 1 parent 5a8d496
@mitsuhiko authored
Showing with 26 additions and 13 deletions.
  1. +3 −0  CHANGES
  2. +17 −11 jinja2/ext.py
  3. +5 −1 jinja2/testsuite/ext.py
  4. +1 −1  setup.py
View
3  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
-----------
View
28 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 @@ def parse(self, parser):
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)
View
6 jinja2/testsuite/ext.py
@@ -53,7 +53,8 @@
'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 @@
'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 @@ def test_num_called_num(self):
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):
View
2  setup.py
@@ -55,7 +55,7 @@
setup(
name='Jinja2',
- version='2.6',
+ version='2.5.1',
url='http://jinja.pocoo.org/',
license='BSD',
author='Armin Ronacher',
Please sign in to comment.
Something went wrong with that request. Please try again.