# HG changeset patch # User fschwarz # Date 1300562939 0 # Node ID 10de195cfb0421267fc6c28c369607724ee6d5b6 # Parent 1d386483ccb66052c0292fcb2fd3ff510c774226 catalog.add() now returns the message instance (closes #245) diff --git a/babel/messages/catalog.py b/babel/messages/catalog.py --- a/babel/messages/catalog.py +++ b/babel/messages/catalog.py @@ -607,6 +607,7 @@ >>> catalog = Catalog() >>> catalog.add(u'foo') + >>> catalog[u'foo'] @@ -626,10 +627,14 @@ :param lineno: the line number on which the msgid line was found in the PO file, if any :param context: the message context + :return: the newly added message + :rtype: `Message` """ - self[id] = Message(id, string, list(locations), flags, auto_comments, - user_comments, previous_id, lineno=lineno, - context=context) + message = Message(id, string, list(locations), flags, auto_comments, + user_comments, previous_id, lineno=lineno, + context=context) + self[id] = message + return message def check(self): """Run various validation checks on the translations in the catalog. @@ -672,13 +677,19 @@ >>> from babel.messages import Catalog >>> template = Catalog() >>> template.add('green', locations=[('main.py', 99)]) + >>> template.add('blue', locations=[('main.py', 100)]) + >>> template.add(('salad', 'salads'), locations=[('util.py', 42)]) + >>> catalog = Catalog(locale='de_DE') >>> catalog.add('blue', u'blau', locations=[('main.py', 98)]) + >>> catalog.add('head', u'Kopf', locations=[('util.py', 33)]) + >>> catalog.add(('salad', 'salads'), (u'Salat', u'Salate'), ... locations=[('util.py', 38)]) + >>> catalog.update(template) >>> len(catalog) diff --git a/babel/messages/mofile.py b/babel/messages/mofile.py --- a/babel/messages/mofile.py +++ b/babel/messages/mofile.py @@ -125,10 +125,15 @@ >>> catalog = Catalog(locale='en_US') >>> catalog.add('foo', 'Voh') + >>> catalog.add((u'bar', u'baz'), (u'Bahr', u'Batz')) + >>> catalog.add('fuz', 'Futz', flags=['fuzzy']) + >>> catalog.add('Fizz', '') + >>> catalog.add(('Fuzz', 'Fuzzes'), ('', '')) + >>> buf = StringIO() >>> write_mo(buf, catalog) diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -345,7 +345,9 @@ >>> catalog = Catalog() >>> catalog.add(u'foo %(name)s', locations=[('main.py', 1)], ... flags=('fuzzy',)) + >>> catalog.add((u'bar', u'baz'), locations=[('main.py', 3)]) + >>> from StringIO import StringIO >>> buf = StringIO() >>> write_po(buf, catalog, omit_header=True) diff --git a/babel/messages/tests/catalog.py b/babel/messages/tests/catalog.py --- a/babel/messages/tests/catalog.py +++ b/babel/messages/tests/catalog.py @@ -57,6 +57,10 @@ class CatalogTestCase(unittest.TestCase): + def test_add_returns_message_instance(self): + cat = catalog.Catalog() + message = cat.add('foo') + self.assertEquals('foo', message.id) def test_two_messages_with_same_singular(self): cat = catalog.Catalog() diff --git a/babel/messages/tests/mofile.py b/babel/messages/tests/mofile.py --- a/babel/messages/tests/mofile.py +++ b/babel/messages/tests/mofile.py @@ -78,7 +78,7 @@ def suite(): suite = unittest.TestSuite() - suite.addTest(doctest.DocTestSuite(mofile)) + suite.addTest(doctest.DocTestSuite(mofile, optionflags=doctest.ELLIPSIS)) suite.addTest(unittest.makeSuite(ReadMoTestCase)) suite.addTest(unittest.makeSuite(WriteMoTestCase)) return suite diff --git a/babel/messages/tests/pofile.py b/babel/messages/tests/pofile.py --- a/babel/messages/tests/pofile.py +++ b/babel/messages/tests/pofile.py @@ -498,7 +498,7 @@ def suite(): suite = unittest.TestSuite() - suite.addTest(doctest.DocTestSuite(pofile)) + suite.addTest(doctest.DocTestSuite(pofile, optionflags=doctest.ELLIPSIS)) suite.addTest(unittest.makeSuite(ReadPoTestCase)) suite.addTest(unittest.makeSuite(WritePoTestCase)) return suite