Fixed #6476 -- Added option to makemessages management command to disable 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
This commit is contained in:
Jannis Leidel 2010-11-04 12:08:37 +00:00
parent c906b270f5
commit efc5384a32
5 changed files with 68 additions and 14 deletions

View File

@ -115,7 +115,7 @@ def copy_plural_forms(msgs, locale, domain, verbosity):
def make_messages(locale=None, domain='django', verbosity='1', all=False, def make_messages(locale=None, domain='django', verbosity='1', all=False,
extensions=None, symlinks=False, ignore_patterns=[]): extensions=None, symlinks=False, ignore_patterns=[], no_wrap=False):
""" """
Uses the locale directory from the Django SVN tree or an application/ Uses the locale directory from the Django SVN tree or an application/
project to process all project to process all
@ -164,6 +164,8 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False,
locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % localedir)) locale_dirs = filter(os.path.isdir, glob.glob('%s/*' % localedir))
languages = [os.path.basename(l) for l in locale_dirs] languages = [os.path.basename(l) for l in locale_dirs]
wrap = no_wrap and '--no-wrap' or ''
for locale in languages: for locale in languages:
if verbosity > 0: if verbosity > 0:
print "processing language", locale print "processing language", locale
@ -190,7 +192,14 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False,
f.write(src) f.write(src)
finally: finally:
f.close() f.close()
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)) cmd = (
'xgettext -d %s -L Perl %s --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, wrap, os.path.join(dirpath, thefile)
)
)
msgs, errors = _popen(cmd) msgs, errors = _popen(cmd)
if errors: if errors:
raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors)) 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,
raise SyntaxError(msg) raise SyntaxError(msg)
if verbosity > 1: if verbosity > 1:
sys.stdout.write('processing file %s in %s\n' % (file, dirpath)) sys.stdout.write('processing file %s in %s\n' % (file, dirpath))
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"' % ( cmd = (
domain, os.path.join(dirpath, thefile)) 'xgettext -d %s -L Python %s --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"' % (domain, wrap, os.path.join(dirpath, thefile))
)
msgs, errors = _popen(cmd) msgs, errors = _popen(cmd)
if errors: if errors:
raise CommandError("errors happened while running xgettext on %s\n%s" % (file, errors)) 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,
os.unlink(os.path.join(dirpath, thefile)) os.unlink(os.path.join(dirpath, thefile))
if os.path.exists(potfile): if os.path.exists(potfile):
msgs, errors = _popen('msguniq --to-code=utf-8 "%s"' % potfile) msgs, errors = _popen('msguniq %s --to-code=utf-8 "%s"' %
(wrap, potfile))
if errors: if errors:
raise CommandError("errors happened while running msguniq\n%s" % errors) raise CommandError("errors happened while running msguniq\n%s" % errors)
f = open(potfile, 'w') f = open(potfile, 'w')
@ -259,7 +276,8 @@ def make_messages(locale=None, domain='django', verbosity='1', all=False,
finally: finally:
f.close() f.close()
if os.path.exists(pofile): if os.path.exists(pofile):
msgs, errors = _popen('msgmerge -q "%s" "%s"' % (pofile, potfile)) msgs, errors = _popen('msgmerge %s -q "%s" "%s"' %
(wrap, pofile, potfile))
if errors: if errors:
raise CommandError("errors happened while running msgmerge\n%s" % errors) raise CommandError("errors happened while running msgmerge\n%s" % errors)
elif not invoked_for_django: elif not invoked_for_django:
@ -289,6 +307,8 @@ class Command(BaseCommand):
default=[], metavar='PATTERN', help='Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.'), default=[], metavar='PATTERN', help='Ignore files or directories matching this glob-style pattern. Use multiple times to ignore more.'),
make_option('--no-default-ignore', action='store_false', dest='use_default_ignore_patterns', make_option('--no-default-ignore', action='store_false', dest='use_default_ignore_patterns',
default=True, help="Don't ignore the common glob-style patterns 'CVS', '.*' and '*~'."), default=True, help="Don't ignore the common glob-style patterns 'CVS', '.*' and '*~'."),
make_option('--no-wrap', action='store_true', dest='no_wrap',
default=False, help="Don't break long message lines into several lines"),
) )
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." 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."
@ -309,6 +329,7 @@ class Command(BaseCommand):
if options.get('use_default_ignore_patterns'): if options.get('use_default_ignore_patterns'):
ignore_patterns += ['CVS', '.*', '*~'] ignore_patterns += ['CVS', '.*', '*~']
ignore_patterns = list(set(ignore_patterns)) ignore_patterns = list(set(ignore_patterns))
no_wrap = options.get('no_wrap')
if domain == 'djangojs': if domain == 'djangojs':
extensions = handle_extensions(extensions or ['js']) extensions = handle_extensions(extensions or ['js'])
@ -316,6 +337,7 @@ class Command(BaseCommand):
extensions = handle_extensions(extensions or ['html']) extensions = handle_extensions(extensions or ['html'])
if verbosity > 1: if verbosity > 1:
sys.stdout.write('examining files with the extensions: %s\n' % get_text_list(list(extensions), 'and')) sys.stdout.write('examining files with the extensions: %s\n'
% get_text_list(list(extensions), 'and'))
make_messages(locale, domain, verbosity, process_all, extensions, symlinks, ignore_patterns) make_messages(locale, domain, verbosity, process_all, extensions, symlinks, ignore_patterns, no_wrap)

