diff options
| author | Ramiro Morales <ramiro@rmorales.net> | 2014-12-28 23:02:14 -0300 |
|---|---|---|
| committer | Ramiro Morales <ramiro@rmorales.net> | 2014-12-29 12:24:50 -0300 |
| commit | 6fb9dee470d57882e378247fd2706d5f9867b5f9 (patch) | |
| tree | dc34966a5164a52c330e1caaa80abf8c0893e30b /tests | |
| parent | 1ee9507eb3e21ce3d2d95a84b52cef687bf0606f (diff) | |
Fixed #23271 -- Don't corrupt PO files on Windows when updating them.
Make sure PO catalog text fetched from gettext programs via standard
output isn't corrupted by mismatch between assumed (UTF-8) and real
(CP1252) encodings. This can cause mojibake to be written when creating
or updating PO files.
Also fixes #23311.
Thanks to contributor with Trac nick 'danielmenzel' for the report,
excellent research and fix.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/i18n/commands/__init__.py | 2 | ||||
| -rw-r--r-- | tests/i18n/commands/locale/pt_BR/LC_MESSAGES/django.pristine | 16 | ||||
| -rw-r--r-- | tests/i18n/test_extraction.py | 29 |
3 files changed, 42 insertions, 5 deletions
diff --git a/tests/i18n/commands/__init__.py b/tests/i18n/commands/__init__.py index 76249ca34d..d26751895f 100644 --- a/tests/i18n/commands/__init__.py +++ b/tests/i18n/commands/__init__.py @@ -10,3 +10,5 @@ dummy2 = _("This is another translatable string.") # shouldn't create a .po file with duplicate `Plural-Forms` headers number = 3 dummuy3 = ungettext("%(number)s Foo", "%(number)s Foos", number) % {'number': number} + +dummy4 = _('Size') diff --git a/tests/i18n/commands/locale/pt_BR/LC_MESSAGES/django.pristine b/tests/i18n/commands/locale/pt_BR/LC_MESSAGES/django.pristine new file mode 100644 index 0000000000..95607d679b --- /dev/null +++ b/tests/i18n/commands/locale/pt_BR/LC_MESSAGES/django.pristine @@ -0,0 +1,16 @@ +msgid "" +msgstr "" +"Project-Id-Version: \n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-03-03 10:44+0100\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: \n" +"Language-Team: \n" +"Language: pt_BR\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" + +msgid "Size" +msgstr "Größe" diff --git a/tests/i18n/test_extraction.py b/tests/i18n/test_extraction.py index 6f8fea1bfe..ad96f978bd 100644 --- a/tests/i18n/test_extraction.py +++ b/tests/i18n/test_extraction.py @@ -67,14 +67,21 @@ class ExtractorTests(SimpleTestCase): po_contents = fp.read() return output, po_contents - def assertMsgId(self, msgid, s, use_quotes=True): + def _assertPoKeyword(self, keyword, expected_value, haystack, use_quotes=True): q = '"' if use_quotes: - msgid = '"%s"' % msgid + expected_value = '"%s"' % expected_value q = "'" - needle = 'msgid %s' % msgid - msgid = re.escape(msgid) - return self.assertTrue(re.search('^msgid %s' % msgid, s, re.MULTILINE), 'Could not find %(q)s%(n)s%(q)s in generated PO file' % {'n': needle, 'q': q}) + needle = '%s %s' % (keyword, expected_value) + expected_value = re.escape(expected_value) + return self.assertTrue(re.search('^%s %s' % (keyword, expected_value), haystack, re.MULTILINE), + 'Could not find %(q)s%(n)s%(q)s in generated PO file' % {'n': needle, 'q': q}) + + def assertMsgId(self, msgid, haystack, use_quotes=True): + return self._assertPoKeyword('msgid', msgid, haystack, use_quotes=use_quotes) + + def assertMsgStr(self, msgstr, haystack, use_quotes=True): + return self._assertPoKeyword('msgstr', msgstr, haystack, use_quotes=use_quotes) def assertNotMsgId(self, msgid, s, use_quotes=True): if use_quotes: @@ -391,6 +398,18 @@ class BasicExtractorTests(ExtractorTests): with six.assertRaisesRegex(self, CommandError, "Unable to get gettext version. Is it installed?"): cmd.gettext_version + def test_po_file_encoding_when_updating(self): + """Update of PO file doesn't corrupt it with non-UTF-8 encoding on Python3+Windows (#23271)""" + BR_PO_BASE = 'locale/pt_BR/LC_MESSAGES/django' + os.chdir(self.test_dir) + shutil.copyfile(BR_PO_BASE + '.pristine', BR_PO_BASE + '.po') + self.addCleanup(self.rmfile, os.path.join(self.test_dir, 'locale', 'pt_BR', 'LC_MESSAGES', 'django.po')) + management.call_command('makemessages', locale=['pt_BR'], verbosity=0) + self.assertTrue(os.path.exists(BR_PO_BASE + '.po')) + with io.open(BR_PO_BASE + '.po', 'r', encoding='utf-8') as fp: + po_contents = force_text(fp.read()) + self.assertMsgStr("Größe", po_contents) + class JavascriptExtractorTests(ExtractorTests): |
