Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #5463 -- Allow alternate file extensions on files that are tran…

…slated.

Patch from Jannis Leidel.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8234 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3735f276603b0c040f3f8aaca64f151397d2cb9b 1 parent afa98db
Malcolm Tredinnick authored August 08, 2008
53  django/core/management/commands/makemessages.py
@@ -5,9 +5,39 @@
5 5
 from optparse import make_option
6 6
 from django.core.management.base import CommandError, BaseCommand
7 7
 
  8
+try:
  9
+    set
  10
+except NameError:
  11
+    from sets import Set as set     # For Python 2.3
  12
+
8 13
 pythonize_re = re.compile(r'\n\s*//')
9 14
 
10  
-def make_messages(locale=None, domain='django', verbosity='1', all=False):
  15
+def handle_extensions(extensions=('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
+    >>> handle_extensions(['.html, txt,.tpl'])
  26
+    ['.html', '.tpl', '.txt']
  27
+    """
  28
+    ext_list = []
  29
+    for ext in extensions:
  30
+        ext_list.extend(ext.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
+    return set([x for x in ext_list if x != '.py'])
  39
+
  40
+def make_messages(locale=None, domain='django', verbosity='1', all=False, extensions=None):
11 41
     """
12 42
     Uses the locale directory from the Django SVN tree or an application/
13 43
     project to process all
@@ -63,7 +93,8 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False):
63 93
             all_files.extend([(dirpath, f) for f in filenames])
64 94
         all_files.sort()
65 95
         for dirpath, file in all_files:
66  
-            if domain == 'djangojs' and file.endswith('.js'):
  96
+            file_base, file_ext = os.path.splitext(file)
  97
+            if domain == 'djangojs' and file_ext == '.js':
67 98
                 if verbosity > 1:
68 99
                     sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
69 100
                 src = open(os.path.join(dirpath, file), "rb").read()
@@ -87,9 +118,9 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False):
87 118
                 if msgs:
88 119
                     open(potfile, 'ab').write(msgs)
89 120
                 os.unlink(os.path.join(dirpath, thefile))
90  
-            elif domain == 'django' and (file.endswith('.py') or file.endswith('.html')):
  121
+            elif domain == 'django' and (file_ext == '.py' or file_ext in extensions):
91 122
                 thefile = file
92  
-                if file.endswith('.html'):
  123
+                if file_ext in extensions:
93 124
                     src = open(os.path.join(dirpath, file), "rb").read()
94 125
                     thefile = '%s.py' % file
95 126
                     open(os.path.join(dirpath, thefile), "wb").write(templatize(src))
@@ -144,6 +175,9 @@ class Command(BaseCommand):
144 175
             help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'),
145 176
         make_option('--all', '-a', action='store_true', dest='all',
146 177
             default=False, help='Reexamines all source code and templates for new translation strings and updates all message files for all available languages.'),
  178
+        make_option('--extension', '-e', dest='extensions',
  179
+            help='The file extension(s) to examine (default: ".html", separate multiple extensions with commas, or use -e multiple times)',
  180
+            action='append'),
147 181
     )
148 182
     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."
149 183
 
@@ -158,5 +192,14 @@ def handle(self, *args, **options):
158 192
         domain = options.get('domain')
159 193
         verbosity = int(options.get('verbosity'))
160 194
         process_all = options.get('all')
  195
+        extensions = options.get('extensions') or ['html']
  196
+
  197
+        if domain == 'djangojs':
  198
+            extensions = []
  199
+        else:
  200
+            extensions = handle_extensions(extensions)
  201
+
  202
+        if '.js' in extensions:
  203
+            raise CommandError("JavaScript files should be examined by using the special 'djangojs' domain only.")
161 204
 
162  
-        make_messages(locale, domain, verbosity, process_all)
  205
+        make_messages(locale, domain, verbosity, process_all, extensions)
14  docs/django-admin.txt
@@ -412,6 +412,20 @@ Example usage::
412 412
 
413 413
     django-admin.py makemessages --all
414 414
 
  415
+--extension
  416
+~~~~~~~~~~~
  417
+
  418
+Use the ``--extension`` or ``-e`` option to specify a list of file extensions
  419
+to examine (default: ".html").
  420
+
  421
+Example usage::
  422
+
  423
+    django-admin.py makemessages --locale=de --extension xhtml
  424
+
  425
+Separate multiple extensions with commas or use -e or --extension multiple times::
  426
+
  427
+    django-admin.py makemessages --locale=de --extension=html,txt --extension xml
  428
+
415 429
 --locale
416 430
 ~~~~~~~~
417 431
 
15  docs/i18n.txt
@@ -426,6 +426,21 @@ If run over your project source tree or your application source tree, it will
426 426
 do the same, but the location of the locale directory is ``locale/LANG/LC_MESSAGES``
427 427
 (note the missing ``conf`` prefix).
428 428
 
  429
+By 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
+
  435
+Separate 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
+
  439
+When `creating JavaScript translation catalogs`_ you need to use the special
  440
+'djangojs' domain, **not** ``-e js``.
  441
+
  442
+.. _create a JavaScript translation catalog: #creating-javascript-translation-catalogs
  443
+
429 444
 .. admonition:: No gettext?
430 445
 
431 446
     If you don't have the ``gettext`` utilities installed,
6  docs/man/django-admin.1
@@ -49,7 +49,7 @@ Executes
49 49
 .B sqlall
50 50
 for the given app(s) in the current database.
51 51
 .TP
52  
-.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-all" "]"
  52
+.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "]"
53 53
 Runs over the entire source tree of the current directory and pulls out all
54 54
 strings marked for translation. It creates (or updates) a message file in the
55 55
 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.
154 154
 .I \-d, \-\-domain=DOMAIN
155 155
 The domain of the message files (default: "django") when using makemessages.
156 156
 .TP
  157
+.I \-e, \-\-extension=EXTENSION
  158
+The file extension(s) to examine (default: ".html", separate multiple
  159
+extensions with commas, or use -e multiple times).
  160
+.TP
157 161
 .I \-a, \-\-all
158 162
 Process all available locales when using makemessages.
159 163
 .SH "ENVIRONMENT"

0 notes on commit 3735f27

Please sign in to comment.
Something went wrong with that request. Please try again.