[Python-modules-commits] [python-translationstring] 02/08: Imported Upstream version 1.3

Takaki Taniguchi takaki at moszumanska.debian.org
Wed Nov 25 14:49:45 UTC 2015


This is an automated email from the git hooks/post-receive script.

takaki pushed a commit to branch master
in repository python-translationstring.

commit d57018b2b3ca095f0344bddf588235f043e1c4bc
Author: TANIGUCHI Takaki <takaki at asis.media-as.org>
Date:   Wed Nov 25 23:44:22 2015 +0900

    Imported Upstream version 1.3
---
 CHANGES.txt                             |  53 --------------
 PKG-INFO                                |  28 ++++++--
 README.txt => README.rst                |   0
 setup.cfg                               |   3 +
 setup.py                                |  12 ++--
 tox.ini                                 |   5 +-
 translationstring.egg-info/PKG-INFO     |  28 ++++++--
 translationstring.egg-info/SOURCES.txt  |   3 +-
 translationstring/__init__.py           | 119 ++++++++++++++++++++++----------
 translationstring/tests/test__init__.py | 102 +++++++++++++++++++++++----
 10 files changed, 229 insertions(+), 124 deletions(-)

diff --git a/CHANGES.txt b/CHANGES.txt
deleted file mode 100644
index 738b0d9..0000000
--- a/CHANGES.txt
+++ /dev/null
@@ -1,53 +0,0 @@
-translationstring
-=================
-
-1.1 (2012-02-08)
-----------------
-
-- Add MANIFEST to make sure all files are present in a release. This fixes
-  `ticket 8 <https://github.com/Pylons/translationstring/issues/8>`_.
-
-
-1.0 (2012-02-04)
-----------------
-
-- coerce non-string values to a string during translation, except for None.
-
-- Honour mapping information passed to the translator, combining it with
-  mapping data already part of the translation string.
-  
-- Support formatting of translation strings with %-operator.
-
-0.4 (09-22-2011)
-----------------
-
-- Python 3 compatibility (thanks to Joe Dallago, GSOC student).
-
-- Remove testing dependency on Babel.
-
-- Moved to GitHub (https://github.com/Pylons/translationstring).
-
-- Added tox.ini for testing purposes.
-
-0.3 (06-25-2010)
-----------------
-
-- Preserve default translations even if they are an empty string. This
-  fixes problems with Chameleon being unable to determine if a translation
-  is present or not.
-
-0.2 (04-25-2010)
-----------------
-
-- Add ``__getstate__`` and ``__reduce__`` methods to translation
-  string to allow for pickling.
-
-- Fix bug in ChameleonTranslate.  When ``i18n:translate`` was used in
-  templates, a translation string was inappropriately created with a
-  ``default`` value of the empty string.  Symptom: template text would
-  "disappear" rather than being returned untranslated.
-
-0.1 (04-24-2010)
-----------------
-
-- Initial release.
diff --git a/PKG-INFO b/PKG-INFO
index 3b76631..3eaef94 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
-Metadata-Version: 1.0
+Metadata-Version: 1.1
 Name: translationstring
-Version: 1.1
+Version: 1.3
 Summary: Utility library for i18n relied on by various Repoze and Pyramid packages
 Home-page: http://pylonsproject.org
 Author: Chris McDonough, Agendaless Consulting
@@ -27,6 +27,24 @@ Description: translationstring
         translationstring
         =================
         
+        1.3 (2014-11-05)
+        ----------------
+        
+        - Fix Python 3-specific test failures.
+        
+        - Restore compatibility with Python 3.2.
+        
+        1.2 (2014-11-04)
+        ----------------
+        
+        - Add support for message contexts.
+        
+        - If the object passed to a TranslationStringFactory ``__call__`` method is
+          itself a translation string, use the passed object's ``domain`` instead of
+          the domain passed to the factory's contstructor.  See
+          https://github.com/Pylons/translationstring/pull/12 .
+        
+        
         1.1 (2012-02-08)
         ----------------
         
@@ -83,12 +101,14 @@ Platform: UNKNOWN
 Classifier: Development Status :: 5 - Production/Stable
 Classifier: Intended Audience :: Developers
 Classifier: Programming Language :: Python :: 2
-Classifier: Programming Language :: Python :: 2.4
-Classifier: Programming Language :: Python :: 2.5
 Classifier: Programming Language :: Python :: 2.6
 Classifier: Programming Language :: Python :: 2.7
 Classifier: Programming Language :: Python :: 3
 Classifier: Programming Language :: Python :: 3.2
+Classifier: Programming Language :: Python :: 3.3
+Classifier: Programming Language :: Python :: 3.4
+Classifier: Programming Language :: Python :: Implementation :: CPython
+Classifier: Programming Language :: Python :: Implementation :: PyPy
 Classifier: Topic :: Software Development :: Libraries :: Python Modules
 Classifier: Topic :: Software Development :: Internationalization
 Classifier: Topic :: Software Development :: Localization
diff --git a/README.txt b/README.rst
similarity index 100%
rename from README.txt
rename to README.rst
diff --git a/setup.cfg b/setup.cfg
index ecc0b41..1d980c4 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -8,6 +8,9 @@ nocapture = 1
 cover-package = translationstring
 cover-erase = 1
 
+[bdist_wheel]
+universal = 1
+
 [egg_info]
 tag_build = 
 tag_date = 0
diff --git a/setup.py b/setup.py
index 3907dcc..685c66f 100644
--- a/setup.py
+++ b/setup.py
@@ -6,14 +6,14 @@ from setuptools import find_packages
 here = os.path.abspath(os.path.dirname(__file__))
 
 try:
-    README = open(os.path.join(here, 'README.txt')).read()
-    CHANGES = open(os.path.join(here, 'CHANGES.txt')).read()
+    README = open(os.path.join(here, 'README.rst')).read()
+    CHANGES = open(os.path.join(here, 'changes.rst')).read()
 except:
     README = ''
     CHANGES = ''
 
 setup(name='translationstring',
-      version='1.1',
+      version='1.3',
       description=('Utility library for i18n relied on by various Repoze '
                    'and Pyramid packages'),
       long_description=README + '\n\n' +  CHANGES,
@@ -21,12 +21,14 @@ setup(name='translationstring',
           "Development Status :: 5 - Production/Stable",
           "Intended Audience :: Developers",
           "Programming Language :: Python :: 2",
-          "Programming Language :: Python :: 2.4",
-          "Programming Language :: Python :: 2.5",
           "Programming Language :: Python :: 2.6",
           "Programming Language :: Python :: 2.7",
           "Programming Language :: Python :: 3",
           "Programming Language :: Python :: 3.2",
+          "Programming Language :: Python :: 3.3",
+          "Programming Language :: Python :: 3.4",
+          "Programming Language :: Python :: Implementation :: CPython",
+          "Programming Language :: Python :: Implementation :: PyPy",
           "Topic :: Software Development :: Libraries :: Python Modules",
           "Topic :: Software Development :: Internationalization",
           "Topic :: Software Development :: Localization",
diff --git a/tox.ini b/tox.ini
index 79bef43..6c81fe1 100644
--- a/tox.ini
+++ b/tox.ini
@@ -1,6 +1,6 @@
 [tox]
 envlist = 
-    py24,py25,py26,py27,py32,jython,pypy,cover
+    py26,py27,py32,jython,pypy,cover
 
 [testenv]
 commands = 
@@ -14,7 +14,7 @@ commands =
 
 [testenv:cover]
 basepython =
-    python2.6
+    python2.7
 commands = 
     python setup.py nosetests --with-xunit --with-xcoverage
 deps = 
@@ -27,4 +27,3 @@ deps =
 # cobertura jenkins reporting and b) pypy and jython can't handle any
 # combination of versions of coverage and nosexcover that i can find.
 # coverage==3.4 is required by nosexcover.
-
diff --git a/translationstring.egg-info/PKG-INFO b/translationstring.egg-info/PKG-INFO
index 3b76631..3eaef94 100644
--- a/translationstring.egg-info/PKG-INFO
+++ b/translationstring.egg-info/PKG-INFO
@@ -1,6 +1,6 @@
-Metadata-Version: 1.0
+Metadata-Version: 1.1
 Name: translationstring
-Version: 1.1
+Version: 1.3
 Summary: Utility library for i18n relied on by various Repoze and Pyramid packages
 Home-page: http://pylonsproject.org
 Author: Chris McDonough, Agendaless Consulting
@@ -27,6 +27,24 @@ Description: translationstring
         translationstring
         =================
         
+        1.3 (2014-11-05)
+        ----------------
+        
+        - Fix Python 3-specific test failures.
+        
+        - Restore compatibility with Python 3.2.
+        
+        1.2 (2014-11-04)
+        ----------------
+        
+        - Add support for message contexts.
+        
+        - If the object passed to a TranslationStringFactory ``__call__`` method is
+          itself a translation string, use the passed object's ``domain`` instead of
+          the domain passed to the factory's contstructor.  See
+          https://github.com/Pylons/translationstring/pull/12 .
+        
+        
         1.1 (2012-02-08)
         ----------------
         
@@ -83,12 +101,14 @@ Platform: UNKNOWN
 Classifier: Development Status :: 5 - Production/Stable
 Classifier: Intended Audience :: Developers
 Classifier: Programming Language :: Python :: 2
-Classifier: Programming Language :: Python :: 2.4
-Classifier: Programming Language :: Python :: 2.5
 Classifier: Programming Language :: Python :: 2.6
 Classifier: Programming Language :: Python :: 2.7
 Classifier: Programming Language :: Python :: 3
 Classifier: Programming Language :: Python :: 3.2
+Classifier: Programming Language :: Python :: 3.3
+Classifier: Programming Language :: Python :: 3.4
+Classifier: Programming Language :: Python :: Implementation :: CPython
+Classifier: Programming Language :: Python :: Implementation :: PyPy
 Classifier: Topic :: Software Development :: Libraries :: Python Modules
 Classifier: Topic :: Software Development :: Internationalization
 Classifier: Topic :: Software Development :: Localization
diff --git a/translationstring.egg-info/SOURCES.txt b/translationstring.egg-info/SOURCES.txt
index d48a136..71a46b0 100644
--- a/translationstring.egg-info/SOURCES.txt
+++ b/translationstring.egg-info/SOURCES.txt
@@ -1,9 +1,8 @@
-CHANGES.txt
 CONTRIBUTORS.txt
 COPYRIGHT.txt
 LICENSE.txt
 MANIFEST.in
-README.txt
+README.rst
 setup.cfg
 setup.py
 tox.ini
diff --git a/translationstring/__init__.py b/translationstring/__init__.py
index 9e0f66c..5fbea80 100644
--- a/translationstring/__init__.py
+++ b/translationstring/__init__.py
@@ -9,6 +9,8 @@ NAME_RE = r"[a-zA-Z][-a-zA-Z0-9_]*"
 _interp_regex = re.compile(r'(?<!\$)(\$(?:(%(n)s)|{(%(n)s)}))'
     % ({'n': NAME_RE}))
 
+CONTEXT_MASK = text_type('%s\x04%s')
+
 class TranslationString(text_type):
     """
     The constructor for a :term:`translation string`.  A translation
@@ -30,7 +32,7 @@ class TranslationString(text_type):
     ``default`` represents an explicit *default text* for this
     translation string.  Default text appears when the translation
     string cannot be translated.  Usually, the ``msgid`` of a
-    translation string serves double duty as as its default text.
+    translation string serves double duty as its default text.
     However, using this option you can provide a different default
     text for this translation string.  This feature is useful when the
     default of a translation string is too complicated or too long to
@@ -45,6 +47,9 @@ class TranslationString(text_type):
     string` *replacement marker* instances found within the ``msgid``
     (or ``default``) value of this translation string.
 
+    ``context`` represents the :term:`translation context`.  By default,
+    the translation context is ``None``.
+
     After a translation string is constructed, it behaves like most
     other ``unicode`` objects; its ``msgid`` value will be displayed
     when it is treated like a ``unicode`` object.  Only when its
@@ -56,9 +61,9 @@ class TranslationString(text_type):
     ``mapping`` attribute.  The object otherwise behaves much like a
     Unicode string.
     """
-    __slots__ = ('domain', 'default', 'mapping')
+    __slots__ = ('domain', 'context', 'default', 'mapping')
 
-    def __new__(self, msgid, domain=None, default=None, mapping=None):
+    def __new__(self, msgid, domain=None, default=None, mapping=None, context=None):
 
         # NB: this function should never never lose the *original
         # identity* of a non-``None`` but empty ``default`` value
@@ -67,10 +72,12 @@ class TranslationString(text_type):
         self = text_type.__new__(self, msgid)
         if isinstance(msgid, self.__class__):
             domain = domain or msgid.domain and msgid.domain[:]
+            context = context or msgid.context and msgid.context[:]
             default = default or msgid.default and msgid.default[:]
             mapping = mapping or msgid.mapping and msgid.mapping.copy()
             msgid = text_type(msgid)
         self.domain = domain
+        self.context = context
         if default is None:
             default = text_type(msgid)
         self.default = default
@@ -97,7 +104,7 @@ class TranslationString(text_type):
     def interpolate(self, translated=None):
         """ Interpolate the value ``translated`` which is assumed to
         be a Unicode object containing zero or more *replacement
-        markers* (``${foo}`` or ``${bar}``) using the ``mapping``
+        markers* (``$foo`` or ``${bar}``) using the ``mapping``
         dictionary attached to this instance.  If the ``mapping``
         dictionary is empty or ``None``, no interpolation is
         performed.
@@ -111,7 +118,7 @@ class TranslationString(text_type):
         # NB: this function should never never lose the *original
         # identity* of a non-``None`` but empty ``default`` value it
         # is provided.  If (translated == default) , it should return the
-        # *orignal* default, not a derivation.  See the comment below in
+        # *original* default, not a derivation.  See the comment below in
         # ChameleonTranslate.
 
         if self.mapping and translated:
@@ -126,9 +133,9 @@ class TranslationString(text_type):
         return self.__class__, self.__getstate__()
 
     def __getstate__(self):
-        return text_type(self), self.domain, self.default, self.mapping
+        return text_type(self), self.domain, self.default, self.mapping, self.context
 
-def TranslationStringFactory(domain):
+def TranslationStringFactory(factory_domain):
     """ Create a factory which will generate translation strings
     without requiring that each call to the factory be passed a
     ``domain`` value.  A single argument is passed to this class'
@@ -139,12 +146,20 @@ def TranslationStringFactory(domain):
     ``__call__`` method of an instance of this class have the meaning
     as described by the constructor of the
     :class:`translationstring.TranslationString`"""
-    def create(msgid, mapping=None, default=None):
+    def create(msgid, mapping=None, default=None, context=None):
         """ Provided a msgid (Unicode object or :term:`translation
         string`) and optionally a mapping object, and a *default
         value*, return a :term:`translation string` object."""
+
+        # if we are passing in a TranslationString as the msgid, then
+        # use its domain
+        if isinstance(msgid, TranslationString):
+            domain = msgid.domain or factory_domain
+        else:
+            domain = factory_domain
+
         return TranslationString(msgid, domain=domain, default=default,
-                                 mapping=mapping)
+                                 mapping=mapping, context=context)
     return create
 
 def ChameleonTranslate(translator):
@@ -202,8 +217,7 @@ def ChameleonTranslate(translator):
         tstring = msgid
 
         if not hasattr(tstring, 'interpolate'):
-            tstring = TranslationString(msgid, domain, default, mapping)
-
+            tstring = TranslationString(msgid, domain, default, mapping, context)
         if translator is None:
             result = tstring.interpolate()
         else:
@@ -213,7 +227,7 @@ def ChameleonTranslate(translator):
 
     return translate
 
-def ugettext_policy(translations, tstring, domain):
+def ugettext_policy(translations, tstring, domain, context):
     """ A translator policy function which unconditionally uses the
     ``ugettext`` API on the translations object."""
 
@@ -222,24 +236,39 @@ def ugettext_policy(translations, tstring, domain):
     else: # pragma: no cover
         _gettext = translations.ugettext
 
-    return _gettext(tstring)
+    if context:
+	# Workaround for http://bugs.python.org/issue2504?
+        msgid = CONTEXT_MASK % (context, tstring)
+    else:
+        msgid = tstring
+
+    translated = _gettext(msgid)
+    return tstring if translated == msgid else translated
 
-def dugettext_policy(translations, tstring, domain):
+def dugettext_policy(translations, tstring, domain, context):
     """ A translator policy function which assumes the use of a
     :class:`babel.support.Translations` translations object, which
     supports the dugettext API; fall back to ugettext."""
     if domain is None:
         default_domain = getattr(translations, 'domain', None) or 'messages'
         domain = getattr(tstring, 'domain', None) or default_domain
-    if getattr(translations, 'dugettext', None) is not None:
-        return translations.dugettext(domain, tstring)
+    context = context or getattr(tstring, 'context', None)
+    if context:
+	# Workaround for http://bugs.python.org/issue2504?
+        msgid = CONTEXT_MASK % (context, tstring)
+    else:
+        msgid = tstring
 
-    if PY3: # pragma: no cover
-        _gettext = translations.gettext
-    else: # pragma: no cover
-        _gettext = translations.ugettext
+    if getattr(translations, 'dugettext', None) is not None:
+        translated = translations.dugettext(domain, msgid)
+    else:
+        if PY3: # pragma: no cover
+            _gettext = translations.gettext
+        else: # pragma: no cover
+            _gettext = translations.ugettext
 
-    return _gettext(tstring)
+        translated = _gettext(msgid)
+    return tstring if translated == msgid else translated
 
 def Translator(translations=None, policy=None):
     """
@@ -269,19 +298,21 @@ def Translator(translations=None, policy=None):
     """
     if policy is None:
         policy = dugettext_policy
-    def translator(tstring, domain=None, mapping=None):
+    def translator(tstring, domain=None, mapping=None, context=None):
         if not hasattr(tstring, 'interpolate'):
-            tstring = TranslationString(tstring, domain=domain, mapping=mapping)
+            tstring = TranslationString(tstring, domain=domain, mapping=mapping, context=context)
         elif mapping:
             if tstring.mapping:
                 new_mapping = tstring.mapping.copy()
                 new_mapping.update(mapping)
             else:
                 new_mapping = mapping
-            tstring = TranslationString(tstring, domain=domain, mapping=new_mapping)
+            tstring = TranslationString(tstring, domain=domain, mapping=new_mapping, context=context)
         translated = tstring
+        domain = domain or tstring.domain
+        context = context or tstring.context
         if translations is not None:
-            translated = policy(translations, tstring, domain)
+            translated = policy(translations, tstring, domain, context)
         if translated == tstring:
             translated = tstring.default
         if translated and '$' in translated and tstring.mapping:
@@ -289,7 +320,7 @@ def Translator(translations=None, policy=None):
         return translated
     return translator
 
-def ungettext_policy(translations, singular, plural, n, domain):
+def ungettext_policy(translations, singular, plural, n, domain, context):
     """ A pluralizer policy function which unconditionally uses the
     ``ungettext`` API on the translations object."""
 
@@ -298,25 +329,37 @@ def ungettext_policy(translations, singular, plural, n, domain):
     else: # pragma: no cover
         _gettext = translations.ungettext
 
-    return _gettext(singular, plural, n)
+    if context:
+	# Workaround for http://bugs.python.org/issue2504?
+        msgid = CONTEXT_MASK % (context, singular)
+    else:
+        msgid = singular
 
-def dungettext_policy(translations, singular, plural, n, domain):
+    translated = _gettext(msgid, plural, n)
+    return singular if translated == msgid else translated
+
+def dungettext_policy(translations, singular, plural, n, domain, context):
     """ A pluralizer policy function which assumes the use of the
     :class:`babel.support.Translations` class, which supports the
     dungettext API; falls back to ungettext."""
 
     default_domain = getattr(translations, 'domain', None) or 'messages'
     domain = domain or default_domain
-
+    if context:
+	# Workaround for http://bugs.python.org/issue2504?
+        msgid = CONTEXT_MASK % (context, singular)
+    else:
+        msgid = singular
     if getattr(translations, 'dungettext', None) is not None:
-        return translations.dungettext(domain, singular, plural, n)
-
-    if PY3: # pragma: no cover
-        _gettext = translations.ngettext
-    else: # pragma: no cover
-        _gettext = translations.ungettext
+        translated = translations.dungettext(domain, msgid, plural, n)
+    else:
+        if PY3: # pragma: no cover
+            _gettext = translations.ngettext
+        else: # pragma: no cover
+            _gettext = translations.ungettext
 
-    return _gettext(singular, plural, n)
+        translated = _gettext(msgid, plural, n)
+    return singular if translated == msgid else translated
 
 def Pluralizer(translations=None, policy=None):
     """
@@ -352,10 +395,10 @@ def Pluralizer(translations=None, policy=None):
         policy = dungettext_policy
     if translations is None:
         translations = NullTranslations()
-    def pluralizer(singular, plural, n, domain=None, mapping=None):
+    def pluralizer(singular, plural, n, domain=None, mapping=None, context=None):
         """ Pluralize this object """
         translated = text_type(
-            policy(translations, singular, plural, n, domain))
+            policy(translations, singular, plural, n, domain, context))
         if translated and '$' in translated and mapping:
             return TranslationString(translated, mapping=mapping).interpolate()
         return translated
diff --git a/translationstring/tests/test__init__.py b/translationstring/tests/test__init__.py
index f76cb8e..03ecf3f 100644
--- a/translationstring/tests/test__init__.py
+++ b/translationstring/tests/test__init__.py
@@ -118,14 +118,14 @@ class TestTranslationString(unittest.TestCase):
                              mapping='mapping')
         result = inst.__reduce__()
         self.assertEqual(result, (klass, (u('msgid'), 'domain', u('default'), 
-                                          'mapping')))
+                                          'mapping', None)))
 
     def test___getstate__(self):
         inst = self._makeOne('msgid', default='default', domain='domain',
                              mapping='mapping')
         result = inst.__getstate__()
         self.assertEqual(result,
-                         (u('msgid'), 'domain', u('default'), 'mapping'))
+                         (u('msgid'), 'domain', u('default'), 'mapping', None))
 
 class TestTranslationStringFactory(unittest.TestCase):
     def _makeOne(self, domain):
