diff options
author | Maurits van Rees <maurits@vanrees.org> | 2021-09-02 11:15:07 +0200 |
---|---|---|
committer | Maurits van Rees <maurits@vanrees.org> | 2021-09-02 13:02:40 +0200 |
commit | e812abaaec8a3adc6e695cd124165e3676bd5466 (patch) | |
tree | aaa938169b7f4a82c848e3678aed353c7c72ed5f | |
parent | a4052e1fce73d8b099d3b5386109023ecdbdadc8 (diff) | |
download | zope-i18n-e812abaaec8a3adc6e695cd124165e3676bd5466.tar.gz |
Add _compat module to define text_type on Py 2 and 3.
The old way worked, but gave a linting error because unicode is not defined.
-rw-r--r-- | src/zope/i18n/__init__.py | 5 | ||||
-rw-r--r-- | src/zope/i18n/_compat.py | 8 | ||||
-rw-r--r-- | src/zope/i18n/format.py | 3 | ||||
-rw-r--r-- | src/zope/i18n/simpletranslationdomain.py | 4 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_itranslationdomain.py | 3 | ||||
-rw-r--r-- | src/zope/i18n/tests/test_zcml.py | 2 | ||||
-rw-r--r-- | src/zope/i18n/translationdomain.py | 3 |
7 files changed, 17 insertions, 11 deletions
diff --git a/src/zope/i18n/__init__.py b/src/zope/i18n/__init__.py index 45a47e3..547209e 100644 --- a/src/zope/i18n/__init__.py +++ b/src/zope/i18n/__init__.py @@ -17,15 +17,16 @@ import re from zope.component import queryUtility from zope.i18nmessageid import Message -# I expect that MessageFactory might be there for BBB reasons, but not sure. +# MessageFactory is not used, but it might be here for BBB reasons, +# as it could be imported by other packages. from zope.i18nmessageid import MessageFactory # noqa +from zope.i18n._compat import text_type from zope.i18n.config import ALLOWED_LANGUAGES from zope.i18n.interfaces import INegotiator from zope.i18n.interfaces import ITranslationDomain from zope.i18n.interfaces import IFallbackTranslationDomainFactory -text_type = str if bytes is not str else unicode # Set up regular expressions for finding interpolation variables in text. # NAME_RE must exactly match the expression of the same name in the diff --git a/src/zope/i18n/_compat.py b/src/zope/i18n/_compat.py new file mode 100644 index 0000000..00fb20f --- /dev/null +++ b/src/zope/i18n/_compat.py @@ -0,0 +1,8 @@ +# This gives a linting error because unicode is not defined on Python 3: +# text_type = str if bytes is not str else unicode +try: + # Python 3 + text_type = unicode +except NameError: + # Python 2 + text_type = str diff --git a/src/zope/i18n/format.py b/src/zope/i18n/format.py index 2e32da2..9ccd7f6 100644 --- a/src/zope/i18n/format.py +++ b/src/zope/i18n/format.py @@ -23,10 +23,11 @@ import datetime import pytz import pytz.reference +from zope.i18n._compat import text_type from zope.i18n.interfaces import IDateTimeFormat, INumberFormat from zope.interface import implementer -text_type = str if bytes is not str else unicode + NATIVE_NUMBER_TYPES = (int, float) try: NATIVE_NUMBER_TYPES += (long,) diff --git a/src/zope/i18n/simpletranslationdomain.py b/src/zope/i18n/simpletranslationdomain.py index 889d6db..2b438f3 100644 --- a/src/zope/i18n/simpletranslationdomain.py +++ b/src/zope/i18n/simpletranslationdomain.py @@ -15,13 +15,11 @@ """ from zope.interface import implementer from zope.component import getUtility +from zope.i18n._compat import text_type from zope.i18n.interfaces import ITranslationDomain, INegotiator from zope.i18n import interpolate -text_type = str if bytes is not str else unicode - - @implementer(ITranslationDomain) class SimpleTranslationDomain(object): """This is the simplest implementation of the ITranslationDomain I diff --git a/src/zope/i18n/tests/test_itranslationdomain.py b/src/zope/i18n/tests/test_itranslationdomain.py index e061d87..87fd395 100644 --- a/src/zope/i18n/tests/test_itranslationdomain.py +++ b/src/zope/i18n/tests/test_itranslationdomain.py @@ -22,12 +22,11 @@ from zope.component.testing import PlacelessSetup from zope.schema import getValidationErrors +from zope.i18n._compat import text_type from zope.i18n.negotiator import negotiator from zope.i18n.interfaces import INegotiator, IUserPreferredLanguages from zope.i18n.interfaces import ITranslationDomain -text_type = str if bytes is not str else unicode - @implementer(IUserPreferredLanguages) class Environment(object): diff --git a/src/zope/i18n/tests/test_zcml.py b/src/zope/i18n/tests/test_zcml.py index b1daf68..c17e3db 100644 --- a/src/zope/i18n/tests/test_zcml.py +++ b/src/zope/i18n/tests/test_zcml.py @@ -25,10 +25,10 @@ from zope.component.testing import PlacelessSetup from zope.configuration import xmlconfig import zope.i18n.tests +from zope.i18n._compat import text_type from zope.i18n.interfaces import ITranslationDomain from zope.i18n import config -text_type = str if bytes is not str else unicode template = """\ <configure diff --git a/src/zope/i18n/translationdomain.py b/src/zope/i18n/translationdomain.py index af8eee2..9915deb 100644 --- a/src/zope/i18n/translationdomain.py +++ b/src/zope/i18n/translationdomain.py @@ -19,6 +19,7 @@ import zope.interface from zope.i18nmessageid import Message from zope.i18n import translate, interpolate +from zope.i18n._compat import text_type from zope.i18n.interfaces import ITranslationDomain, INegotiator @@ -32,8 +33,6 @@ from zope.i18n.interfaces import ITranslationDomain, INegotiator # message in a catalog is not translated, tough luck, you get the msgid. LANGUAGE_FALLBACKS = ['en'] -text_type = str if bytes is not str else unicode - @zope.interface.implementer(ITranslationDomain) class TranslationDomain(object): |