[Checkins] SVN: Zope3/trunk/ Merged revision 68769 from the 3.3 branch:

Dmitry Vasilev cvs-admin at zope.org
Tue Jun 20 07:24:34 EDT 2006


Log message for revision 68771:
  Merged revision 68769 from the 3.3 branch:
  
    Fixed issue 531: Spurious i18n deprecation warnings.
    
    Also a little bit restructured tests and removed long ago deprecated
    behaviour of translate().
  

Changed:
  U   Zope3/trunk/doc/CHANGES.txt
  U   Zope3/trunk/src/zope/app/i18n/translationdomain.py
  U   Zope3/trunk/src/zope/i18n/__init__.py
  U   Zope3/trunk/src/zope/i18n/simpletranslationdomain.py
  U   Zope3/trunk/src/zope/i18n/tests/test.py
  D   Zope3/trunk/src/zope/i18n/tests/test_interpolate.py
  U   Zope3/trunk/src/zope/i18n/tests/test_itranslationdomain.py
  U   Zope3/trunk/src/zope/i18n/translationdomain.py

-=-
Modified: Zope3/trunk/doc/CHANGES.txt
===================================================================
--- Zope3/trunk/doc/CHANGES.txt	2006-06-20 10:40:34 UTC (rev 68770)
+++ Zope3/trunk/doc/CHANGES.txt	2006-06-20 11:24:31 UTC (rev 68771)
@@ -16,6 +16,8 @@
 
     Bug fixes
 
+      - Fixed issue 531: Spurious i18n deprecation warnings;
+
       - Fixed issue 639: Default ITraverser ignores dict methods
 
       - Fixed issue 636: Default ITraverser can't traverse old style

Modified: Zope3/trunk/src/zope/app/i18n/translationdomain.py
===================================================================
--- Zope3/trunk/src/zope/app/i18n/translationdomain.py	2006-06-20 10:40:34 UTC (rev 68770)
+++ Zope3/trunk/src/zope/app/i18n/translationdomain.py	2006-06-20 11:24:31 UTC (rev 68771)
@@ -32,6 +32,7 @@
 from zope.app.container.contained import Contained
 from zope.app.component import queryNextUtility
 
+
 class TranslationDomain(BTreeContainer, SimpleTranslationDomain, Contained):
     implements(ILocalTranslationDomain)
 
@@ -86,7 +87,7 @@
                 return domain.translate(msgid, mapping, context,
                                         target_language, default=default)
             if default is None:
-                default = msgid
+                default = unicode(msgid)
             text = default
 
         # Now we need to do the interpolation

Modified: Zope3/trunk/src/zope/i18n/__init__.py
===================================================================
--- Zope3/trunk/src/zope/i18n/__init__.py	2006-06-20 10:40:34 UTC (rev 68770)
+++ Zope3/trunk/src/zope/i18n/__init__.py	2006-06-20 11:24:31 UTC (rev 68771)
@@ -16,7 +16,6 @@
 $Id$
 """
 import re
-import warnings
 
 from zope.i18nmessageid import MessageFactory, Message
 from zope.i18n.interfaces import ITranslationDomain
@@ -32,16 +31,67 @@
 _interp_regex = re.compile(r'(?<!\$)(\$(?:(%(n)s)|{(%(n)s)}))'
     % ({'n': NAME_RE}))
 
-def _translate(msgid, domain=None, mapping=None, context=None,
+def translate(msgid, domain=None, mapping=None, context=None,
                target_language=None, default=None):
+    """Translate text.
 