@@ -140,6 +140,27 @@ class TestTranslationStringFactory(unittest.TestCase):
         self.assertEqual(inst.mapping, 'mapping')
         self.assertEqual(inst.default, 'default')
 
+    def test_msgid_is_translation_string_override_domain(self):
+        user_factory = self._makeOne('user')
+        factory = self._makeOne('budge')
+
+        wrapped_inst = user_factory('wrapped_msgid', mapping={'a':1}, default='default')
+        wrapper_inst = factory(wrapped_inst)
+
+        self.assertEqual(str(wrapper_inst), 'wrapped_msgid')
+        self.assertEqual(wrapper_inst.domain, 'user')
+
+    def test_msgid_is_translation_string_override_kwarg(self):
+        user_factory = self._makeOne('user')
+        factory = self._makeOne('budge')
+
+        wrapped_inst = user_factory('wrapped_msgid', mapping={'a':1}, default='default')
+        wrapper_inst = factory(wrapped_inst, mapping={'b':1}, default='other_default')
+
+        self.assertEqual(str(wrapper_inst), 'wrapped_msgid')
+        self.assertEqual(wrapper_inst.mapping, {'b':1})
+        self.assertEqual(wrapper_inst.default, 'other_default')
+
 
 class TestChameleonTranslate(unittest.TestCase):
     def _makeOne(self, translator):
