diff options
| author | Marti Raudsepp <marti@voicecom.ee> | 2015-11-09 15:58:24 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-11-11 08:57:46 -0500 |
| commit | 9cdfdbdd1fea70390e30d204423ab27ba788d422 (patch) | |
| tree | 84c7c46d28b300d36d1fb36047dd8277ea8c78db | |
| parent | 43099f543eb8cc3969c66f68579685a87bea4b37 (diff) | |
[1.8.x] Fixed #25720 -- Made gettext() return bytestring on Python 2 if input is bytestring.
This is consistent with the behavior of Django 1.7.x and earlier.
Backport of d3e3703a15cd9d294406121bc43be0c75b1a4e0e from master
| -rw-r--r-- | django/utils/translation/trans_real.py | 1 | ||||
| -rw-r--r-- | docs/releases/1.8.7.txt | 3 | ||||
| -rw-r--r-- | tests/i18n/tests.py | 12 |
3 files changed, 16 insertions, 0 deletions
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index cd1ac1debe..6e7ef9b742 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -107,6 +107,7 @@ class DjangoTranslation(gettext_module.GNUTranslations): def __init__(self, language): """Create a GNUTranslations() using many locale directories""" gettext_module.GNUTranslations.__init__(self) + self.set_output_charset('utf-8') # For Python 2 gettext() (#25720) self.__language = language self.__to_language = to_language(language) diff --git a/docs/releases/1.8.7.txt b/docs/releases/1.8.7.txt index cf548ee74d..ec9f310ad9 100644 --- a/docs/releases/1.8.7.txt +++ b/docs/releases/1.8.7.txt @@ -27,3 +27,6 @@ Bugfixes * Fixed a data loss possibility with :class:`~django.db.models.Prefetch` if ``to_attr`` is set to a ``ManyToManyField`` (:ticket:`25693`). + +* Fixed a regression in 1.8 by making ``gettext()`` once again return UTF-8 + bytestrings on Python 2 if the input is a bytestring (:ticket:`25720`). diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 58ef0c74a8..788a6dec89 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -142,6 +142,18 @@ class TranslationTests(TestCase): self.assertNotEqual(s, s4) @skipUnless(six.PY2, "No more bytestring translations on PY3") + def test_bytestrings(self): + """gettext() returns a bytestring if input is bytestring.""" + + # Using repr() to check translated text and type + self.assertEqual(repr(gettext(b"Time")), repr(b"Time")) + self.assertEqual(repr(gettext("Time")), repr("Time")) + + with translation.override('de', deactivate=True): + self.assertEqual(repr(gettext(b"Time")), repr(b"Zeit")) + self.assertEqual(repr(gettext("Time")), repr(b"Zeit")) + + @skipUnless(six.PY2, "No more bytestring translations on PY3") def test_lazy_and_bytestrings(self): # On Python 2, (n)gettext_lazy should not transform a bytestring to unicode self.assertEqual(gettext_lazy(b"test").upper(), b"TEST") |
