diff --git a/django/core/template/__init__.py b/django/core/template/__init__.py index 0011cb2c9a..2f1caf657f 100644 --- a/django/core/template/__init__.py +++ b/django/core/template/__init__.py @@ -348,7 +348,7 @@ class FilterParser: self.current_filter_arg = None # First read the variable part - decide on wether we need # to parse a string or a variable by peeking into the stream - if self.peek_char() in ('"', "'"): + if self.peek_char() in ('_', '"', "'"): self.var = self.read_constant_string_token() else: self.var = self.read_alphanumeric_token() @@ -382,7 +382,14 @@ class FilterParser: or ' characters. The string is returned with it's delimiters.""" val = '' qchar = None + i18n = False self.next_char() + if self.current == '_': + i18n = True + self.next_char() + if self.current != '(': + raise TemplateSyntaxError, "Bad character (expecting '(') '%s'" % self.current + self.next_char() if not self.current in ('"', "'"): raise TemplateSyntaxError, "Bad character (expecting '\"' or ''') '%s'" % self.current qchar = self.current @@ -394,6 +401,11 @@ class FilterParser: val += self.current val += self.current self.next_char() + if i18n: + if self.current != ')': + raise TemplateSyntaxError, "Bad character (expecting ')') '%s'" % self.current + self.next_char() + val = qchar+_(val.strip(qchar))+qchar return val def read_alphanumeric_token(self): diff --git a/tests/othertests/templates.py b/tests/othertests/templates.py index c889f2c7cb..ec6c55fb05 100644 --- a/tests/othertests/templates.py +++ b/tests/othertests/templates.py @@ -252,6 +252,9 @@ TEMPLATE_TESTS = { # usage of the get_available_languages tag 'i18n12': ('{% load i18n %}{% get_available_languages as langs %}{% for lang in langs %}{% ifequal lang.0 "de" %}{{ lang.0 }}{% endifequal %}{% endfor %}', {}, 'de'), + + # translation of a constant string + 'i18n13': ('{{ _("Page not found") }}', {'LANGUAGE_CODE': 'de'}, 'Seite nicht gefunden'), } def test_template_loader(template_name, template_dirs=None):