[Zope3-checkins] SVN: Zope3/trunk/src/zope/ Fix the translate() methods of TranslationDomain and

Philipp von Weitershausen philikon at philikon.de
Tue Oct 11 14:36:33 EDT 2005


Log message for revision 39062:
  
  Fix the translate() methods of TranslationDomain and
  SimpleTranslationDomain so that it works with immutable Messages.  As
  a side effect, they now comply with what the ITranslationDomain
  interface says: "Return the default if no translation is found."
  
  Also work around deprecation warnings that would result from the (still
  necessary) import of the old-style mutable MessageID implementation.
  
  

Changed:
  U   Zope3/trunk/src/zope/app/i18n/tests/test_translationdomain.py
  U   Zope3/trunk/src/zope/app/i18n/translationdomain.py
  U   Zope3/trunk/src/zope/i18n/simpletranslationdomain.py
  U   Zope3/trunk/src/zope/i18n/tests/test_itranslationdomain.py
  U   Zope3/trunk/src/zope/i18n/tests/test_translationdomain.py
  U   Zope3/trunk/src/zope/i18n/translationdomain.py

-=-
Modified: Zope3/trunk/src/zope/app/i18n/tests/test_translationdomain.py
===================================================================
--- Zope3/trunk/src/zope/app/i18n/tests/test_translationdomain.py	2005-10-11 18:36:20 UTC (rev 39061)
+++ Zope3/trunk/src/zope/app/i18n/tests/test_translationdomain.py	2005-10-11 18:36:32 UTC (rev 39062)
@@ -69,7 +69,6 @@
         languages.sort()
         return languages
 
-
     def testGetAddDeleteLanguage(self):
         domain = self._domain
         langs = self._getLanguages(domain)
@@ -84,24 +83,21 @@
         domain.deleteLanguage('fr')
         self.assertEqual(self._getLanguages(domain), langs)
 
-
     def testAddUpdateDeleteMessage(self):
         domain = self._domain
         self.assertEqual(domain.translate('greeting2', target_language='de'),
-                         None)
-        self.assertEqual(domain.translate('greeting2', 
-                                           target_language='de', default=42),
-                         42)
+                         'greeting2')
+        self.assertEqual(domain.translate(
+            'greeting2', target_language='de', default=42), 42)
         domain.addMessage('greeting2', 'Hallo!', 'de')
-        self.assertEqual(domain.translate('greeting2',
-                                           target_language='de'), 'Hallo!')
+        self.assertEqual(domain.translate('greeting2', target_language='de'),
+                         'Hallo!')
         domain.updateMessage('greeting2', 'Hallo Ihr da!', 'de')