+    First setup some test components:
+
+    >>> from zope import component, interface
+    >>> import zope.i18n.interfaces
+
+    >>> class TestDomain:
+    ...     interface.implements(zope.i18n.interfaces.ITranslationDomain)
+    ...
+    ...     def __init__(self, **catalog):
+    ...         self.catalog = catalog
+    ...
+    ...     def translate(self, text, *_, **__):
+    ...         return self.catalog[text]
+
+    Normally, the translation system will use a domain utility:
+
+    >>> component.provideUtility(TestDomain(eek=u'ook'), name='my.domain')
+    >>> translate(u'eek', 'my.domain')
+    u'ook'
+
+    Normally, if no domain is given, or if there is no domain utility
+    for the given domain, then the text isn't translated:
+
+    >>> translate(u'eek')
+    u'eek'
+
+    Moreover the text will be converted to unicode:
+
+    >>> translate('eek', 'your.domain')
+    u'eek'
+
+    A fallback domain factory can be provided. This is normally used
+    for testing:
+
+    >>> def fallback(domain=u''):
+    ...     return TestDomain(eek=u'test-from-' + domain)
+    >>> interface.directlyProvides(
+    ...     fallback,
+    ...     zope.i18n.interfaces.IFallbackTranslationDomainFactory,
+    ...     )
+
+    >>> component.provideUtility(fallback)
+
+    >>> translate(u'eek')
+    u'test-from-'
+
+    >>> translate(u'eek', 'your.domain')
+    u'test-from-your.domain'
+    """
+
     if isinstance(msgid, Message):
         domain = msgid.domain
         default = msgid.default
         mapping = msgid.mapping
 
     if default is None:
-        default = msgid
+        default = unicode(msgid)
 
     if domain:
         util = queryUtility(ITranslationDomain, domain)
@@ -59,16 +109,6 @@
 
     return util.translate(msgid, mapping, context, target_language, default)
 
-# BBB Backward compat
-def translate(*args, **kw):
-    if args and not isinstance(args[0], basestring):
-        warnings.warn(
-            "translate no longer takes a location argument. "
-            "The argument was ignored.",
-            DeprecationWarning, 2)
-        args = args[1:]
-    return _translate(*args, **kw)
-
 def interpolate(text, mapping=None):
     """Insert the data passed from mapping into the text.
 

Modified: Zope3/trunk/src/zope/i18n/simpletranslationdomain.py
===================================================================
--- Zope3/trunk/src/zope/i18n/simpletranslationdomain.py	2006-06-20 10:40:34 UTC (rev 68770)
+++ Zope3/trunk/src/zope/i18n/simpletranslationdomain.py	2006-06-20 11:24:31 UTC (rev 68771)
@@ -20,6 +20,7 @@
 from zope.i18n.interfaces import ITranslationDomain, INegotiator
 from zope.i18n import interpolate
 
+
 class SimpleTranslationDomain(object):
     """This is the simplest implementation of the ITranslationDomain I
        could come up with.
@@ -60,7 +61,7 @@
         # Find a translation; if nothing is found, use the default
         # value
         if default is None:
-            default = msgid
+            default = unicode(msgid)
         text = self.messages.get((target_language, msgid))
         if text is None:
             text = default

Modified: Zope3/trunk/src/zope/i18n/tests/test.py
===================================================================
--- Zope3/trunk/src/zope/i18n/tests/test.py	2006-06-20 10:40:34 UTC (rev 68770)
+++ Zope3/trunk/src/zope/i18n/tests/test.py	2006-06-20 11:24:31 UTC (rev 68771)
@@ -17,63 +17,13 @@
 """
 import unittest
 
-from zope import component, interface
+from zope.testing import doctest
 from zope.component.testing import setUp, tearDown
-import zope.i18n.interfaces
-from zope.i18n import _translate
 
-class TestDomain:
-    interface.implements(zope.i18n.interfaces.ITranslationDomain)
 
