Ticket #5849: blocktrans_strip.diff
File blocktrans_strip.diff, 3.4 KB (added by , 17 years ago) |
---|
-
django/utils/translation/trans_real.py
443 443 inplural = False 444 444 singular = [] 445 445 plural = [] 446 447 from django.utils.translation import strip_blocktrans 448 446 449 for t in Lexer(src, None).tokenize(): 447 450 if intrans: 448 451 if t.token_type == TOKEN_BLOCK: 449 452 endbmatch = endblock_re.match(t.contents) 450 453 pluralmatch = plural_re.match(t.contents) 451 454 if endbmatch: 455 singular = strip_blocktrans(''.join(singular)) 456 452 457 if inplural: 453 out.write(' ngettext(%r,%r,count) ' % (''.join(singular), ''.join(plural))) 458 plural = strip_blocktrans(''.join(plural)) 459 out.write(' ngettext(%r,%r,count) ' % (singular, plural)) 454 460 for part in singular: 455 461 out.write(blankout(part, 'S')) 456 462 for part in plural: 457 463 out.write(blankout(part, 'P')) 458 464 else: 459 out.write(' gettext(%r) ' % ''.join(singular))465 out.write(' gettext(%r) ' % singular) 460 466 for part in singular: 461 467 out.write(blankout(part, 'S')) 462 468 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() -
django/templatetags/i18n.py
66 66 for var,val in self.extra_context.items(): 67 67 context[var] = val.resolve(context) 68 68 singular = self.render_token_list(self.singular) 69 singular = translation.strip_blocktrans(singular) 69 70 if self.plural and self.countervar and self.counter: 70 71 count = self.counter.resolve(context) 71 72 context[self.countervar] = count 72 73 plural = self.render_token_list(self.plural) 74 plural = translation.strip_blocktrans(plural) 73 75 result = translation.ungettext(singular, plural, count) 74 76 else: 75 77 result = translation.ugettext(singular)