changeset 205:aefe4ac123a2

Minor changes to how previous msgids are processed.
author cmlenz
date Tue, 03 Jul 2007 21:19:27 +0000
parents 9181a11feb81
children 3be04778442f
files babel/messages/catalog.py babel/messages/pofile.py babel/messages/tests/pofile.py
diffstat 3 files changed, 49 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/babel/messages/catalog.py
+++ b/babel/messages/catalog.py
@@ -40,7 +40,7 @@
     """Representation of a single message in a catalog."""
 
     def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(),
-                 user_comments=(), old_msgid=()):
+                 user_comments=(), previous_id=()):
         """Create the message object.
 
         :param id: the message ID, or a ``(singular, plural)`` tuple for
@@ -51,8 +51,8 @@
         :param flags: a set or sequence of flags
         :param auto_comments: a sequence of automatic comments for the message
         :param user_comments: a sequence of user comments for the message
-        :param old_message: the old message ID, or a ``(singular, plural)``
-                            tuple for old pluralizable messages
+        :param previous_id: the previous message ID, or a ``(singular, plural)``
+                            tuple for pluralizable messages
         """
         self.id = id #: The message ID
         if not string and self.pluralizable:
@@ -66,10 +66,10 @@
             self.flags.discard('python-format')
         self.auto_comments = list(auto_comments)
         self.user_comments = list(user_comments)
-        if isinstance(old_msgid, basestring):
-            self.old_msgid = [old_msgid]
+        if isinstance(previous_id, basestring):
+            self.previous_id = [previous_id]
         else:
-            self.old_msgid = list(old_msgid)
+            self.previous_id = list(previous_id)
 
     def __repr__(self):
         return '<%s %r (flags: %r)>' % (type(self).__name__, self.id,
@@ -469,7 +469,7 @@
             self._messages[key] = message
 
     def add(self, id, string=None, locations=(), flags=(), auto_comments=(),
-            user_comments=(), old_msgid=()):
+            user_comments=(), previous_id=()):
         """Add or update the message with the specified ID.
 
         >>> catalog = Catalog()
@@ -488,12 +488,13 @@
         :param flags: a set or sequence of flags
         :param auto_comments: a sequence of automatic comments
         :param user_comments: a sequence of user comments
+        :param previous_id: the previous message ID, or a ``(singular, plural)``
+                            tuple for pluralizable messages
         """
         self[id] = Message(id, string, list(locations), flags, auto_comments,
-                           user_comments, old_msgid)
+                           user_comments, previous_id)
 
-    def update(self, template, no_fuzzy_matching=False,
-               include_old_msgid=False):
+    def update(self, template, no_fuzzy_matching=False):
         """Update the catalog based on the given template catalog.
 
         >>> from babel.messages import Catalog
@@ -537,27 +538,8 @@
         >>> catalog.obsolete.values()
         [<Message 'head' (flags: [])>]
 
-        # Include old msgid
-        >>> template = Catalog()
-        >>> template.add((u'shoe', u'shoes'), locations=[('util.py', 39)])
-        >>> catalog = Catalog(locale='pt_PT')
-        >>> catalog.add((u'shoee', u'shoes'), (u'Sapato', u'Sapatos'),
-        ...             locations=[('util.py', 39)])
-        >>> catalog.update(template, include_old_msgid=True)
-        >>> len(catalog)
-        1
-        >>> msg1 = catalog['shoe']
-        >>> msg1.id
-        (u'shoe', u'shoes')
-        >>> msg1.string
-        (u'Sapato', u'Sapatos')
-        >>> msg1.old_msgid
-        [u'shoee', u'shoes']
-
         :param template: the reference catalog, usually read from a POT file
         :param no_fuzzy_matching: whether to use fuzzy matching of message IDs
-        :param include_old_msgid: include the old msgid as a comment when
-                                  updating the catalog
         """
         messages = self._messages
         self._messages = odict()
@@ -580,11 +562,10 @@
                             oldmsg = messages.pop(matches[0])
                             message.string = oldmsg.string
                             message.flags |= oldmsg.flags | set([u'fuzzy'])
-                            if include_old_msgid:
-                                if isinstance(oldmsg.id, basestring):
-                                    message.old_msgid = [oldmsg.id]
-                                else:
-                                    message.old_msgid = list(oldmsg.id)
+                            if isinstance(oldmsg.id, basestring):
+                                message.previous_id = [oldmsg.id]
+                            else:
+                                message.previous_id = list(oldmsg.id)
                             self[message.id] = message
                             continue
 
--- a/babel/messages/pofile.py
+++ b/babel/messages/pofile.py
@@ -315,7 +315,7 @@
 
 def write_po(fileobj, catalog, width=76, no_location=False, omit_header=False,
              sort_output=False, sort_by_file=False, ignore_obsolete=False,
-             include_old_msgid=False):
+             include_previous=False):
     r"""Write a ``gettext`` PO (portable object) template file for a given
     message catalog to the provided file-like object.
 
@@ -351,7 +351,7 @@
     :sort_by_file: whether to sort the messages in the output by their locations
     :ignore_obsolete: whether to ignore obsolete messages and not include them
                       in the output; by default they are included as comments
-    :param include_old_msgid: include the old msgid as a comment when
+    :param include_previous: include the old msgid as a comment when
                               updating the catalog
     """
     def _normalize(key, prefix=''):
@@ -417,10 +417,13 @@
         if message.flags:
             _write('#%s\n' % ', '.join([''] + list(message.flags)))
 
-        if message.old_msgid and include_old_msgid:
-            _write_comment(message.old_msgid[0], prefix='| msgid')
-            if len(message.old_msgid) > 1:
-                _write_comment(message.old_msgid[1], prefix='| msgid_plural')
+        if message.previous_id and include_previous:
+            _write_comment(u'msgid %s' % _normalize(message.previous_id[0]),
+                           prefix='|')
+            if len(message.previous_id) > 1:
+                _write_comment(u'msgid_plural %s' % _normalize(
+                    message.previous_id[1]
+                ), prefix='|')
 
         _write_message(message)
         _write('\n')
--- a/babel/messages/tests/pofile.py
+++ b/babel/messages/tests/pofile.py
@@ -250,6 +250,31 @@
 msgid "foo"
 msgstr "Voh"''', buf.getvalue().strip())
 
+    def test_po_with_previous_msgid(self):
+        catalog = Catalog()
+        catalog.add(u'foo', u'Voh', locations=[('main.py', 1)],
+                    previous_id=u'fo')
+        buf = StringIO()
+        pofile.write_po(buf, catalog, omit_header=True, include_previous=True)
+        self.assertEqual('''#: main.py:1
+#| msgid "fo"
+msgid "foo"
+msgstr "Voh"''', buf.getvalue().strip())
+
+    def test_po_with_previous_msgid_plural(self):
+        catalog = Catalog()
+        catalog.add((u'foo', u'foos'), (u'Voh', u'Voeh'),
+                    locations=[('main.py', 1)], previous_id=(u'fo', u'fos'))
+        buf = StringIO()
+        pofile.write_po(buf, catalog, omit_header=True, include_previous=True)
+        self.assertEqual('''#: main.py:1
+#| msgid "fo"
+#| msgid_plural "fos"
+msgid "foo"
+msgid_plural "foos"
+msgstr[0] "Voh"
+msgstr[1] "Voeh"''', buf.getvalue().strip())
+
 
 def suite():
     suite = unittest.TestSuite()
Copyright (C) 2012-2017 Edgewall Software