-        self.assertEqual(domain.translate('greeting2',
-                                           target_language='de'),
+        self.assertEqual(domain.translate('greeting2', target_language='de'),
                          'Hallo Ihr da!')
         domain.deleteMessage('greeting2', 'de')
-        self.assertEqual(domain.translate('greeting2',
-                                           target_language='de'), None)
+        self.assertEqual(domain.translate('greeting2', target_language='de'),
+                         'greeting2')
 
 
 # A test mixing -- don't add this to the suite

Modified: Zope3/trunk/src/zope/app/i18n/translationdomain.py
===================================================================
--- Zope3/trunk/src/zope/app/i18n/translationdomain.py	2005-10-11 18:36:20 UTC (rev 39061)
+++ Zope3/trunk/src/zope/app/i18n/translationdomain.py	2005-10-11 18:36:32 UTC (rev 39062)
@@ -86,8 +86,9 @@
             if domain is not None:
                 return domain.translate(msgid, mapping, context,
                                         target_language, default=default)
-            else:
-                text = default
+            if default is None:
+                default = msgid
+            text = default
 
         # Now we need to do the interpolation
         return interpolate(text, mapping)

Modified: Zope3/trunk/src/zope/i18n/simpletranslationdomain.py
===================================================================
--- Zope3/trunk/src/zope/i18n/simpletranslationdomain.py	2005-10-11 18:36:20 UTC (rev 39061)
+++ Zope3/trunk/src/zope/i18n/simpletranslationdomain.py	2005-10-11 18:36:32 UTC (rev 39062)
@@ -59,6 +59,8 @@
 
         # Find a translation; if nothing is found, use the default
         # value
+        if default is None:
+            default = msgid
         text = self.messages.get((target_language, msgid))
         if text is None:
             text = default

Modified: Zope3/trunk/src/zope/i18n/tests/test_itranslationdomain.py
===================================================================
--- Zope3/trunk/src/zope/i18n/tests/test_itranslationdomain.py	2005-10-11 18:36:20 UTC (rev 39061)
+++ Zope3/trunk/src/zope/i18n/tests/test_itranslationdomain.py	2005-10-11 18:36:32 UTC (rev 39062)
@@ -78,9 +78,9 @@
     def testNoTranslation(self):
         translate = self._domain.translate
         eq = self.assertEqual
-        # Test that an unknown message id returns None as a translation
+        # Verify that an unknown message id will end up not being translated
         eq(translate('glorp_smurf_hmpf', target_language='en'),
-           None)
+           'glorp_smurf_hmpf')
         # Test default value behaviour
         eq(translate('glorp_smurf_hmpf', target_language='en',
                      default='Glorp Smurf Hmpf'),

Modified: Zope3/trunk/src/zope/i18n/tests/test_translationdomain.py
===================================================================
--- Zope3/trunk/src/zope/i18n/tests/test_translationdomain.py	2005-10-11 18:36:20 UTC (rev 39061)
+++ Zope3/trunk/src/zope/i18n/tests/test_translationdomain.py	2005-10-11 18:36:32 UTC (rev 39062)
@@ -59,19 +59,15 @@
 
         # Test that a translation in an unsupported language returns the
         # default, if there is no fallback language
-        eq(translate('short_greeting', target_language='es'),
-           None)
+        eq(translate('short_greeting', target_language='es'), 'short_greeting')
         eq(translate('short_greeting', target_language='es',
-                     default='short_greeting'),
-           'short_greeting')
+                     default='short_greeting'), 'short_greeting')
 
         # Same test, but use the context argument instead of target_language
         context = Environment()
-        eq(translate('short_greeting', context=context),
-           None)
+        eq(translate('short_greeting', context=context), 'short_greeting')
         eq(translate('short_greeting', context=context,
-                     default='short_greeting'),
-           'short_greeting')
+                     default='short_greeting'), 'short_greeting')
 
     def testEmptyStringTranslate(self):
         translate = self._domain.translate

Modified: Zope3/trunk/src/zope/i18n/translationdomain.py
===================================================================
--- Zope3/trunk/src/zope/i18n/translationdomain.py	2005-10-11 18:36:20 UTC (rev 39061)
+++ Zope3/trunk/src/zope/i18n/translationdomain.py	2005-10-11 18:36:32 UTC (rev 39062)
@@ -15,9 +15,14 @@
 
 $Id$
 """
+# BBB 2005/10/10
+import zope.deprecation
+zope.deprecation.__show__.off()
+from zope.i18nmessageid import MessageID, Message
+zope.deprecation.__show__.on()
+
+from zope.i18n import interpolate
 from zope.i18n.simpletranslationdomain import SimpleTranslationDomain
-from zope.i18n.messageid import MessageID
-from zope.i18n import interpolate
 from zope.component import getUtility
 from zope.i18n.interfaces import ITranslationDomain
 from zope.i18n.interfaces import INegotiator
@@ -64,7 +69,7 @@
 
     def translate(self, msgid, mapping=None, context=None,
                   target_language=None, default=None):
-        '''See interface ITranslationService'''
+        """See zope.i18n.interfaces.ITranslationDomain"""
 
         # if the msgid is empty, let's save a lot of calculations and return
         # an empty string.
@@ -79,15 +84,17 @@
             target_language = negotiator.getLanguage(langs, context)
 
         # MessageID attributes override arguments
-        if isinstance(msgid, MessageID):
+        if isinstance(msgid, (Message, MessageID)):
             if msgid.domain != self.domain:
                 util = getUtility(ITranslationDomain, msgid.domain)
                 return util.translate(msgid, mapping, context,
                                       target_language, default)
-            else:
-                mapping = msgid.mapping
-                default = msgid.default
+            mapping = msgid.mapping
+            default = msgid.default
 
+        if default is None:
+            default = msgid
+
         # Get the translation. Use the specified fallbacks if this fails
         catalog_names = self._catalogs.get(target_language)
         if catalog_names is None:



More information about the Zope3-Checkins mailing list