Ticket #6505: 6505-1.diff

File 6505-1.diff, 2.6 KB (added by Ramiro Morales, 14 years ago)

Patch that implements copying the pluiral forms .po file header from the Django one for the same locale if it exists to newly created .po files

  • 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*//')
     13plural_forms_re = re.compile(r'^(?P<value>"Plural-Forms.+?\\n")\s*$', re.MULTILINE | re.DOTALL)
    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 copy_django_plural_forms(msgs, loc, domain):
     41    """
     42    Copies plural forms header contents from a Django catalog of locale loc to
     43    the msgs string, inserting it at the right place. msgs should be the
     44    contents of a newly created .po file.
     45    """
     46    django_dir = os.path.normpath(os.path.join(os.path.dirname(__file__), '../../..'))
     47    if domain == 'djangojs':
     48        domains = ('djangojs', 'django')
     49    else:
     50        domains = ('django',)
     51    for domain in domains:
     52        django_po = os.path.join(django_dir, 'conf/locale/%s/LC_MESSAGES/%s.po' % (loc, domain))
     53        if os.path.exists(django_po):
     54            m = plural_forms_re.search(open(django_po, 'rU').read())
     55            if m:
     56                #sys.stderr.write("value: %s\n" % m.group('value'))
     57                lines = []
     58                seen = False
     59                for line in msgs.split('\n'):
     60                    if not line and not seen:
     61                        line = '%s\n' % m.group('value')
     62                        seen = True
     63                    lines.append(line)
     64                msgs = '\n'.join(lines)
     65                break
     66    return msgs
     67
    3968def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=None):
    4069    """
    4170    Uses the locale directory from the Django SVN tree or an application/
     
    5079
    5180    from django.utils.translation import templatize
    5281
     82    invoked_for_django = False
    5383    if os.path.isdir(os.path.join('conf', 'locale')):
    5484        localedir = os.path.abspath(os.path.join('conf', 'locale'))
     85        invoked_for_django = True
    5586    elif os.path.isdir('locale'):
    5687        localedir = os.path.abspath('locale')
    5788    else:
     
    173204                errors = p.stderr.read()
    174205                if errors:
    175206                    raise CommandError("errors happened while running msgmerge\n%s" % errors)
     207            elif not invoked_for_django:
     208                msgs = copy_django_plural_forms(msgs, locale, domain)
    176209            open(pofile, 'wb').write(msgs)
    177210            os.unlink(potfile)
    178211
Back to Top