@@ -232,7 +253,7 @@ class TestTranslator(unittest.TestCase):
 
     def test_policy_returns_msgid(self):
         tstring = DummyTranslationString('msgid', default='default')
-        def policy(translations, msg, domain):
+        def policy(translations, msg, domain, context):
             return msg
         inst = self._makeOne('ignoreme', policy)
         result = inst(tstring)
@@ -240,7 +261,7 @@ class TestTranslator(unittest.TestCase):
 
     def test_policy_returns_translation(self):
         tstring = DummyTranslationString('msgid')
-        def policy(translations, msg, domain):
+        def policy(translations, msg, domain, context):
             return 'translated'
         inst = self._makeOne('ignoreme', policy)
         result = inst(tstring)
@@ -263,7 +284,7 @@ class TestPluralizer(unittest.TestCase):
 
     def test_policy_returns_translated(self):
         translations = DummyTranslations('result')
-        def policy(translations, singular, plural, n, domain):
+        def policy(translations, singular, plural, n, domain, context):
             return 'translated'
         inst = self._makeOne(translations, policy)
         tstring = DummyTranslationString('msgid')
@@ -271,19 +292,31 @@ class TestPluralizer(unittest.TestCase):
         self.assertEqual(result, 'translated')
 
 class Test_ugettext_policy(unittest.TestCase):
