From 3735f276603b0c040f3f8aaca64f151397d2cb9b Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Fri, 8 Aug 2008 16:41:55 +0000 Subject: [PATCH] Fixed #5463 -- Allow alternate file extensions on files that are translated. Patch from Jannis Leidel. git-svn-id: http://code.djangoproject.com/svn/django/trunk@8234 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- .../core/management/commands/makemessages.py | 53 +++++++++++++++++-- docs/django-admin.txt | 14 +++++ docs/i18n.txt | 15 ++++++ docs/man/django-admin.1 | 6 ++- 4 files changed, 82 insertions(+), 6 deletions(-) diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index 5be4b7adba..aaa99eed9f 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -5,9 +5,39 @@ from itertools import dropwhile from optparse import make_option from django.core.management.base import CommandError, BaseCommand +try: + set +except NameError: + from sets import Set as set # For Python 2.3 + pythonize_re = re.compile(r'\n\s*//') -def make_messages(locale=None, domain='django', verbosity='1', all=False): +def handle_extensions(extensions=('html',)): + """ + organizes multiple extensions that are separated with commas or passed by + using --extension/-e multiple times. + + for example: running 'django-admin makemessages -e js,txt -e xhtml -a' + would result in a extension list: ['.js', '.txt', '.xhtml'] + + >>> handle_extensions(['.html', 'html,js,py,py,py,.py', 'py,.py']) + ['.html', '.js'] + >>> handle_extensions(['.html, txt,.tpl']) + ['.html', '.tpl', '.txt'] + """ + ext_list = [] + for ext in extensions: + ext_list.extend(ext.replace(' ','').split(',')) + for i, ext in enumerate(ext_list): + if not ext.startswith('.'): + ext_list[i] = '.%s' % ext_list[i] + + # we don't want *.py files here because of the way non-*.py files + # are handled in make_messages() (they are copied to file.ext.py files to + # trick xgettext to parse them as Python files) + return set([x for x in ext_list if x != '.py']) + +def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=None): """ Uses the locale directory from the Django SVN tree or an application/ project to process all @@ -63,7 +93,8 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False): all_files.extend([(dirpath, f) for f in filenames]) all_files.sort() for dirpath, file in all_files: - if domain == 'djangojs' and file.endswith('.js'): + file_base, file_ext = os.path.splitext(file) + if domain == 'djangojs' and file_ext == '.js': if verbosity > 1: sys.stdout.write('processing file %s in %s\n' % (file, dirpath)) src = open(os.path.join(dirpath, file), "rb").read() @@ -87,9 +118,9 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False): if msgs: open(potfile, 'ab').write(msgs) os.unlink(os.path.join(dirpath, thefile)) - elif domain == 'django' and (file.endswith('.py') or file.endswith('.html')): + elif domain == 'django' and (file_ext == '.py' or file_ext in extensions): thefile = file - if file.endswith('.html'): + if file_ext in extensions: src = open(os.path.join(dirpath, file), "rb").read() thefile = '%s.py' % file open(os.path.join(dirpath, thefile), "wb").write(templatize(src)) @@ -144,6 +175,9 @@ class Command(BaseCommand): help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'), make_option('--all', '-a', action='store_true', dest='all', default=False, help='Reexamines all source code and templates for new translation strings and updates all message files for all available languages.'), + make_option('--extension', '-e', dest='extensions', + help='The file extension(s) to examine (default: ".html", separate multiple extensions with commas, or use -e multiple times)', + action='append'), ) 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." @@ -158,5 +192,14 @@ class Command(BaseCommand): domain = options.get('domain') verbosity = int(options.get('verbosity')) process_all = options.get('all') + extensions = options.get('extensions') or ['html'] - make_messages(locale, domain, verbosity, process_all) + if domain == 'djangojs': + extensions = [] + else: + extensions = handle_extensions(extensions) + + if '.js' in extensions: + raise CommandError("JavaScript files should be examined by using the special 'djangojs' domain only.") + + make_messages(locale, domain, verbosity, process_all, extensions) diff --git a/docs/django-admin.txt b/docs/django-admin.txt index 71c4cf545a..f827a7243b 100644 --- a/docs/django-admin.txt +++ b/docs/django-admin.txt @@ -412,6 +412,20 @@ Example usage:: django-admin.py makemessages --all +--extension +~~~~~~~~~~~ + +Use the ``--extension`` or ``-e`` option to specify a list of file extensions +to examine (default: ".html"). + +Example usage:: + + django-admin.py makemessages --locale=de --extension xhtml + +Separate multiple extensions with commas or use -e or --extension multiple times:: + + django-admin.py makemessages --locale=de --extension=html,txt --extension xml + --locale ~~~~~~~~ diff --git a/docs/i18n.txt b/docs/i18n.txt index 5269024e96..62d78fb224 100644 --- a/docs/i18n.txt +++ b/docs/i18n.txt @@ -426,6 +426,21 @@ If run over your project source tree or your application source tree, it will do the same, but the location of the locale directory is ``locale/LANG/LC_MESSAGES`` (note the missing ``conf`` prefix). +By default ``django-admin.py makemessages`` examines every file that has the +``.html`` file extension. In case you want to override that default, use the +``--extension`` or ``-e`` option to specify the file extensions to examine:: + + django-admin.py makemessages -l de -e txt + +Separate multiple extensions with commas and/or use ``-e`` or ``--extension`` multiple times:: + + django-admin.py makemessages -l=de -e=html,txt -e xml + +When `creating JavaScript translation catalogs`_ you need to use the special +'djangojs' domain, **not** ``-e js``. + +.. _create a JavaScript translation catalog: #creating-javascript-translation-catalogs + .. admonition:: No gettext? If you don't have the ``gettext`` utilities installed, diff --git a/docs/man/django-admin.1 b/docs/man/django-admin.1 index c482a3fc6e..2f4b5a02d8 100644 --- a/docs/man/django-admin.1 +++ b/docs/man/django-admin.1 @@ -49,7 +49,7 @@ Executes .B sqlall for the given app(s) in the current database. .TP -.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-all" "]" +.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "]" 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. @@ -154,6 +154,10 @@ The locale to process when using makemessages or compilemessages. .I \-d, \-\-domain=DOMAIN The domain of the message files (default: "django") when using makemessages. .TP +.I \-e, \-\-extension=EXTENSION +The file extension(s) to examine (default: ".html", separate multiple +extensions with commas, or use -e multiple times). +.TP .I \-a, \-\-all Process all available locales when using makemessages. .SH "ENVIRONMENT"