Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6476 -- Added option to makemessages management command to dis…

…able wrapping of long lines. Thanks to pytechd for the initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14454 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit efc5384a325e8621cac37b2a33ce64e609c66f39 1 parent c906b27
Jannis Leidel authored
38  django/core/management/commands/makemessages.py
@@ -115,7 +115,7 @@ def copy_plural_forms(msgs, locale, domain, verbosity):
115 115
 
116 116
 
117 117
 def make_messages(locale=None, domain='django', verbosity='1', all=False,
118  
-        extensions=None, symlinks=False, ignore_patterns=[]):
  118
+        extensions=None, symlinks=False, ignore_patterns=[], no_wrap=False):
119 119
     """
120 120
     Uses the locale directory from the Django SVN tree or an application/
121 121
     project to process all
@@ -164,6 +164,8 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False,
164 164
         locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % localedir))
165 165
         languages = [os.path.basename(l) for l in locale_dirs]
166 166
 
  167
+    wrap = no_wrap and '--no-wrap' or ''
  168
+
167 169
     for locale in languages:
168 170
         if verbosity > 0:
169 171
             print "processing language", locale
@@ -190,7 +192,14 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False,
190 192
                     f.write(src)
191 193
                 finally:
192 194
                     f.close()
193  
-                cmd = 'xgettext -d %s -L Perl --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 --from-code UTF-8 -o - "%s"' % (domain, os.path.join(dirpath, thefile))
  195
+                cmd = (
  196
+                    'xgettext -d %s -L Perl %s --keyword=gettext_noop '
  197
+                    '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
  198
+                    '--keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 '
  199
+                    '--from-code UTF-8 -o - "%s"' % (
  200
+                        domain, wrap, os.path.join(dirpath, thefile)
  201
+                    )
  202
+                )
194 203
                 msgs, errors = _popen(cmd)
195 204
                 if errors:
196 205
                     raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
@@ -225,8 +234,15 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False,
225 234
                         raise SyntaxError(msg)
226 235
                 if verbosity > 1:
227 236
                     sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
228  
-                cmd = 'xgettext -d %s -L Python --keyword=gettext_noop --keyword=gettext_lazy --keyword=ngettext_lazy:1,2 --keyword=ugettext_noop --keyword=ugettext_lazy --keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 --keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 --keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 -o - "%s"' % (
229  
-                    domain, os.path.join(dirpath, thefile))
  237
+                cmd = (
  238
+                    'xgettext -d %s -L Python %s --keyword=gettext_noop '
  239
+                    '--keyword=gettext_lazy --keyword=ngettext_lazy:1,2 '
  240
+                    '--keyword=ugettext_noop --keyword=ugettext_lazy '
  241
+                    '--keyword=ungettext_lazy:1,2 --keyword=pgettext:1c,2 '
  242
+                    '--keyword=npgettext:1c,2,3 --keyword=pgettext_lazy:1c,2 '
  243
+                    '--keyword=npgettext_lazy:1c,2,3 --from-code UTF-8 -o - '
  244
+                    '"%s"' % (domain, wrap, os.path.join(dirpath, thefile))
  245
+                )
230 246
                 msgs, errors = _popen(cmd)
231 247
                 if errors:
232 248
                     raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors))
@@ -250,7 +266,8 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False,
250 266
                     os.unlink(os.path.join(dirpath, thefile))
251 267
 
252 268
         if os.path.exists(potfile):
253  
-            msgs, errors = _popen('msguniq --to-code=utf-8 "%s"' % potfile)
  269
+            msgs, errors = _popen('msguniq %s --to-code=utf-8 "%s"' %
  270
+                                  (wrap, potfile))
254 271
             if errors:
255 272
                 raise CommandError("errors happened while running msguniq\n%s" % errors)
256 273
             f = open(potfile, 'w')
@@ -259,7 +276,8 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False,
259 276
             finally:
260 277
                 f.close()
261 278
             if os.path.exists(pofile):
262  
-                msgs, errors = _popen('msgmerge -q "%s" "%s"' % (pofile, potfile))
  279
+                msgs, errors = _popen('msgmerge %s -q "%s" "%s"' %
  280
+                                      (wrap, pofile, potfile))
263 281
                 if errors:
264 282
                     raise CommandError("errors happened while running msgmerge\n%s" % errors)
265 283
             elif not invoked_for_django:
@@ -289,6 +307,8 @@ class Command(BaseCommand):
289 307
             default=[], metavar='PATTERN', help='Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.'),
290 308
         make_option('--no-default-ignore', action='store_false', dest='use_default_ignore_patterns',
291 309
             default=True, help="Don't ignore the common glob-style patterns 'CVS', '.*' and '*~'."),
  310
+        make_option('--no-wrap', action='store_true', dest='no_wrap',
  311
+            default=False, help="Don't break long message lines into several lines"),
292 312
     )
293 313
     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."
294 314
 
@@ -309,6 +329,7 @@ def handle(self, *args, **options):
309 329
         if options.get('use_default_ignore_patterns'):
310 330
             ignore_patterns += ['CVS', '.*', '*~']
311 331
         ignore_patterns = list(set(ignore_patterns))
  332
+        no_wrap = options.get('no_wrap')
312 333
 
313 334
         if domain == 'djangojs':
314 335
             extensions = handle_extensions(extensions or ['js'])
@@ -316,6 +337,7 @@ def handle(self, *args, **options):
316 337
             extensions = handle_extensions(extensions or ['html'])
317 338
 
318 339
         if verbosity > 1:
319  
-            sys.stdout.write('examining files with the extensions: %s\n' % get_text_list(list(extensions), 'and'))
  340
+            sys.stdout.write('examining files with the extensions: %s\n'
  341
+                             % get_text_list(list(extensions), 'and'))
320 342
 
321  
-        make_messages(locale, domain, verbosity, process_all, extensions, symlinks, ignore_patterns)
  343
+        make_messages(locale, domain, verbosity, process_all, extensions, symlinks, ignore_patterns, no_wrap)
5  docs/man/django-admin.1
@@ -60,7 +60,7 @@ Executes
60 60
 .B sqlall
61 61
 for the given app(s) in the current database.
62 62
 .TP
63  
-.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "] [" "\-\-symlinks" "] [" "\-\-ignore=PATTERN" "] [" "\-\-no\-default\-ignore" "]"
  63
+.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "] [" "\-\-symlinks" "] [" "\-\-ignore=PATTERN" "] [" "\-\-no\-default\-ignore" "] [" "\-\-no\-wrap" "]"
64 64
 Runs over the entire source tree of the current directory and pulls out all
65 65
 strings marked for translation. It creates (or updates) a message file in the
66 66
 conf/locale (in the django tree) or locale (for project and application) directory.
@@ -196,6 +196,9 @@ times to ignore more.
196 196
 .I \-\-no\-default\-ignore
197 197
 Don't ignore the common private glob-style patterns 'CVS', '.*' and '*~'.
198 198
 .TP
  199
+.I \-\-no\-wrap
  200
+Don't break long message lines into several lines.
  201
+.TP
199 202
 .I \-a, \-\-all
200 203
 Process all available locales when using makemessages..SH "ENVIRONMENT"
201 204
 .TP
7  docs/ref/django-admin.txt
@@ -481,6 +481,13 @@ Example usage::
481 481
 Use the ``--no-default-ignore`` option to disable the default values of
482 482
 :djadminopt:`--ignore`.
483 483
 
  484
+.. django-admin-option:: --no-wrap
  485
+
  486
+.. versionadded:: 1.3
  487
+
  488
+Use the ``--no-wrap`` option to disable breaking long message lines into
  489
+several lines in language files.
  490
+
484 491
 reset <appname appname ...>
485 492
 ---------------------------
486 493
 
29  tests/regressiontests/i18n/commands/extraction.py
@@ -27,11 +27,15 @@ def tearDown(self):
27 27
             pass
28 28
         os.chdir(self._cwd)
29 29
 
30  
-    def assertMsgId(self, msgid, s):
31  
-        return self.assert_(re.search('^msgid "%s"' % msgid, s, re.MULTILINE))
  30
+    def assertMsgId(self, msgid, s, use_quotes=True):
  31
+        if use_quotes:
  32
+            msgid = '"%s"' % msgid
  33
+        return self.assert_(re.search('^msgid %s' % msgid, s, re.MULTILINE))
32 34
 
33  
-    def assertNotMsgId(self, msgid, s):
34  
-        return self.assert_(not re.search('^msgid "%s"' % msgid, s, re.MULTILINE))
  35
+    def assertNotMsgId(self, msgid, s, use_quotes=True):
  36
+        if use_quotes:
  37
+            msgid = '"%s"' % msgid
  38
+        return self.assert_(not re.search('^msgid %s' % msgid, s, re.MULTILINE))
35 39
 
36 40
 
37 41
 class JavascriptExtractorTests(ExtractorTests):
@@ -96,3 +100,20 @@ def test_copy_plural_forms(self):
96 100
         self.assert_(os.path.exists(self.PO_FILE))
97 101
         po_contents = open(self.PO_FILE, 'r').read()
98 102
         self.assert_('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents)
  103
+
  104
+
  105
+class NoWrapExtractorTests(ExtractorTests):
  106
+
  107
+    def test_no_wrap_enabled(self):
  108
+        os.chdir(self.test_dir)
  109
+        management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=True)
  110
+        self.assert_(os.path.exists(self.PO_FILE))
  111
+        po_contents = open(self.PO_FILE, 'r').read()
  112
+        self.assertMsgId('This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option.', po_contents)
  113
+
  114
+    def test_no_wrap_disabled(self):
  115
+        os.chdir(self.test_dir)
  116
+        management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=False)
  117
+        self.assert_(os.path.exists(self.PO_FILE))
  118
+        po_contents = open(self.PO_FILE, 'r').read()
  119
+        self.assertMsgId('""\n"This literal should also be included wrapped or not wrapped depending on the "\n"use of the --no-wrap option."', po_contents, use_quotes=False)
3  tests/regressiontests/i18n/commands/templates/test.html
... ...
@@ -1,2 +1,3 @@
1 1
 {% load i18n %}
2  
-{% trans "This literal should be included." %}
  2
+{% trans "This literal should be included." %}
  3
+{% trans "This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option." %}

0 notes on commit efc5384

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