-    def _callFUT(self, translations, tstring, domain):
+    def _callFUT(self, translations, tstring, domain, context):
         from translationstring import ugettext_policy
-        return ugettext_policy(translations, tstring, domain)
+        return ugettext_policy(translations, tstring, domain, context)
 
     def test_it(self):
         translations = DummyTranslations('result')
-        result = self._callFUT(translations, 'string', None)
+        result = self._callFUT(translations, 'string', None, None)
+        self.assertEqual(result, 'result')
+
+    def test_msgctxt(self):
+        translations = DummyTranslations('result')
+        result = self._callFUT(translations, u('p\xf8f'), None, 'button')
+        self.assertEqual(translations.params, (u('button\x04p\xf8f'),))
         self.assertEqual(result, 'result')
 
+    def test_msgctxt_no_translation_found(self):
+        input = u('p\xf8f')
+        translations = DummyTranslations(input)
+        result = self._callFUT(translations, input, None, 'button')
+        self.assertEqual(result, u('p\xf8f'))
+
 class Test_dugettext_policy(unittest.TestCase):
-    def _callFUT(self, translations, tstring, domain):
+    def _callFUT(self, translations, tstring, domain, context=None):
         from translationstring import dugettext_policy
-        return dugettext_policy(translations, tstring, domain)
+        return dugettext_policy(translations, tstring, domain, context)
 
     def test_it_use_default_domain(self):
         translations = DummyTranslations('result', domain=None)