-    def __init__(self, **catalog):
-        self.catalog = catalog
-
-    def translate(self, text, *_, **__):
-        return self.catalog[text]
-        
-
-def test_fallback_domain():
-    """\
-Normally, the translation system will use a domain utility:
-
-    >>> component.provideUtility(TestDomain(eek=u'ook'), name='my.domain')
-    >>> _translate(u'eek', 'my.domain')
-    u'ook'
-
-Normally, if no domain is given, or if there is no domain utility
-for the given domain, then the text isn't translated:
-
-    >>> _translate(u'eek')
-    u'eek'
-
-    >>> _translate(u'eek', 'your.domain')
-    u'eek'
-
-A fallback domain factory can be provided. This is normally used for testing:
-
-    >>> def fallback(domain=u''):
-    ...     return TestDomain(eek=u'test-from-' + domain)
-    >>> interface.directlyProvides(
-    ...     fallback,
-    ...     zope.i18n.interfaces.IFallbackTranslationDomainFactory,
-    ...     )
-
-    >>> component.provideUtility(fallback)
-
-    >>> _translate(u'eek')
-    u'test-from-'
-
-    >>> _translate(u'eek', 'your.domain')
-    u'test-from-your.domain'
-    
-    """
-
 def test_suite():
-    from zope.testing import doctest
-    return unittest.TestSuite((
-        doctest.DocTestSuite(setUp=setUp, tearDown=tearDown),
-        ))
+    return doctest.DocTestSuite("zope.i18n", setUp=setUp, tearDown=tearDown)
 
+
 if __name__ == '__main__':
     unittest.main(defaultTest='test_suite')
-

Deleted: Zope3/trunk/src/zope/i18n/tests/test_interpolate.py
===================================================================
--- Zope3/trunk/src/zope/i18n/tests/test_interpolate.py	2006-06-20 10:40:34 UTC (rev 68770)
+++ Zope3/trunk/src/zope/i18n/tests/test_interpolate.py	2006-06-20 11:24:31 UTC (rev 68771)
@@ -1,28 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Tests for zope.i18n.interpolate.
-
-$Id$
-"""
-import unittest
-
-from zope.testing import doctest
-
-
-def test_suite():
-    return doctest.DocTestSuite("zope.i18n")
-
-
-if __name__=='__main__':
-    unittest.TextTestRunner().run(test_suite())

Modified: Zope3/trunk/src/zope/i18n/tests/test_itranslationdomain.py
===================================================================
--- Zope3/trunk/src/zope/i18n/tests/test_itranslationdomain.py	2006-06-20 10:40:34 UTC (rev 68770)
+++ Zope3/trunk/src/zope/i18n/tests/test_itranslationdomain.py	2006-06-20 11:24:31 UTC (rev 68771)
@@ -26,6 +26,7 @@
 from zope.i18n.interfaces import INegotiator, IUserPreferredLanguages
 from zope.i18n.interfaces import ITranslationDomain
 
+
 class Environment(object):
 
     implements(IUserPreferredLanguages)
@@ -47,7 +48,7 @@
         self._domain = self._getTranslationDomain()
 
         # Setup the negotiator utility
-        zope.component.provideUtility(negotiator, INegotiator)        
+        zope.component.provideUtility(negotiator, INegotiator)
 
     def testInterface(self):
         verifyObject(ITranslationDomain, self._domain)
@@ -86,6 +87,12 @@
                      default='Glorp Smurf Hmpf'),
            'Glorp Smurf Hmpf')
 
+    def testUnicodeDefaultValue(self):
+        translate = self._domain.translate
+        translated = translate('no way', target_language='en')
+        self.assertEqual(translated, "no way")
+        self.assert_(type(translated) is unicode)
+
     def testNoTargetLanguage(self):
         translate = self._domain.translate
         eq = self.assertEqual

Modified: Zope3/trunk/src/zope/i18n/translationdomain.py
===================================================================
--- Zope3/trunk/src/zope/i18n/translationdomain.py	2006-06-20 10:40:34 UTC (rev 68770)
+++ Zope3/trunk/src/zope/i18n/translationdomain.py	2006-06-20 11:24:31 UTC (rev 68771)
@@ -85,7 +85,7 @@
             default = msgid.default
 
         if default is None:
-            default = msgid
+            default = unicode(msgid)
 
         # Get the translation. Use the specified fallbacks if this fails
         catalog_names = self._catalogs.get(target_language)
@@ -123,4 +123,3 @@
     def reloadCatalogs(self, catalogNames):
         for catalogName in catalogNames:
             self._data[catalogName].reload()
-



More information about the Checkins mailing list