Changeset 6682
- Timestamp:
- 11/17/07 06:12:18 (1 year ago)
- Files:
-
- django/trunk/django/templatetags/i18n.py (modified) (6 diffs)
- django/trunk/tests/regressiontests/templates/tests.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/templatetags/i18n.py
r6565 r6682 1 1 import re 2 2 3 from django.template import Node, Variable 3 from django.template import Node, Variable, VariableNode 4 4 from django.template import TemplateSyntaxError, TokenParser, Library 5 5 from django.template import TOKEN_TEXT, TOKEN_VAR 6 6 from django.utils import translation 7 from django.utils.encoding import force_unicode 7 8 8 9 register = Library() … … 46 47 47 48 class BlockTranslateNode(Node): 48 def __init__(self, extra_context, singular, plural=None, countervar=None, counter=None): 49 def __init__(self, extra_context, singular, plural=None, countervar=None, 50 counter=None): 49 51 self.extra_context = extra_context 50 52 self.singular = singular … … 55 57 def render_token_list(self, tokens): 56 58 result = [] 59 vars = [] 57 60 for token in tokens: 58 61 if token.token_type == TOKEN_TEXT: 59 62 result.append(token.contents) 60 63 elif token.token_type == TOKEN_VAR: 61 result.append('%%(%s)s' % token.contents) 62 return ''.join(result) 64 result.append(u'%%(%s)s' % token.contents) 65 vars.append(token.contents) 66 return ''.join(result), vars 63 67 64 68 def render(self, context): 65 69 context.push() 66 for var, val in self.extra_context.items():67 context[var] = val.re solve(context)68 singular = self.render_token_list(self.singular)70 for var, val in self.extra_context.items(): 71 context[var] = val.render(context) 72 singular, vars = self.render_token_list(self.singular) 69 73 if self.plural and self.countervar and self.counter: 70 74 count = self.counter.resolve(context) 71 75 context[self.countervar] = count 72 plural = self.render_token_list(self.plural) 76 plural = self.render_token_list(self.plural)[0] 73 77 result = translation.ungettext(singular, plural, count) 74 78 else: 75 79 result = translation.ugettext(singular) 76 80 # Escape all isolated '%' before substituting in the context. 77 result = re.sub('%(?!\()', '%%', result) % context 81 result = re.sub(u'%(?!\()', u'%%', result) 82 data = dict([(v, force_unicode(context[v])) for v in vars]) 78 83 context.pop() 79 return result 84 return result % data 80 85 81 86 def do_get_available_languages(parser, token): … … 199 204 """ 200 205 class BlockTranslateParser(TokenParser): 201 202 206 def top(self): 203 207 countervar = None … … 210 214 if self.tag() != 'as': 211 215 raise TemplateSyntaxError, "variable bindings in 'blocktrans' must be 'with value as variable'" 212 extra_context[self.tag()] = parser.compile_filter(value) 216 extra_context[self.tag()] = VariableNode( 217 parser.compile_filter(value)) 213 218 elif tag == 'count': 214 219 counter = parser.compile_filter(self.value()) … … 242 247 raise TemplateSyntaxError, "'blocktrans' doesn't allow other block tags (seen %r) inside it" % token.contents 243 248 244 return BlockTranslateNode(extra_context, singular, plural, countervar, counter) 249 return BlockTranslateNode(extra_context, singular, plural, countervar, 250 counter) 245 251 246 252 register.tag('get_available_languages', do_get_available_languages) django/trunk/tests/regressiontests/templates/tests.py
r6680 r6682 706 706 707 707 # simple translation of a variable 708 'i18n03': ('{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}', {'anton': ' xxxyyyxxx'}, "xxxyyyxxx"),708 'i18n03': ('{% load i18n %}{% blocktrans %}{{ anton }}{% endblocktrans %}', {'anton': '\xc3\x85'}, u"Å"), 709 709 710 710 # simple translation of a variable and filter 711 'i18n04': ('{% load i18n %}{% blocktrans with anton|lower as berta %}{{ berta }}{% endblocktrans %}', {'anton': ' XXXYYYXXX'}, "xxxyyyxxx"),711 'i18n04': ('{% load i18n %}{% blocktrans with anton|lower as berta %}{{ berta }}{% endblocktrans %}', {'anton': '\xc3\x85'}, u'å'), 712 712 713 713 # simple translation of a string with interpolation … … 740 740 'i18n15': ('{{ absent|default:_("Password") }}', {'LANGUAGE_CODE': 'de', 'absent': ""}, 'Passwort'), 741 741 'i18n16': ('{{ _("<") }}', {'LANGUAGE_CODE': 'de'}, '<'), 742 743 # Escaping inside blocktrans works as if it was directly in the 744 # template. 745 'i18n17': ('{% load i18n %}{% blocktrans with anton|escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'), 746 'i18n18': ('{% load i18n %}{% blocktrans with anton|force_escape as berta %}{{ berta }}{% endblocktrans %}', {'anton': 'α & β'}, u'α & β'), 742 747 743 748 ### HANDLING OF TEMPLATE_STRING_IF_INVALID ###################################