@@ -320,22 +353,54 @@ class Test_dugettext_policy(unittest.TestCase):
         result = self._callFUT(translations, tstring, None)
         self.assertEqual(result, 'result')
 
+    def test_msgctxt_from_tstring(self):
+        translations = DummyTranslations('result')
+        tstring = DummyTranslationString(u('p\xf8f'), context='button')
+        result = self._callFUT(translations, tstring, None)
+        self.assertEqual(translations.params, ('messages', u('button\x04p\xf8f'),))
+        self.assertEqual(result, 'result')
+
+    def test_msgctxt_override(self):
+        translations = DummyTranslations('result')
+        tstring = DummyTranslationString(u('p\xf8f'), context='other')
+        result = self._callFUT(translations, tstring, None, context='button')
+        self.assertEqual(translations.params, ('messages', u('button\x04p\xf8f'),))
+        self.assertEqual(result, 'result')
+
+    def test_msgctxt_no_translation_found(self):
+        translations = DummyTranslations(u('button\x04p\xf8f'))
+        tstring = DummyTranslationString(u('p\xf8f'), context='button')
+        result = self._callFUT(translations, tstring, None)
+        self.assertEqual(result, u('p\xf8f'))
+
 class Test_ungettext_policy(unittest.TestCase):
     def _callFUT(self, translations, singular, plural, n, domain=None,
-                 mapping=None):
+                 mapping=None, context=None):
         from translationstring import ungettext_policy
