From d16753eecd3c97c781382fbb4c119f6bae314487 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Tue, 1 Oct 2013 20:23:36 -0300 Subject: [PATCH] [1.6.x] Fixed #21209 -- .po file path comments on Windows. Literals from source files with Django template language syntax don't have a '.py' suffix anymore. Also, the '.\' prefix is preserved to respect GNU gettext behavior on that platform. Refs #16903. 4b715fc05a from master. --- .../core/management/commands/makemessages.py | 10 +++++++-- tests/i18n/commands/extraction.py | 22 ++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py index 695cad419b..7ad1a8f3aa 100644 --- a/django/core/management/commands/makemessages.py +++ b/django/core/management/commands/makemessages.py @@ -135,8 +135,14 @@ class TranslatableFile(object): command.stdout.write(errors) if msgs: if is_templatized: - old = '#: ' + work_file[2:] - new = '#: ' + orig_file[2:] + # Remove '.py' suffix + if os.name =='nt': + # Preserve '.\' prefix on Windows to respect gettext behavior + old = '#: ' + work_file + new = '#: ' + orig_file + else: + old = '#: ' + work_file[2:] + new = '#: ' + orig_file[2:] msgs = msgs.replace(old, new) write_pot_file(potfile, msgs) if is_templatized: diff --git a/tests/i18n/commands/extraction.py b/tests/i18n/commands/extraction.py index dd1dce2ed0..4139abdd37 100644 --- a/tests/i18n/commands/extraction.py +++ b/tests/i18n/commands/extraction.py @@ -377,23 +377,39 @@ class NoWrapExtractorTests(ExtractorTests): 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) -class NoLocationExtractorTests(ExtractorTests): +class LocationCommentsTests(ExtractorTests): def test_no_location_enabled(self): + """Behavior is correct if --no-location switch is specified. See #16903.""" os.chdir(self.test_dir) management.call_command('makemessages', locale=LOCALE, verbosity=0, no_location=True) self.assertTrue(os.path.exists(self.PO_FILE)) with open(self.PO_FILE, 'r') as fp: po_contents = force_text(fp.read()) - self.assertFalse('#: templates/test.html:55' in po_contents) + needle = os.sep.join(['#: templates', 'test.html:55']) + self.assertFalse(needle in po_contents, '"%s" shouldn\'t be in final .po file.' % needle) def test_no_location_disabled(self): + """Behavior is correct if --no-location switch isn't specified.""" os.chdir(self.test_dir) management.call_command('makemessages', locale=LOCALE, verbosity=0, no_location=False) self.assertTrue(os.path.exists(self.PO_FILE)) with open(self.PO_FILE, 'r') as fp: + # Standard comment with source file relative path should be present -- #16903 po_contents = force_text(fp.read()) - self.assertTrue('#: templates/test.html:55' in po_contents) + if os.name == 'nt': + # #: .\path\to\file.html:123 + cwd_prefix = '%s%s' % (os.curdir, os.sep) + else: + # #: path/to/file.html:123 + cwd_prefix = '' + needle = os.sep.join(['#: %stemplates' % cwd_prefix, 'test.html:55']) + self.assertTrue(needle in po_contents, '"%s" not found in final .po file.' % needle) + + # #21208 -- Leaky paths in comments on Windows e.g. #: path\to\file.html.py:123 + bad_suffix = '.py' + bad_string = 'templates%stest.html%s' % (os.sep, bad_suffix) # + self.assertFalse(bad_string in po_contents, '"%s" shouldn\'t be in final .po file.' % bad_string) class KeepPotFileExtractorTests(ExtractorTests):