summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Madden <jamadden@gmail.com>2017-12-17 09:57:13 -0600
committerJason Madden <jamadden@gmail.com>2017-12-17 09:57:13 -0600
commit47ee44a0c1368f2f3c09b1055eef934449f3c584 (patch)
tree3a316555acc574ca93209afada4d11e3e53417d0
parenta4cf800dc23b5fc3072e26db539edc3c92891113 (diff)
downloadzope-i18n-47ee44a0c1368f2f3c09b1055eef934449f3c584.tar.gz
Make [Simple]TranslationDomain and GettextMessageCatalog properly implement their interfaces on Python 2 when given native strings for 'domain' and 'language'---these are supposed to be text. Test this.
-rw-r--r--CHANGES.rst5
-rw-r--r--src/zope/i18n/gettextmessagecatalog.py5
-rw-r--r--src/zope/i18n/simpletranslationdomain.py2
-rw-r--r--src/zope/i18n/tests/test_formats.py6
-rw-r--r--src/zope/i18n/tests/test_imessagecatalog.py5
-rw-r--r--src/zope/i18n/translationdomain.py2
6 files changed, 17 insertions, 8 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 2d2fe46..737aeb5 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -15,6 +15,11 @@
It overrode every method and didn't properly initialize the super
class. ``TranslationDomain`` continues to implement ``ITranslationDomain``.
+- ``TranslationDomain`` and ``GettextMessageCatalog`` now ensure that
+ their ``domain`` and ``language`` attributes are text in order to
+ match their respective interfaces. Byte strings (such as native
+ string literals on Python 2) are decoded using UTF-8.
+
4.2.0 (2017-05-23)
==================
diff --git a/src/zope/i18n/gettextmessagecatalog.py b/src/zope/i18n/gettextmessagecatalog.py
index 7b3985c..307e5be 100644
--- a/src/zope/i18n/gettextmessagecatalog.py
+++ b/src/zope/i18n/gettextmessagecatalog.py
@@ -18,6 +18,7 @@ from gettext import GNUTranslations
from zope.i18n.interfaces import IGlobalMessageCatalog
from zope.interface import implementer
+
class _KeyErrorRaisingFallback(object):
def ugettext(self, message):
raise KeyError(message)
@@ -32,8 +33,8 @@ class GettextMessageCatalog(object):
def __init__(self, language, domain, path_to_file):
"""Initialize the message catalog"""
- self.language = language
- self.domain = domain
+ self.language = language.decode('utf-8') if isinstance(language, bytes) else language
+ self.domain = domain.decode("utf-8") if isinstance(domain, bytes) else domain
self._path_to_file = path_to_file
self.reload()
catalog = self._catalog
diff --git a/src/zope/i18n/simpletranslationdomain.py b/src/zope/i18n/simpletranslationdomain.py
index e131c57..b50fb68 100644
--- a/src/zope/i18n/simpletranslationdomain.py
+++ b/src/zope/i18n/simpletranslationdomain.py
@@ -39,7 +39,7 @@ class SimpleTranslationDomain(object):
def __init__(self, domain, messages=None):
"""Initializes the object. No arguments are needed."""
- self.domain = domain
+ self.domain = domain.decode("utf-8") if isinstance(domain, bytes) else domain
self.messages = messages if messages is not None else {}
assert self.messages is not None
diff --git a/src/zope/i18n/tests/test_formats.py b/src/zope/i18n/tests/test_formats.py
index a77fc8e..6d1380d 100644
--- a/src/zope/i18n/tests/test_formats.py
+++ b/src/zope/i18n/tests/test_formats.py
@@ -99,9 +99,9 @@ class LocaleCalendarStub(object):
class _TestCase(TestCase):
- if not hasattr(TestCase, 'assertRaisesRegex'):
- # Avoid deprecation warnings in Python 3
- assertRaisesRegex = TestCase.assertRaisesRegexp
+ # Avoid deprecation warnings in Python 3 by making the preferred
+ # method name available for Python 2.
+ assertRaisesRegex = getattr(TestCase, 'assertRaisesRegex', TestCase.assertRaisesRegexp)
class TestDateTimePatternParser(_TestCase):
diff --git a/src/zope/i18n/tests/test_imessagecatalog.py b/src/zope/i18n/tests/test_imessagecatalog.py
index 28866e3..2856c67 100644
--- a/src/zope/i18n/tests/test_imessagecatalog.py
+++ b/src/zope/i18n/tests/test_imessagecatalog.py
@@ -16,12 +16,13 @@
import unittest
from zope.interface.verify import verifyObject
from zope.i18n.interfaces import IMessageCatalog
+from zope.schema import getValidationErrors
class TestIMessageCatalog(unittest.TestCase):
- # This should be overwritten by every class that inherits this test
+ # This should be overridden by every class that inherits this test
def _getMessageCatalog(self):
raise NotImplementedError()
@@ -34,6 +35,8 @@ class TestIMessageCatalog(unittest.TestCase):
def testInterface(self):
verifyObject(IMessageCatalog, self._catalog)
+ errors = getValidationErrors(IMessageCatalog, self._catalog)
+ self.assertFalse(errors)
def testGetMessage(self):
catalog = self._catalog
diff --git a/src/zope/i18n/translationdomain.py b/src/zope/i18n/translationdomain.py
index 274b744..684fa9b 100644
--- a/src/zope/i18n/translationdomain.py
+++ b/src/zope/i18n/translationdomain.py
@@ -38,7 +38,7 @@ text_type = str if bytes is not str else unicode
class TranslationDomain(object):
def __init__(self, domain, fallbacks=None):
- self.domain = domain
+ self.domain = domain.decode("utf-8") if isinstance(domain, bytes) else domain
# _catalogs maps (language, domain) to IMessageCatalog instances
self._catalogs = {}
# _data maps IMessageCatalog.getIdentifier() to IMessageCatalog