Skip to content

Commit

Permalink
Fixed django#17008 -- Added makemessages option to not remove .pot fi…
Browse files Browse the repository at this point in the history
…les.

Thanks airstrike for the report and initial patch, Julien for an
enhanced patch and Jannis for reviewing.
  • Loading branch information
ramiro committed Jan 16, 2013
1 parent d406afe commit eee8652
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 13 deletions.
33 changes: 21 additions & 12 deletions django/core/management/commands/makemessages.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def write_pot_file(potfile, msgs, file, work_file, is_templatized):
fp.write(msgs)

def process_file(file, dirpath, potfile, domain, verbosity,
extensions, wrap, location, stdout=sys.stdout):
extensions, wrap, location, keep_pot, stdout=sys.stdout):
"""
Extract translatable literals from :param file: for :param domain:
creating or updating the :param potfile: POT file.
Expand Down Expand Up @@ -183,7 +183,7 @@ def process_file(file, dirpath, potfile, domain, verbosity,
if status != STATUS_OK:
if is_templatized:
os.unlink(work_file)
if os.path.exists(potfile):
if not keep_pot and os.path.exists(potfile):
os.unlink(potfile)
raise CommandError(
"errors happened while running xgettext on %s\n%s" %
Expand All @@ -197,7 +197,7 @@ def process_file(file, dirpath, potfile, domain, verbosity,
os.unlink(work_file)

def write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
copy_pforms, wrap, location, no_obsolete):
copy_pforms, wrap, location, no_obsolete, keep_pot):
"""
Creates of updates the :param pofile: PO file for :param domain: and :param
locale:. Uses contents of the existing :param potfile:.
Expand All @@ -208,7 +208,8 @@ def write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
(wrap, location, potfile))
if errors:
if status != STATUS_OK:
os.unlink(potfile)
if not keep_pot:
os.unlink(potfile)
raise CommandError(
"errors happened while running msguniq\n%s" % errors)
elif verbosity > 0:
Expand All @@ -221,7 +222,8 @@ def write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
(wrap, location, pofile, potfile))
if errors:
if status != STATUS_OK:
os.unlink(potfile)
if not keep_pot:
os.unlink(potfile)
raise CommandError(
"errors happened while running msgmerge\n%s" % errors)
elif verbosity > 0:
Expand All @@ -232,7 +234,8 @@ def write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
"#. #-#-#-#-# %s.pot (PACKAGE VERSION) #-#-#-#-#\n" % domain, "")
with open(pofile, 'w') as fp:
fp.write(msgs)
os.unlink(potfile)
if not keep_pot:
os.unlink(potfile)
if no_obsolete:
msgs, errors, status = _popen(
'msgattrib %s %s -o "%s" --no-obsolete "%s"' %
Expand All @@ -246,7 +249,7 @@ def write_po_file(pofile, potfile, domain, locale, verbosity, stdout,

def make_messages(locale=None, domain='django', verbosity=1, all=False,
extensions=None, symlinks=False, ignore_patterns=None, no_wrap=False,
no_location=False, no_obsolete=False, stdout=sys.stdout):
no_location=False, no_obsolete=False, stdout=sys.stdout, keep_pot=False):
"""
Uses the ``locale/`` directory from the Django Git tree or an
application/project to process all files with translatable literals for
Expand Down Expand Up @@ -280,10 +283,12 @@ def make_messages(locale=None, domain='django', verbosity=1, all=False,
"if you want to enable i18n for your project or application.")

if domain not in ('django', 'djangojs'):
raise CommandError("currently makemessages only supports domains 'django' and 'djangojs'")
raise CommandError("currently makemessages only supports domains "
"'django' and 'djangojs'")

if (locale is None and not all) or domain is None:
message = "Type '%s help %s' for usage information." % (os.path.basename(sys.argv[0]), sys.argv[1])
message = "Type '%s help %s' for usage information." % (
os.path.basename(sys.argv[0]), sys.argv[1])
raise CommandError(message)

# We require gettext version 0.15 or newer.
Expand Down Expand Up @@ -325,11 +330,11 @@ def make_messages(locale=None, domain='django', verbosity=1, all=False,
for dirpath, file in find_files(".", ignore_patterns, verbosity,
stdout, symlinks=symlinks):
process_file(file, dirpath, potfile, domain, verbosity, extensions,
wrap, location, stdout)
wrap, location, keep_pot, stdout)

if os.path.exists(potfile):
write_po_file(pofile, potfile, domain, locale, verbosity, stdout,
not invoked_for_django, wrap, location, no_obsolete)
not invoked_for_django, wrap, location, no_obsolete, keep_pot)


class Command(NoArgsCommand):
Expand All @@ -355,6 +360,8 @@ class Command(NoArgsCommand):
default=False, help="Don't write '#: filename:line' lines"),
make_option('--no-obsolete', action='store_true', dest='no_obsolete',
default=False, help="Remove obsolete message strings"),
make_option('--keep-pot', action='store_true', dest='keep_pot',
default=False, help="Keep .pot file after making messages. Useful when debugging."),
)
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 "
Expand All @@ -379,6 +386,7 @@ def handle_noargs(self, *args, **options):
no_wrap = options.get('no_wrap')
no_location = options.get('no_location')
no_obsolete = options.get('no_obsolete')
keep_pot = options.get('keep_pot')
if domain == 'djangojs':
exts = extensions if extensions else ['js']
else:
Expand All @@ -390,4 +398,5 @@ def handle_noargs(self, *args, **options):
% get_text_list(list(extensions), 'and'))

make_messages(locale, domain, verbosity, process_all, extensions,
symlinks, ignore_patterns, no_wrap, no_location, no_obsolete, self.stdout)
symlinks, ignore_patterns, no_wrap, no_location,
no_obsolete, self.stdout, keep_pot)
8 changes: 8 additions & 0 deletions docs/ref/django-admin.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,14 @@ Use the ``--no-location`` option to not write '``#: filename:line``'
comment lines in language files. Note that using this option makes it harder
for technically skilled translators to understand each message's context.

.. django-admin-option:: --keep-pot

.. versionadded:: 1.6

Use the ``--keep-pot`` option to prevent django from deleting the temporary
.pot file it generates before creating the .po file. This is useful for
debugging errors which may prevent the final language files from being created.

runfcgi [options]
-----------------

Expand Down
33 changes: 33 additions & 0 deletions tests/regressiontests/i18n/commands/extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,36 @@ def test_no_location_disabled(self):
with open(self.PO_FILE, 'r') as fp:
po_contents = force_text(fp.read())
self.assertTrue('#: templates/test.html:55' in po_contents)


class KeepPotFileExtractorTests(ExtractorTests):

def setUp(self):
self.POT_FILE = self.PO_FILE + 't'
super(KeepPotFileExtractorTests, self).setUp()

def tearDown(self):
super(KeepPotFileExtractorTests, self).tearDown()
os.chdir(self.test_dir)
try:
os.unlink(self.POT_FILE)
except OSError:
pass
os.chdir(self._cwd)

def test_keep_pot_disabled_by_default(self):
os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0)
self.assertFalse(os.path.exists(self.POT_FILE))

def test_keep_pot_explicitly_disabled(self):
os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0,
keep_pot=False)
self.assertFalse(os.path.exists(self.POT_FILE))

def test_keep_pot_enabled(self):
os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0,
keep_pot=True)
self.assertTrue(os.path.exists(self.POT_FILE))
2 changes: 1 addition & 1 deletion tests/regressiontests/i18n/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from .commands.extraction import (ExtractorTests, BasicExtractorTests,
JavascriptExtractorTests, IgnoredExtractorTests, SymlinkExtractorTests,
CopyPluralFormsExtractorTests, NoWrapExtractorTests,
NoLocationExtractorTests)
NoLocationExtractorTests, KeepPotFileExtractorTests)
if can_run_compilation_tests:
from .commands.compilation import (PoFileTests, PoFileContentsTests,
PercentRenderingTests)
Expand Down

0 comments on commit eee8652

Please sign in to comment.