-        return ungettext_policy(translations, singular, plural, n, domain)
+        return ungettext_policy(translations, singular, plural, n, domain, context)
 
     def test_it(self):
         translations = DummyTranslations('result')
         result = self._callFUT(translations, 'singular', 'plural', 1)
         self.assertEqual(result, 'result')
 
+    def test_msgctxt(self):
+        translations = DummyTranslations('result')
+        result = self._callFUT(translations, u('p\xf8f'), 'plural', 1, context='button')
+        self.assertEqual(translations.params, (u('button\x04p\xf8f'), 'plural', 1))
+        self.assertEqual(result, 'result')
+
+    def test_msgctxt_no_translation(self):
+        translations = DummyTranslations(u('button\x04p\xf8f'))
+        result = self._callFUT(translations, u('p\xf8f'), 'plural', 1, context='button')
+        self.assertEqual(translations.params, (u('button\x04p\xf8f'), 'plural', 1))
+        self.assertEqual(result, u('p\xf8f'))
+
 class Test_dungettext_policy(unittest.TestCase):
     def _callFUT(self, translations, singular, plural, n, domain=None,
-                 mapping=None):
+                 mapping=None, context=None):
         from translationstring import dungettext_policy
-        return dungettext_policy(translations, singular, plural, n, domain)
+        return dungettext_policy(translations, singular, plural, n, domain, context)
 
     def test_it_use_default_domain(self):
         translations = DummyTranslations('result')
