Ticket #5849: blocktrans_strip.diff

File blocktrans_strip.diff, 3.4 KB (added by Dmitri Fedortchenko <zeraien@…>, 16 years ago)

Patch that adds stripping of indentation from contents of blocktrans block.

  • django/utils/translation/trans_real.py

     
    443443    inplural = False
    444444    singular = []
    445445    plural = []
     446   
     447    from django.utils.translation import strip_blocktrans
     448           
    446449    for t in Lexer(src, None).tokenize():
    447450        if intrans:
    448451            if t.token_type == TOKEN_BLOCK:
    449452                endbmatch = endblock_re.match(t.contents)
    450453                pluralmatch = plural_re.match(t.contents)
    451454                if endbmatch:
     455                    singular = strip_blocktrans(''.join(singular))
     456                   
    452457                    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))
    454460                        for part in singular:
    455461                            out.write(blankout(part, 'S'))
    456462                        for part in plural:
    457463                            out.write(blankout(part, 'P'))
    458464                    else:
    459                         out.write(' gettext(%r) ' % ''.join(singular))
     465                        out.write(' gettext(%r) ' % singular)
    460466                        for part in singular:
    461467                            out.write(blankout(part, 'S'))
    462468                    intrans = False
  • django/utils/translation/__init__.py

     
    109109    """
    110110    return u''.join([force_unicode(s) for s in strings])
    111111string_concat = lazy(string_concat, unicode)
     112
     113import os,re
     114def 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

     
    6666        for var,val in self.extra_context.items():
    6767            context[var] = val.resolve(context)
    6868        singular = self.render_token_list(self.singular)
     69        singular = translation.strip_blocktrans(singular)
    6970        if self.plural and self.countervar and self.counter:
    7071            count = self.counter.resolve(context)
    7172            context[self.countervar] = count
    7273            plural = self.render_token_list(self.plural)
     74            plural = translation.strip_blocktrans(plural)
    7375            result = translation.ungettext(singular, plural, count)
    7476        else:
    7577            result = translation.ugettext(singular)
Back to Top