View File

@ -60,7 +60,7 @@ Executes
.B sqlall .B sqlall
for the given app(s) in the current database. for the given app(s) in the current database.
.TP .TP
.BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "] [" "\-\-symlinks" "] [" "\-\-ignore=PATTERN" "] [" "\-\-no\-default\-ignore" "]" .BI "makemessages [" "\-\-locale=LOCALE" "] [" "\-\-domain=DOMAIN" "] [" "\-\-extension=EXTENSION" "] [" "\-\-all" "] [" "\-\-symlinks" "] [" "\-\-ignore=PATTERN" "] [" "\-\-no\-default\-ignore" "] [" "\-\-no\-wrap" "]"
Runs over the entire source tree of the current directory and pulls out 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 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. conf/locale (in the django tree) or locale (for project and application) directory.
@ -196,6 +196,9 @@ times to ignore more.
.I \-\-no\-default\-ignore .I \-\-no\-default\-ignore
Don't ignore the common private glob-style patterns 'CVS', '.*' and '*~'. Don't ignore the common private glob-style patterns 'CVS', '.*' and '*~'.
.TP .TP
.I \-\-no\-wrap
Don't break long message lines into several lines.
.TP
.I \-a, \-\-all .I \-a, \-\-all
Process all available locales when using makemessages..SH "ENVIRONMENT" Process all available locales when using makemessages..SH "ENVIRONMENT"
.TP .TP

View File

@ -481,6 +481,13 @@ Example usage::
Use the ``--no-default-ignore`` option to disable the default values of Use the ``--no-default-ignore`` option to disable the default values of
:djadminopt:`--ignore`. :djadminopt:`--ignore`.
.. django-admin-option:: --no-wrap
.. versionadded:: 1.3
Use the ``--no-wrap`` option to disable breaking long message lines into
several lines in language files.
reset <appname appname ...> reset <appname appname ...>
--------------------------- ---------------------------

View File

@ -27,11 +27,15 @@ class ExtractorTests(TestCase):
pass pass
os.chdir(self._cwd) os.chdir(self._cwd)
def assertMsgId(self, msgid, s): def assertMsgId(self, msgid, s, use_quotes=True):
return self.assert_(re.search('^msgid "%s"' % msgid, s, re.MULTILINE)) if use_quotes:
msgid = '"%s"' % msgid
return self.assert_(re.search('^msgid %s' % msgid, s, re.MULTILINE))
def assertNotMsgId(self, msgid, s): def assertNotMsgId(self, msgid, s, use_quotes=True):
return self.assert_(not re.search('^msgid "%s"' % msgid, s, re.MULTILINE)) if use_quotes:
msgid = '"%s"' % msgid
return self.assert_(not re.search('^msgid %s' % msgid, s, re.MULTILINE))
class JavascriptExtractorTests(ExtractorTests): class JavascriptExtractorTests(ExtractorTests):
@ -96,3 +100,20 @@ class CopyPluralFormsExtractorTests(ExtractorTests):
self.assert_(os.path.exists(self.PO_FILE)) self.assert_(os.path.exists(self.PO_FILE))
po_contents = open(self.PO_FILE, 'r').read() po_contents = open(self.PO_FILE, 'r').read()
self.assert_('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents) self.assert_('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents)
class NoWrapExtractorTests(ExtractorTests):
def test_no_wrap_enabled(self):
os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=True)
self.assert_(os.path.exists(self.PO_FILE))
po_contents = open(self.PO_FILE, 'r').read()
self.assertMsgId('This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option.', po_contents)
def test_no_wrap_disabled(self):
os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=False)
self.assert_(os.path.exists(self.PO_FILE))
po_contents = open(self.PO_FILE, 'r').read()
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)

View File

@ -1,2 +1,3 @@
{% load i18n %} {% load i18n %}
{% trans "This literal should be included." %} {% trans "This literal should be included." %}
{% trans "This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option." %}