Ticket #14844: 14844-1-wip.diff
File 14844-1-wip.diff, 7.0 KB (added by , 13 years ago) |
---|
-
django/templatetags/i18n.py
diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py
a b 96 96 97 97 class BlockTranslateNode(Node): 98 98 def __init__(self, extra_context, singular, plural=None, countervar=None, 99 counter=None, message_context=None ):99 counter=None, message_context=None, plural_vname_var=None, plural_vname=None): 100 100 self.extra_context = extra_context 101 101 self.singular = singular 102 102 self.plural = plural 103 103 self.countervar = countervar 104 104 self.counter = counter 105 105 self.message_context = message_context 106 self.plural_vname_var = plural_vname_var 107 self.plural_vname = plural_vname 106 108 107 109 def render_token_list(self, tokens): 108 110 result = [] … … 129 131 singular, vars = self.render_token_list(self.singular) 130 132 # Escape all isolated '%' 131 133 singular = re.sub(u'%(?!\()', u'%%', singular) 134 count, plural_vname = None, None 132 135 if self.plural and self.countervar and self.counter: 133 136 count = self.counter.resolve(context) 134 137 context[self.countervar] = count 138 if self.plural_vname_var is not None and self.plural_vname is not None: 139 plural_vname = self.plural_vname.resolve(context) 135 140 plural, plural_vars = self.render_token_list(self.plural) 136 141 plural = re.sub(u'%(?!\()', u'%%', plural) 137 142 if message_context: … … 145 150 result = translation.pgettext(message_context, singular) 146 151 else: 147 152 result = translation.ugettext(singular) 153 if count not in (1, None) and plural_vname is not None: # TODO: Hardcoding 1 isn't right 154 #print 'setting context["%s"] = "%s" to render "%s"' % (self.plural_vname_var, plural_vname, result) 155 context[self.plural_vname_var] = plural_vname 148 156 data = dict([(v, _render_value_in_context(context.get(v, ''), context)) for v in vars]) 149 157 context.pop() 150 158 try: … … 432 440 else: 433 441 message_context = None 434 442 extra_context = options.get('with', {}) 443 plural_vname_var, plural_vname = None, None 435 444 436 445 singular = [] 437 446 plural = [] … … 442 451 else: 443 452 break 444 453 if countervar and counter: 445 if token.contents.strip() != 'plural': 446 raise TemplateSyntaxError("'blocktrans' doesn't allow other block tags inside it") 454 if not token.contents.lstrip().startswith('plural'): 455 raise TemplateSyntaxError("'blocktrans' doesn't allow other block tags inside it (%s)" % token.contents.strip()) 456 else: 457 poptions = {} 458 bits = token.split_contents() 459 remaining_bits = bits[1:] 460 while remaining_bits: 461 option = remaining_bits.pop(0) 462 if option in poptions: 463 raise TemplateSyntaxError('The %r option was specified more ' 464 'than once.' % option) 465 if option == 'with': 466 pvalue = token_kwargs(remaining_bits, parser, support_legacy=False) 467 if len(pvalue) != 1: 468 raise TemplateSyntaxError('"with" in %r section of blocktrans tag accepts only ' 469 'one keyword argument.' % bits[0]) 470 if pvalue.keys()[0] not in options['with']: 471 raise TemplateSyntaxError( 472 'blocktrans: Pluralized verbose name variable ' 473 'specified in plural section should have the same ' 474 'name as the one in the singular section') 475 else: 476 raise TemplateSyntaxError('Unknown argument for %r tag: %r.' % 477 (bits[0], option)) 478 poptions[option] = pvalue 479 plural_vname = poptions.get('with') 480 if 'with' in poptions: 481 plural_vname_var, plural_vname = poptions['with'].items()[0] 447 482 while parser.tokens: 448 483 token = parser.next_token() 449 484 if token.token_type in (TOKEN_VAR, TOKEN_TEXT): … … 454 489 raise TemplateSyntaxError("'blocktrans' doesn't allow other block tags (seen %r) inside it" % token.contents) 455 490 456 491 return BlockTranslateNode(extra_context, singular, plural, countervar, 457 counter, message_context )492 counter, message_context, plural_vname_var, plural_vname) 458 493 459 494 @register.tag 460 495 def language(parser, token): -
tests/regressiontests/templates/tests.py
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
a b 1305 1305 'i18n36': ('{% load i18n %}{% trans "Page not found" as page_not_found noop %}{{ page_not_found }}', {'LANGUAGE_CODE': 'de'}, "Page not found"), 1306 1306 'i18n37': ('{% load i18n %}{% trans "Page not found" as page_not_found %}{% blocktrans %}Error: {{ page_not_found }}{% endblocktrans %}', {'LANGUAGE_CODE': 'de'}, "Error: Seite nicht gefunden"), 1307 1307 1308 # New in 1.4: When the plural form functionality is used with a 1309 # count variable, the plural inner tag can specify one var binding 1310 # that overrides a similarly named one declared in the main tag body 1311 'i18n38': ('{% load i18n %}{% blocktrans with name=singular_name count counter=number %}singular: {{ counter }} {{ name }}{% plural with name=plural_name %}plural: {{ counter }} {{ name }}{% endblocktrans %}', {'number': 1, 'singular_name': 'pony', 'plural_name': 'ponies'}, u"singular: 1 pony"), 1312 'i18n39': ('{% load i18n %}{% blocktrans with name=singular_name count counter=number %}singular: {{ counter }} {{ name }}{% plural with name=plural_name %}plural: {{ counter }} {{ name }}{% endblocktrans %}', {'number': 2, 'singular_name': 'pony', 'plural_name': 'ponies'}, u"plural: 2 ponies"), 1313 'i18n40': ('{% load i18n %}{% blocktrans with name=singular_name count counter=number %}singular: {{ counter }} {{ name }}{% plural with name=plural_name foo="bar" %}plural: {{ counter }} {{ name }}{% endblocktrans %}', {'number': 2, 'singular_name': 'pony', 'plural_name': 'ponies'}, template.TemplateSyntaxError), 1314 'i18n41': ('{% load i18n %}{% blocktrans with name=singular_name %}singular: {{ counter }} {{ name }}{% plural with name=plural_name %}plural: {{ counter }} {{ name }}{% endblocktrans %}', {'number': 2, 'singular_name': 'pony', 'plural_name': 'ponies'}, template.TemplateSyntaxError), 1315 'i18n42': ('{% load i18n %}{% blocktrans with name=singular_name count counter=number %}singular: {{ counter }} {{ name}}{% plural foo %}plural: {{ counter }} {{ name }}{% endblocktrans %}', {'number': 2, 'singular_name': 'pony'}, template.TemplateSyntaxError), 1316 1308 1317 ### HANDLING OF TEMPLATE_STRING_IF_INVALID ################################### 1309 1318 1310 1319 'invalidstr01': ('{{ var|default:"Foo" }}', {}, ('Foo','INVALID')),