"""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
: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:
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,
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()
: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
>>> 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()
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
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.
: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=''):
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')
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()