@@ -367,30 +432,37 @@ class DummyTranslations(object):
         self.domain = domain
         
     def gettext(self, tstring): # pragma: no cover
+        self.params = (tstring,)
         return self.result
     
     def ngettext(self, singular, plural, n): # pragma: no cover
+        self.params = (singular, plural, n)
         return self.result
 
     def ugettext(self, tstring): # pragma: no cover
+        self.params = (tstring,)
         return self.result
 
     def dugettext(self, domain, tstring): # pragma: no cover
+        self.params = (domain, tstring)
         self.asked_domain = domain
         return self.result
 
     def ungettext(self, singular, plural, n): # pragma: no cover
+        self.params = (singular, plural, n)
         return self.result
 
     def dungettext(self, domain, singular, plural, n): # pragma: no cover
+        self.params = (domain, singular, plural, n)
         self.asked_domain = domain
         return self.result
 
 class DummyTranslationString(text_type):
-    def __new__(cls, msgid='', domain=None, default=None, mapping=None):
+    def __new__(cls, msgid='', domain=None, default=None, mapping=None, context=None):
         self = text_type.__new__(cls, msgid)
         text_type.__init__(self, msgid)
         self.domain = domain
+        self.context = context
         self.mapping = mapping
         if default is None:
             default = msgid

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-translationstring.git



More information about the Python-modules-commits mailing list