[Checkins] SVN: zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/ Changed the API. It's unwieldy as it is, so better have separate methods

Lennart Regebro regebro at gmail.com
Wed Jul 16 12:59:30 EDT 2008


Log message for revision 88417:
  Changed the API. It's unwieldy as it is, so better have separate methods 
  for plurals.
  

Changed:
  U   zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/gettextmessagecatalog.py
  U   zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/tests/en-default.mo
  U   zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/tests/en-default.po
  U   zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/tests/test_imessagecatalog.py

-=-
Modified: zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/gettextmessagecatalog.py
===================================================================
--- zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/gettextmessagecatalog.py	2008-07-16 16:38:14 UTC (rev 88416)
+++ zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/gettextmessagecatalog.py	2008-07-16 16:59:29 UTC (rev 88417)
@@ -23,6 +23,9 @@
 class _KeyErrorRaisingFallback(object):
     def ugettext(self, message):
         raise KeyError(message)
+    
+    def ungettext(self, message, message2, n):
+        raise KeyError(message)
 
 
 class GettextMessageCatalog(object):
@@ -46,31 +49,40 @@
         finally:
             fp.close()
 
-    def getMessage(self, id, n=None):
+    def getMessage(self, id):
         'See IMessageCatalog'
-        if n is None:
-            return self._catalog.ugettext(id)
-        else:
-            msg = self._catalog.ungettext(id, id, n)
-            try:
-                return msg % n
-            except TypeError:
-                return msg                    
+        return self._catalog.ugettext(id)
+    
+    def getPluralMessage(self, id1, id2, n):
+        'See IMessageCatalog'
+        msg = self._catalog.ungettext(id1, id2, n)
+        try:
+            return msg % n
+        except TypeError:
+            return msg                    
 
-    def queryMessage(self, id, default=None, n=None):
+    def queryMessage(self, id, default=None):
         'See IMessageCatalog'
         try:
-            if n is None:
-                return self._catalog.ugettext(id)
-            else:
-                msg = self._catalog.ungettext(id, id, n)
-                try:
-                    return msg % n
-                except TypeError:
-                    return msg                    
+            return self._catalog.ugettext(id)
         except KeyError:
             return default
 
+    def queryPluralMessage(self, id1, id2, n, default1=None, default2=None):
+        'See IMessageCatalog'
+        try:
+            msg = self._catalog.ungettext(id1, id2, n)
+        except KeyError:
+            if n == 1:
+                msg = default1
+            else:
+                msg = default2
+        try:
+            return msg % n
+        except TypeError:
+            return msg                    
+
+
     def getIdentifier(self):
         'See IMessageCatalog'
         return self._path_to_file

Modified: zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/tests/en-default.mo
===================================================================
(Binary files differ)

Modified: zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/tests/en-default.po
===================================================================
--- zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/tests/en-default.po	2008-07-16 16:38:14 UTC (rev 88416)
+++ zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/tests/en-default.po	2008-07-16 16:59:29 UTC (rev 88417)
@@ -14,7 +14,7 @@
 msgid "greeting"
 msgstr "Hello $name, how are you?"
 
-msgid "There are %d files."
+msgid "There is one file."
 msgid_plural "There are %d files."
 msgstr[0] "There is one file."
 msgstr[1] "There are %d files."

Modified: zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/tests/test_imessagecatalog.py
===================================================================
--- zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/tests/test_imessagecatalog.py	2008-07-16 16:38:14 UTC (rev 88416)
+++ zope.i18n/branches/regebro-pluralsupport/src/zope/i18n/tests/test_imessagecatalog.py	2008-07-16 16:59:29 UTC (rev 88417)
@@ -49,13 +49,34 @@
         self.assertEqual(catalog.queryMessage('foo'), None)
         self.assertEqual(catalog.queryMessage('foo', 'bar'), 'bar')
 
-    def testPluralMessage(self):
+    def testGetPluralMessage(self):
         catalog = self._catalog
-        self.assertEqual(catalog.getMessage('There are %d files.', n=1),
+        self.assertEqual(catalog.getPluralMessage(
+                         'There is one file.', 'There are %d files.', 1),
                          'There is one file.')
-        self.assertEqual(catalog.getMessage('There are %d files.', n=3),
+        self.assertEqual(catalog.getPluralMessage(
+                         'There is one file.', 'There are %d files.', 3),
                          'There are 3 files.')
+        self.assertRaises(KeyError, catalog.getPluralMessage, 
+                          'There are %d files.', 'bar', 6)
 
+    def testQeuryPluralMessage(self):
+        catalog = self._catalog
+        self.assertEqual(catalog.queryPluralMessage(
+                         'There is one file.', 'There are %d files.', 1),
+                         'There is one file.')
+        self.assertEqual(catalog.queryPluralMessage(
+                         'There is one file.', 'There are %d files.', 3),
+                         'There are 3 files.')
+        self.assertEqual(catalog.queryPluralMessage(
+                         'There are %d files.', 'There is one file.', 1,
+                         'There is one file.', 'There are %d files.', ),
+                         'There is one file.')
+        self.assertEqual(catalog.queryPluralMessage(
+                         'There are %d files.', 'There is one file.', 3,
+                         'There is one file.', 'There are %d files.', ),
+                         'There are 3 files.')
+        
     def testGetLanguage(self):
         catalog = self._catalog
         self.assertEqual(catalog.language, 'en')



More information about the Checkins mailing list