comparison babel/messages/catalog.py @ 205:aefe4ac123a2

Minor changes to how previous msgids are processed.
author cmlenz
date Tue, 03 Jul 2007 21:19:27 +0000
parents 9181a11feb81
children 6cd31048eb5c
comparison
equal deleted inserted replaced
204:9181a11feb81 205:aefe4ac123a2
38 38
39 class Message(object): 39 class Message(object):
40 """Representation of a single message in a catalog.""" 40 """Representation of a single message in a catalog."""
41 41
42 def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(), 42 def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(),
43 user_comments=(), old_msgid=()): 43 user_comments=(), previous_id=()):
44 """Create the message object. 44 """Create the message object.
45 45
46 :param id: the message ID, or a ``(singular, plural)`` tuple for 46 :param id: the message ID, or a ``(singular, plural)`` tuple for
47 pluralizable messages 47 pluralizable messages
48 :param string: the translated message string, or a 48 :param string: the translated message string, or a
49 ``(singular, plural)`` tuple for pluralizable messages 49 ``(singular, plural)`` tuple for pluralizable messages
50 :param locations: a sequence of ``(filenname, lineno)`` tuples 50 :param locations: a sequence of ``(filenname, lineno)`` tuples
51 :param flags: a set or sequence of flags 51 :param flags: a set or sequence of flags
52 :param auto_comments: a sequence of automatic comments for the message 52 :param auto_comments: a sequence of automatic comments for the message
53 :param user_comments: a sequence of user comments for the message 53 :param user_comments: a sequence of user comments for the message
54 :param old_message: the old message ID, or a ``(singular, plural)`` 54 :param previous_id: the previous message ID, or a ``(singular, plural)``
55 tuple for old pluralizable messages 55 tuple for pluralizable messages
56 """ 56 """
57 self.id = id #: The message ID 57 self.id = id #: The message ID
58 if not string and self.pluralizable: 58 if not string and self.pluralizable:
59 string = (u'', u'') 59 string = (u'', u'')
60 self.string = string #: The message translation 60 self.string = string #: The message translation
64 self.flags.add('python-format') 64 self.flags.add('python-format')
65 else: 65 else:
66 self.flags.discard('python-format') 66 self.flags.discard('python-format')
67 self.auto_comments = list(auto_comments) 67 self.auto_comments = list(auto_comments)
68 self.user_comments = list(user_comments) 68 self.user_comments = list(user_comments)
69 if isinstance(old_msgid, basestring): 69 if isinstance(previous_id, basestring):
70 self.old_msgid = [old_msgid] 70 self.previous_id = [previous_id]
71 else: 71 else:
72 self.old_msgid = list(old_msgid) 72 self.previous_id = list(previous_id)
73 73
74 def __repr__(self): 74 def __repr__(self):
75 return '<%s %r (flags: %r)>' % (type(self).__name__, self.id, 75 return '<%s %r (flags: %r)>' % (type(self).__name__, self.id,
76 list(self.flags)) 76 list(self.flags))
77 77
467 if isinstance(id, (list, tuple)): 467 if isinstance(id, (list, tuple)):
468 assert isinstance(message.string, (list, tuple)) 468 assert isinstance(message.string, (list, tuple))
469 self._messages[key] = message 469 self._messages[key] = message
470 470
471 def add(self, id, string=None, locations=(), flags=(), auto_comments=(), 471 def add(self, id, string=None, locations=(), flags=(), auto_comments=(),
472 user_comments=(), old_msgid=()): 472 user_comments=(), previous_id=()):
473 """Add or update the message with the specified ID. 473 """Add or update the message with the specified ID.
474 474
475 >>> catalog = Catalog() 475 >>> catalog = Catalog()
476 >>> catalog.add(u'foo') 476 >>> catalog.add(u'foo')
477 >>> catalog[u'foo'] 477 >>> catalog[u'foo']
486 ``(singular, plural)`` tuple for pluralizable messages 486 ``(singular, plural)`` tuple for pluralizable messages
487 :param locations: a sequence of ``(filenname, lineno)`` tuples 487 :param locations: a sequence of ``(filenname, lineno)`` tuples
488 :param flags: a set or sequence of flags 488 :param flags: a set or sequence of flags
489 :param auto_comments: a sequence of automatic comments 489 :param auto_comments: a sequence of automatic comments
490 :param user_comments: a sequence of user comments 490 :param user_comments: a sequence of user comments
491 :param previous_id: the previous message ID, or a ``(singular, plural)``
492 tuple for pluralizable messages
491 """ 493 """
492 self[id] = Message(id, string, list(locations), flags, auto_comments, 494 self[id] = Message(id, string, list(locations), flags, auto_comments,
493 user_comments, old_msgid) 495 user_comments, previous_id)
494 496
495 def update(self, template, no_fuzzy_matching=False, 497 def update(self, template, no_fuzzy_matching=False):
496 include_old_msgid=False):
497 """Update the catalog based on the given template catalog. 498 """Update the catalog based on the given template catalog.
498 499
499 >>> from babel.messages import Catalog 500 >>> from babel.messages import Catalog
500 >>> template = Catalog() 501 >>> template = Catalog()
501 >>> template.add('green', locations=[('main.py', 99)]) 502 >>> template.add('green', locations=[('main.py', 99)])
535 >>> 'head' in catalog 536 >>> 'head' in catalog
536 False 537 False
537 >>> catalog.obsolete.values() 538 >>> catalog.obsolete.values()
538 [<Message 'head' (flags: [])>] 539 [<Message 'head' (flags: [])>]
539 540
540 # Include old msgid
541 >>> template = Catalog()
542 >>> template.add((u'shoe', u'shoes'), locations=[('util.py', 39)])
543 >>> catalog = Catalog(locale='pt_PT')
544 >>> catalog.add((u'shoee', u'shoes'), (u'Sapato', u'Sapatos'),
545 ... locations=[('util.py', 39)])
546 >>> catalog.update(template, include_old_msgid=True)
547 >>> len(catalog)
548 1
549 >>> msg1 = catalog['shoe']
550 >>> msg1.id
551 (u'shoe', u'shoes')
552 >>> msg1.string
553 (u'Sapato', u'Sapatos')
554 >>> msg1.old_msgid
555 [u'shoee', u'shoes']
556
557 :param template: the reference catalog, usually read from a POT file 541 :param template: the reference catalog, usually read from a POT file
558 :param no_fuzzy_matching: whether to use fuzzy matching of message IDs 542 :param no_fuzzy_matching: whether to use fuzzy matching of message IDs
559 :param include_old_msgid: include the old msgid as a comment when
560 updating the catalog
561 """ 543 """
562 messages = self._messages 544 messages = self._messages
563 self._messages = odict() 545 self._messages = odict()
564 546
565 for message in template: 547 for message in template:
578 [self._key_for(msgid) for msgid in messages], 1) 560 [self._key_for(msgid) for msgid in messages], 1)
579 if matches: 561 if matches:
580 oldmsg = messages.pop(matches[0]) 562 oldmsg = messages.pop(matches[0])
581 message.string = oldmsg.string 563 message.string = oldmsg.string
582 message.flags |= oldmsg.flags | set([u'fuzzy']) 564 message.flags |= oldmsg.flags | set([u'fuzzy'])
583 if include_old_msgid: 565 if isinstance(oldmsg.id, basestring):
584 if isinstance(oldmsg.id, basestring): 566 message.previous_id = [oldmsg.id]
585 message.old_msgid = [oldmsg.id] 567 else:
586 else: 568 message.previous_id = list(oldmsg.id)
587 message.old_msgid = list(oldmsg.id)
588 self[message.id] = message 569 self[message.id] = message
589 continue 570 continue
590 571
591 self[message.id] = message 572 self[message.id] = message
592 573
Copyright (C) 2012-2017 Edgewall Software