Django makemessages ignore switch doesn't work for me
Asked Answered
S

2

22

I have problems localizing a django-nonrel project, which is deployed to GAE. Because of GAE I have to put everything into my project folder, so it looks like something like this

project
  + django
  + dbindexer
  + registration
  + myapp
  ...
  + locale
  + templates

I have strings to localize in templates directory, and in the myapp directory. When I run python manage.py makemessages -l en --ignore django\* from the project dir it crawl through all the directories of the project, including django, so I get a quite big po file. My strings from the templates are there, along with all of the strings from django directory.

after --ignore ( or just -i ) I tried to pu django django/* , but nothing changed. Any ideas?

Sheeran answered 27/9, 2011 at 18:58 Comment(0)
S
35

./manage.py help makemessages

-i PATTERN, --ignore=PATTERN
                        Ignore files or directories matching this glob-style
                        pattern. Use multiple times to ignore more.

I have just tested it, and this command successfully ignored my application:

./manage.py makemessages -l da -i "django*"

But beware that before you test it, you should delete the old .po file, as I think it will not automatically remove the translation lines from your previous makemessages execution.

Swearingen answered 27/9, 2011 at 23:6 Comment(0)
G
2

The problem is with the pattern - maybe the shell was expanding it for you.

In general - it is good to avoid path separators (whether / or \) in the pattern.

If you need to always pass specific options to the makemessages command, you could consider your own wrapper, like this one, which I use myself:

from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.management import call_command

class Command(BaseCommand):
    help = "Scan i18n messages without going into externals."

    def handle(self, *args, **options):
        call_command('makemessages',
            all=True,
            extensions=['html', 'inc'],
            ignore_patterns=['externals*'])

This saves you typing, and gives a common entry point for scanning messages across the project (your translator colleague will not destroy translations by missing out some parameter).

Don't delete the old .po file, once you have cleared it from the totally unwanted (i.e. - those from 'django' directory) messages. This allows gettext to recycle old unused messages, once they are used again (or simmilar ones, which will be marked as #, fuzzy.

Edit - as mt4x noted - the wrapper above doesn't allow for passing the options to the wrapped command. This is easy to fix:

from django.core.management import call_command
from django.core.management.commands.makemessages import (
    Command as MakeMessagesCommand
)

class Command(MakeMessagesCommand):
    help = "Scan i18n messages without going into externals."
    
    def handle(self, *args, **options):
        options['all'] = True
        options['extensions'] = ['html', 'inc']

        if 'ignore_patterns' not in options:
            options['ignore_patterns'] = []

        options['ignore_patterns'] += ['externals*']
        call_command('makemessages', **options)

Thus - you can fix what needs to be fixed, and flex the rest. And this needs not be blind override like above, but also some conditional edit of the parameters passed to the command - appending something to a list or only adding it when it's missing.

Galleywest answered 8/1, 2014 at 13:20 Comment(5)
This does not allow to use standard makemessages options, like --domain, --locale and others, without creating more wrappers or adding the options manually. Am I right?Laterite
@Laterite - yes, but there is a simple fix for that. I will update my answer.Galleywest
How does this work with multiple ignore patterns? i=['admin*', 'colorful*', 'css*'] for example?Gutter
@Gutter - yes, just as the command line option --ignore=PATTERN, which can be specified multiple times, to ignore using multiple different patterns.Galleywest
Okay, thanks. The strange thing was, I first tried i='excep1', i='excep2' but that didn't work (cant assign multiple time). But i=['excep1', 'excep2'] did work, thank you! :)Gutter

© 2022 - 2024 — McMap. All rights reserved.