Ticket #5463: make-messages.1.diff

File make-messages.1.diff, 7.2 KB (added by Jannis Leidel, 16 years ago)

This is a patch that depends on the changes made in patch 6 of ticket #5522 and adds a --extension option to the new djangoadmin.py makemessages tool, as discussed with jacob. ".html" stays the default. fully backwards-compatible and documented.

  • django/bin/make-messages.py

     
    22
    33import os
    44import optparse
    5 from django.core.management.commands.makemessages import make_messages
     5from django.core.management.commands.makemessages import make_messages, handle_extensions
    66
    77def main():
    88    parser = optparse.OptionParser()
     
    1717        default=False, help='Reexamines all source code and templates for \
    1818            new translation strings and updates all message files for all \
    1919            available languages.')
     20    parser.add_option('-e', '--extension', default=[], dest='extensions',
     21        help='The file extension(s) to examine (default: ".html", separate multiple extensions with commas, or use -e multiple times)',
     22        action='append'),
    2023
    2124    options, args = parser.parse_args()
    2225    if len(args):
     
    2528        verbosity = 2
    2629    else:
    2730        verbosity = 1
    28    
     31
     32    extensions = handle_extensions(options.extensions)
     33    print extensions
    2934    make_messages(locale=options.locale, domain=options.domain,
    30         verbosity=verbosity, all=options.all)
     35        verbosity=verbosity, all=options.all, extensions=extensions)
    3136
    3237if __name__ == "__main__":
    3338    main()
  • docs/django-admin.txt

     
     
    390390
    391391    django-admin.py makemessages --all
    392392
     393--extension
     394~~~~~~~~~~~
     395
     396Use the ``--extension`` or ``-e`` option to specify a list of file extensions
     397to examine (default: ".html").
     398
     399Example usage::
     400
     401    django-admin.py makemessages --locale=de --extension xhtml
     402
     403Separate multiple extensions with commas or use -e/--extension multiple times::
     404
     405    django-admin.py makemessages --locale=de --extension=tpl,xml --extension=txt
     406
    393407--locale
    394408~~~~~~~~
    395409
  • docs/man/django-admin.1

     
    4949.B sqlall
    5050for the given app(s) in the current database.
    5151.TP
    52 .BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-all" "]"
     52.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "]"
    5353Runs over the entire source tree of the current directory and pulls out all
    5454strings marked for translation. It creates (or updates) a message file in the
    5555conf/locale (in the django tree) or locale (for project and application) directory.
     
    154154.I \-d, \-\-domain=DOMAIN
    155155The domain of the message files (default: "django") when using makemessages.
    156156.TP
     157.I \-e, \-\-extension=EXTENSION
     158The file extension(s) to examine (default: ".html", separate multiple
     159extensions with commas, or use -e multiple times).
     160.TP
    157161.I \-a, \-\-all
    158162Process all available locales when using makemessages.
    159163.SH "ENVIRONMENT"
  • django/core/management/commands/makemessages.py

     
     
    55from optparse import make_option
    66from django.core.management.base import CommandError, BaseCommand
    77
     8try:
     9    set
     10except NameError:
     11    from sets import Set as set     # For Python 2.3
     12
    813pythonize_re = re.compile(r'\n\s*//')
    914
    10 def make_messages(locale=None, domain='django', verbosity='1', all=False):
     15def handle_extensions(extensions, default='.html'):
     16    """
     17    organizes multiple extensions that are separated with commas or passed by
     18    using --extension/-e multiple times.
     19   
     20    for example: running 'django-admin makemessages -e js,txt -e xhtml -a'
     21    would result in a extension list: ['.js', '.txt', '.xhtml']
     22   
     23    >>> organize_extensions(['.html', 'html,js,py,py,py,.py', 'py,.py'])
     24    ['.html', '.js']
     25    """
     26    ext_list = []
     27    for ext in extensions:
     28        ext_list += ext.lower().split(',')
     29    for i, ext in enumerate(ext_list):
     30        if not ext.startswith('.'):
     31            ext_list[i] = '.%s' % ext_list[i]
     32
     33    # we don't want *.py files here because of the way non-*.py files
     34    # are handled in make_messages() (they are copied to file.ext.py files to
     35    # trick xgettext to parse them as python files)
     36    extensions = list(set([x for x in ext_list if not x == ('.py')]))
     37    if not extensions:
     38        return [default]
     39    return extensions
     40
     41def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=['.html']):
    1142    """
    1243    Uses the locale directory from the Django SVN tree or an application/
    1344    project to process all
     
    6192            all_files.extend([(dirpath, f) for f in filenames])
    6293        all_files.sort()
    6394        for dirpath, file in all_files:
    64             if domain == 'djangojs' and file.endswith('.js'):
     95            file_base, file_ext = os.path.splitext(file)
     96            if domain == 'djangojs' and file_ext == '.js':
    6597                if verbosity > 1:
    6698                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
    6799                src = open(os.path.join(dirpath, file), "rb").read()
     
    87119                if msgs:
    88120                    open(potfile, 'ab').write(msgs)
    89121                os.unlink(os.path.join(dirpath, thefile))
    90             elif domain == 'django' and (file.endswith('.py') or file.endswith('.html')):
     122            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
    91123                thefile = file
    92                 if file.endswith('.html'):
     124                if file_ext in extensions:
    93125                    src = open(os.path.join(dirpath, file), "rb").read()
    94126                    thefile = '%s.py' % file
    95127                    open(os.path.join(dirpath, thefile), "wb").write(templatize(src))
     
    150182            help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
    151183        make_option('--all', '-a', action='store_true', dest='all',
    152184            default=False, help='Reexamines all source code and templates for new translation strings and updates all message files for all available languages.'),
     185        make_option('--extension', '-e', default=[], dest='extensions',
     186            help='The file extension(s) to examine (default: ".html", separate multiple extensions with commas, or use -e multiple times)',
     187            action='append'),
    153188    )
    154189    help = "Runs over the entire source tree of the current directory and pulls out all strings marked for translation. It creates (or updates) a message file in the conf/locale (in the django tree) or locale (for project and application) directory."
    155190
     
    164199        domain = options.get('domain')
    165200        verbosity = int(options.get('verbosity'))
    166201        process_all = options.get('all')
    167 
    168         make_messages(locale, domain, verbosity, process_all)
     202        extensions = options.get('extensions')
     203       
     204        extensions = organize_extensions(extensions)
     205        make_messages(locale, domain, verbosity, process_all, extensions)
Back to Top