Ticket #5463: t5463-r7844.diff

File t5463-r7844.diff, 7.2 KB (added by Jannis Leidel, 16 years ago)

Updated to trunk after merge of #5522, added section to i18n.txt

  • 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'):
    1116    """
     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    >>> handle_extensions(['.html, txt,.tpl'])
     26    ['.html', '.tpl', '.txt']
     27    """
     28    ext_list = []
     29    for ext in extensions:
     30        ext_list += ext.lower().replace(' ', '').split(',')
     31    for i, ext in enumerate(ext_list):
     32        if not ext.startswith('.'):
     33            ext_list[i] = '.%s' % ext_list[i]
     34
     35    # we don't want *.py files here because of the way non-*.py files
     36    # are handled in make_messages() (they are copied to file.ext.py files to
     37    # trick xgettext to parse them as python files)
     38    extensions = list(set([x for x in ext_list if not x == ('.py')]))
     39    if not extensions:
     40        return [default]
     41    return extensions
     42
     43def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=None):
     44    """
    1245    Uses the locale directory from the Django SVN tree or an application/
    13     project to process all 
     46    project to process all
    1447    """
    1548    # Need to ensure that the i18n framework is enabled
    1649    from django.conf import settings
     
    3669            message = "usage: make-messages.py -l <language>\n   or: make-messages.py -a\n"
    3770        raise CommandError(message)
    3871
     72    if not extensions:
     73        extensions = ['.html']
     74
    3975    languages = []
    4076    if locale is not None:
    4177        languages.append(locale)
     
    6096            all_files.extend([(dirpath, f) for f in filenames])
    6197        all_files.sort()
    6298        for dirpath, file in all_files:
    63             if domain == 'djangojs' and file.endswith('.js'):
     99            file_base, file_ext = os.path.splitext(file)
     100            if domain == 'djangojs' and file_ext == '.js':
    64101                if verbosity > 1:
    65102                    sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
    66103                src = open(os.path.join(dirpath, file), "rb").read()
     
    84121                if msgs:
    85122                    open(potfile, 'ab').write(msgs)
    86123                os.unlink(os.path.join(dirpath, thefile))
    87             elif domain == 'django' and (file.endswith('.py') or file.endswith('.html')):
     124            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
    88125                thefile = file
    89                 if file.endswith('.html'):
     126                if file_ext in extensions:
    90127                    src = open(os.path.join(dirpath, file), "rb").read()
    91128                    thefile = '%s.py' % file
    92129                    open(os.path.join(dirpath, thefile), "wb").write(templatize(src))
     
    141178            help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
    142179        make_option('--all', '-a', action='store_true', dest='all',
    143180            default=False, help='Reexamines all source code and templates for new translation strings and updates all message files for all available languages.'),
     181        make_option('--extension', '-e', default=None, dest='extensions',
     182            help='The file extension(s) to examine (default: ".html", separate multiple extensions with commas, or use -e multiple times)',
     183            action='append'),
    144184    )
    145185    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."
    146186
     
    155195        domain = options.get('domain')
    156196        verbosity = int(options.get('verbosity'))
    157197        process_all = options.get('all')
     198        extensions = options.get('extensions') or []
    158199
    159         make_messages(locale, domain, verbosity, process_all)
     200        extensions = handle_extensions(extensions)
     201        make_messages(locale, domain, verbosity, process_all, extensions)
  • 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"
  • docs/i18n.txt

     
    426426do the same, but the location of the locale directory is ``locale/LANG/LC_MESSAGES``
    427427(note the missing ``conf`` prefix).
    428428
     429By default ``django-admin.py makemessages`` examines every file that has the
     430``.html`` file extension. In case you want to override that default, use the
     431``--extension`` or ``-e`` option to specify the file extensions to examine::
     432
     433    django-admin.py makemessages -l de -e txt
     434
     435Separate multiple extensions with commas and/or use ``-e`` or ``--extension`` multiple times::
     436
     437    django-admin.py makemessages -l=de -e=html,txt -e xml
     438
    429439.. admonition:: No gettext?
    430440
    431441    If you don't have the ``gettext`` utilities installed,
  • docs/django-admin.txt

     
    412412
    413413    django-admin.py makemessages --all
    414414
     415
     416--extension
     417~~~~~~~~~~~
     418
     419Use the ``--extension`` or ``-e`` option to specify a list of file extensions
     420to examine (default: ".html").
     421
     422Example usage::
     423
     424    django-admin.py makemessages --locale=de --extension xhtml
     425
     426Separate multiple extensions with commas or use -e or --extension multiple times::
     427
     428    django-admin.py makemessages --locale=de --extension=html,txt --extension xml
     429
    415430--locale
    416431~~~~~~~~
    417432
Back to Top