Ticket #5463: t5463-r7720.diff

File t5463-r7720.diff, 8.9 KB (added by Ramiro Morales, 16 years ago)
  • django/bin/make-messages.py

    diff -r 81d089f1a551 django/bin/make-messages.py
    a b  
    55import optparse
    66
    77from django.core.management.base import CommandError
    8 from django.core.management.commands.makemessages import make_messages
     8from django.core.management.commands.makemessages import make_messages, handle_extensions
    99from django.core.management.color import color_style
    1010
    1111def main():
     
    1818    parser.add_option('-v', '--verbose', action='store_true', dest='verbose',
    1919        default=False, help='Verbosity output')
    2020    parser.add_option('-a', '--all', action='store_true', dest='all',
    21         default=False, help='Reexamines all source code and templates for \
    22             new translation strings and updates all message files for all \
    23             available languages.')
     21        default=False, help='Reexamines all source code and templates for '
     22            'new translation strings and updates all message files for all '
     23            'available languages.')
     24    parser.add_option('-e', '--extension', action='append', dest='extensions',
     25        help='The file extension(s) to examine (default: ".html", '
     26            'separate multiple extensions with commas, or use -e multiple '
     27            'times).')
    2428
    2529    options, args = parser.parse_args()
    2630    if len(args):
     
    2933        verbosity = 2
    3034    else:
    3135        verbosity = 1
    32    
     36    if options.extensions is None:
     37        options.extensions = []
     38
     39    extensions = handle_extensions(options.extensions)
    3340    try:
    3441        make_messages(locale=options.locale, domain=options.domain,
    35             verbosity=verbosity, all=options.all)
     42            verbosity=verbosity, all=options.all, extensions=extensions)
    3643    except CommandError, e:
    3744        style = color_style()
    3845        sys.stderr.write(style.ERROR(str('Error: %s\n' % e)))
  • django/core/management/commands/makemessages.py

    diff -r 81d089f1a551 django/core/management/commands/makemessages.py
    a b  
    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    >>> handle_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=None):
    1142    """
    1243    Uses the locale directory from the Django SVN tree or an application/
    13     project to process all 
     44    project to process all
    1445    """
    1546    # Need to ensure that the i18n framework is enabled
    1647    from django.conf import settings
    1748    settings.configure(USE_I18N = True)
    18    
     49
    1950    from django.utils.translation import templatize
    2051
    2152    if os.path.isdir(os.path.join('conf', 'locale')):
     
    2455        localedir = os.path.abspath('locale')
    2556    else:
    2657        raise CommandError("This script should be run from the Django SVN tree or your project or app tree. If you did indeed run it from the SVN checkout or your project or application, maybe you are just missing the conf/locale (in the django tree) or locale (for project and application) directory? It is not created automatically, you have to create it by hand if you want to enable i18n for your project or application.")
    27    
     58
    2859    if domain not in ('django', 'djangojs'):
    2960        raise CommandError("currently makemessages only supports domains 'django' and 'djangojs'")
    3061
     
    3566        else:
    3667            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
    3768        raise CommandError(message)
     69
     70    if not extensions:
     71        extensions = ['.html']
    3872
    3973    languages = []
    4074    if locale is not None:
     
    6094            all_files.extend([(dirpath, f) for f in filenames])
    6195        all_files.sort()
    6296        for dirpath, file in all_files:
    63             if domain == 'djangojs' and file.endswith('.js'):
     97            file_base, file_ext = os.path.splitext(file)
     98            if domain == 'djangojs' and file_ext == '.js':
    6499                if verbosity > 1:
    65100                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
    66101                src = open(os.path.join(dirpath, file), "rb").read()
     
    84119                if msgs:
    85120                    open(potfile, 'ab').write(msgs)
    86121                os.unlink(os.path.join(dirpath, thefile))
    87             elif domain == 'django' and (file.endswith('.py') or file.endswith('.html')):
     122            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
    88123                thefile = file
    89                 if file.endswith('.html'):
     124                if file_ext in extensions:
    90125                    src = open(os.path.join(dirpath, file), "rb").read()
    91126                    thefile = '%s.py' % file
    92127                    open(os.path.join(dirpath, thefile), "wb").write(templatize(src))
     
    141176            help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
    142177        make_option('--all', '-a', action='store_true', dest='all',
    143178            default=False, help='Reexamines all source code and templates for new translation strings and updates all message files for all available languages.'),
     179        make_option('--extension', '-e', default=None, dest='extensions',
     180            help='The file extension(s) to examine (default: ".html", separate multiple extensions with commas, or use -e multiple times)',
     181            action='append'),
    144182    )
    145183    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."
    146184
     
    155193        domain = options.get('domain')
    156194        verbosity = int(options.get('verbosity'))
    157195        process_all = options.get('all')
     196        extensions = options.get('extensions') or []
    158197
    159         make_messages(locale, domain, verbosity, process_all)
     198        extensions = handle_extensions(extensions)
     199        make_messages(locale, domain, verbosity, process_all, extensions)
  • docs/django-admin.txt

    diff -r 81d089f1a551 docs/django-admin.txt
    a b  
    411411Example usage::
    412412
    413413    django-admin.py makemessages --all
     414
     415--extension
     416~~~~~~~~~~~
     417
     418Use the ``--extension`` or ``-e`` option to specify a list of file extensions
     419to examine (default: ".html").
     420
     421Example usage::
     422
     423    django-admin.py makemessages --locale=de --extension xhtml
     424
     425Separate multiple extensions with commas or use -e/--extension multiple times::
     426
     427    django-admin.py makemessages --locale=de --extension=tpl,xml --extension=txt
    414428
    415429--locale
    416430~~~~~~~~
  • docs/man/django-admin.1

    diff -r 81d089f1a551 docs/man/django-admin.1
    a b  
    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"
Back to Top