Ticket #12201: 12201-2.diff

File 12201-2.diff, 6.2 KB (added by Ramiro Morales, 15 years ago)

A slightly enhanced patch

  • django/core/management/commands/makemessages.py

    diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py
    a b  
    128128                os.unlink(os.path.join(dirpath, thefile))
    129129            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
    130130                thefile = file
     131                orig_file = os.path.join(dirpath, file)
    131132                if file_ext in extensions:
    132                     src = open(os.path.join(dirpath, file), "rU").read()
     133                    src = open(orig_file, "rU").read()
    133134                    thefile = '%s.py' % file
    134                     try:
    135                         open(os.path.join(dirpath, thefile), "w").write(templatize(src))
    136                     except SyntaxError, msg:
    137                         msg = "%s (file: %s)" % (msg, os.path.join(dirpath, file))
    138                         raise SyntaxError(msg)
     135                    open(os.path.join(dirpath, thefile), "w").write(templatize(src, orig_file[2:]))
    139136                if verbosity > 1:
    140137                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
    141138                cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --from-code UTF-8 -o - "%s"' % (
     
    148145
    149146                if thefile != file:
    150147                    old = '#: '+os.path.join(dirpath, thefile)[2:]
    151                     new = '#: '+os.path.join(dirpath, file)[2:]
     148                    new = '#: '+orig_file[2:]
    152149                    msgs = msgs.replace(old, new)
    153150                if os.path.exists(potfile):
    154151                    # Strip the header
  • django/template/__init__.py

    diff --git a/django/template/__init__.py b/django/template/__init__.py
    a b  
    193193    else:
    194194        lexer_class, parser_class = Lexer, Parser
    195195    lexer = lexer_class(template_string, origin)
    196     parser = parser_class(lexer.tokenize())
     196    tokens = lexer.tokenize()
     197    tokens = zip(*tokens)
     198    if tokens:
     199        tokens = tokens[0]
     200    parser = parser_class(list(tokens))
    197201    return parser.parse()
    198202
    199203class Token(object):
     
    225229    def __init__(self, template_string, origin):
    226230        self.template_string = template_string
    227231        self.origin = origin
     232        self.lineno = 1
    228233
    229234    def tokenize(self):
    230235        "Return a list of tokens from a given template_string."
     
    232237        result = []
    233238        for bit in tag_re.split(self.template_string):
    234239            if bit:
    235                 result.append(self.create_token(bit, in_tag))
     240                result.append((self.create_token(bit, in_tag), self.lineno))
    236241            in_tag = not in_tag
    237242        return result
    238243
     
    251256                token = Token(TOKEN_COMMENT, '')
    252257        else:
    253258            token = Token(TOKEN_TEXT, token_string)
     259        self.lineno += token_string.count('\n')
    254260        return token
    255261
    256262class Parser(object):
  • django/template/debug.py

    diff --git a/django/template/debug.py b/django/template/debug.py
    a b  
    1414        for match in tag_re.finditer(self.template_string):
    1515            start, end = match.span()
    1616            if start > upto:
    17                 result.append(self.create_token(self.template_string[upto:start], (upto, start), False))
     17                result.append((self.create_token(self.template_string[upto:start], (upto, start), False), None))
    1818                upto = start
    19             result.append(self.create_token(self.template_string[start:end], (start, end), True))
     19            result.append((self.create_token(self.template_string[start:end], (start, end), True), None))
    2020            upto = end
    2121        last_bit = self.template_string[upto:]
    2222        if last_bit:
    23             result.append(self.create_token(last_bit, (upto, upto + len(last_bit)), False))
     23            result.append((self.create_token(last_bit, (upto, upto + len(last_bit)), False), None))
    2424        return result
    2525
    2626    def create_token(self, token_string, source, in_tag):
  • django/utils/translation/__init__.py

    diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
    a b  
    9696def get_language_from_request(request):
    9797    return real_get_language_from_request(request)
    9898
    99 def templatize(src):
    100     return real_templatize(src)
     99def templatize(src, origin=None):
     100    return real_templatize(src, origin)
    101101
    102102def deactivate_all():
    103103    return real_deactivate_all()
  • django/utils/translation/trans_real.py

    diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
    a b  
    244244    True = right-to-left layout
    245245    """
    246246    from django.conf import settings
    247    
     247
    248248    base_lang = get_language().split('-')[0]
    249249    return base_lang in settings.LANGUAGES_BIDI
    250250
     
    408408plural_re = re.compile(r"""^\s*plural$""")
    409409constant_re = re.compile(r"""_\(((?:".*?")|(?:'.*?'))\)""")
    410410
    411 def templatize(src):
     411def templatize(src, origin=None):
    412412    """
    413413    Turns a Django template into something that is understood by xgettext. It
    414414    does so by translating the Django translation tags into standard gettext
     
    420420    inplural = False
    421421    singular = []
    422422    plural = []
    423     for t in Lexer(src, None).tokenize():
     423    for t, lineno in Lexer(src, origin).tokenize():
    424424        if intrans:
    425425            if t.token_type == TOKEN_BLOCK:
    426426                endbmatch = endblock_re.match(t.contents)
     
    443443                elif pluralmatch:
    444444                    inplural = True
    445445                else:
    446                     raise SyntaxError("Translation blocks must not include other block tags: %s" % t.contents)
     446                    filemsg = ''
     447                    if origin:
     448                        filemsg = 'file %s, ' % origin
     449                    raise SyntaxError("Translation blocks must not include other block tags: %s (%sline %d)" % (t.contents, filemsg, lineno))
    447450            elif t.token_type == TOKEN_VAR:
    448451                if inplural:
    449452                    plural.append('%%(%s)s' % t.contents)
Back to Top