Ticket #6106: 6106-1.diff

File 6106-1.diff, 2.0 KB (added by Ramiro Morales, 15 years ago)

Patch that preserves the POT-Creation-Date header of a .po file when makemessages applies the msguniq/msmerge process

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

    diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py
    a b  
    1010from django.core.management.base import CommandError, BaseCommand
    1111
    1212pythonize_re = re.compile(r'\n\s*//')
     13pot_timestamp_re = re.compile(r'^"POT-Creation-Date:[^\\]+', re.MULTILINE)
    1314
    1415def handle_extensions(extensions=('html',)):
    1516    """
     
    3637    # trick xgettext to parse them as Python files)
    3738    return set([x for x in ext_list if x != '.py'])
    3839
     40def get_pot_timestamp(pofname):
     41    """
     42    Extracts the POT-Creation-Date header line from .po file pofname.
     43    """
     44    if not os.path.exists(pofname):
     45        return None
     46    tstamp = None
     47    pof = open(pofname, "rU")
     48    try:
     49        for c, line in enumerate(pof):
     50            if not line or c == 30:
     51                break
     52            m = pot_timestamp_re.search(line)
     53            if m:
     54                tstamp = m.group(0)
     55                break
     56    finally:
     57        pof.close()
     58    return tstamp
     59
    3960def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=None):
    4061    """
    4162    Uses the locale directory from the Django SVN tree or an application/
     
    93114        pofile = os.path.join(basedir, '%s.po' % domain)
    94115        potfile = os.path.join(basedir, '%s.pot' % domain)
    95116
     117        # If there is a .po file already, get its POT creation timestamp header
     118        pot_timestamp = get_pot_timestamp(pofile)
     119
    96120        if os.path.exists(potfile):
    97121            os.unlink(potfile)
    98122
     
    173197                errors = p.stderr.read()
    174198                if errors:
    175199                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
     200                if pot_timestamp:
     201                    # Put back the POT creation timestamp we preserved
     202                    msgs = pot_timestamp_re.sub(pot_timestamp, msgs)
    176203            open(pofile, 'wb').write(msgs)
    177204            os.unlink(potfile)
    178205
Back to Top