Ticket #5849: blocktrans_strip.2.diff
File blocktrans_strip.2.diff, 3.4 KB (added by , 17 years ago) |
---|
-
django/templatetags/i18n.py
70 72 for var, val in self.extra_context.items(): 71 73 context[var] = val.render(context) 72 74 singular, vars = self.render_token_list(self.singular) 75 singular = translation.strip_blocktrans(singular) 73 76 if self.plural and self.countervar and self.counter: 74 77 count = self.counter.resolve(context) 75 78 context[self.countervar] = count 76 79 plural, vars = self.render_token_list(self.plural) 80 plural = translation.strip_blocktrans(plural) 77 81 result = translation.ungettext(singular, plural, count) 78 82 else: 79 83 result = translation.ugettext(singular) -
django/utils/translation/trans_real.py
448 448 inplural = False 449 449 singular = [] 450 450 plural = [] 451 452 from django.utils.translation import strip_blocktrans 453 451 454 for t in Lexer(src, None).tokenize(): 452 455 if intrans: 453 456 if t.token_type == TOKEN_BLOCK: 454 457 endbmatch = endblock_re.match(t.contents) 455 458 pluralmatch = plural_re.match(t.contents) 456 459 if endbmatch: 460 singular = strip_blocktrans(''.join(singular)) 461 457 462 if inplural: 458 out.write(' ngettext(%r,%r,count) ' % (''.join(singular), ''.join(plural))) 463 plural = strip_blocktrans(''.join(plural)) 464 out.write(' ngettext(%r,%r,count) ' % (singular, plural)) 459 465 for part in singular: 460 466 out.write(blankout(part, 'S')) 461 467 for part in plural: 462 468 out.write(blankout(part, 'P')) 463 469 else: 464 out.write(' gettext(%r) ' % ''.join(singular))470 out.write(' gettext(%r) ' % singular) 465 471 for part in singular: 466 472 out.write(blankout(part, 'S')) 467 473 intrans = False -
django/utils/translation/__init__.py
109 109 """ 110 110 return u''.join([force_unicode(s) for s in strings]) 111 111 string_concat = lazy(string_concat, unicode) 112 113 import os,re 114 def strip_blocktrans(text): 115 """Strip indentation from the argument based on the indentation of the first non-empty line. 116 117 The returned value is stripped of trailing linebreaks and spaces.""" 118 119 lines = text.split(os.linesep) 120 newlines = [] 121 122 firstline = lines[0] 123 if not re.match("\w",firstline) and len(lines) > 1: 124 firstline = lines[1] 125 126 thematch = re.match('^[\t\s]+',firstline) 127 if thematch: 128 pos = thematch.end() 129 130 for line in lines: 131 newlines.append(re.sub("^[\t\s]{0,%d}"%pos,"",line)) 132 return os.linesep.join(newlines).strip() 133 else: 134 return text.strip()