comparison babel/messages/catalog.py @ 337:662d332c0a2b

More preparation for msgctxt support (#54).
author cmlenz
date Wed, 11 Jun 2008 18:56:27 +0000
parents 355a977c92aa
children 6811369cb912
comparison
equal deleted inserted replaced
336:6e86b862af57 337:662d332c0a2b
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 # 2 #
3 # Copyright (C) 2007 Edgewall Software 3 # Copyright (C) 2007-2008 Edgewall Software
4 # All rights reserved. 4 # All rights reserved.
5 # 5 #
6 # This software is licensed as described in the file COPYING, which 6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution. The terms 7 # you should have received as part of this distribution. The terms
8 # are also available at http://babel.edgewall.org/wiki/License. 8 # are also available at http://babel.edgewall.org/wiki/License.
39 39
40 class Message(object): 40 class Message(object):
41 """Representation of a single message in a catalog.""" 41 """Representation of a single message in a catalog."""
42 42
43 def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(), 43 def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(),
44 user_comments=(), previous_id=(), lineno=None): 44 user_comments=(), previous_id=(), lineno=None, context=None):
45 """Create the message object. 45 """Create the message object.
46 46
47 :param id: the message ID, or a ``(singular, plural)`` tuple for 47 :param id: the message ID, or a ``(singular, plural)`` tuple for
48 pluralizable messages 48 pluralizable messages
49 :param string: the translated message string, or a 49 :param string: the translated message string, or a
54 :param user_comments: a sequence of user comments for the message 54 :param user_comments: a sequence of user comments for the message
55 :param previous_id: the previous message ID, or a ``(singular, plural)`` 55 :param previous_id: the previous message ID, or a ``(singular, plural)``
56 tuple for pluralizable messages 56 tuple for pluralizable messages
57 :param lineno: the line number on which the msgid line was found in the 57 :param lineno: the line number on which the msgid line was found in the
58 PO file, if any 58 PO file, if any
59 :param context: the message context
59 """ 60 """
60 self.id = id #: The message ID 61 self.id = id #: The message ID
61 if not string and self.pluralizable: 62 if not string and self.pluralizable:
62 string = (u'', u'') 63 string = (u'', u'')
63 self.string = string #: The message translation 64 self.string = string #: The message translation
72 if isinstance(previous_id, basestring): 73 if isinstance(previous_id, basestring):
73 self.previous_id = [previous_id] 74 self.previous_id = [previous_id]
74 else: 75 else:
75 self.previous_id = list(previous_id) 76 self.previous_id = list(previous_id)
76 self.lineno = lineno 77 self.lineno = lineno
78 self.context = context
77 79
78 def __repr__(self): 80 def __repr__(self):
79 return '<%s %r (flags: %r)>' % (type(self).__name__, self.id, 81 return '<%s %r (flags: %r)>' % (type(self).__name__, self.id,
80 list(self.flags)) 82 list(self.flags))
81 83
93 return cmp(self.id, obj.id) 95 return cmp(self.id, obj.id)
94 96
95 def clone(self): 97 def clone(self):
96 return Message(self.id, self.string, self.locations, self.flags, 98 return Message(self.id, self.string, self.locations, self.flags,
97 self.auto_comments, self.user_comments, 99 self.auto_comments, self.user_comments,
98 self.previous_id, self.lineno) 100 self.previous_id, self.lineno, self.context)
99 101
100 def fuzzy(self): 102 def fuzzy(self):
101 return 'fuzzy' in self.flags 103 return 'fuzzy' in self.flags
102 fuzzy = property(fuzzy, doc="""\ 104 fuzzy = property(fuzzy, doc="""\
103 Whether the translation is fuzzy. 105 Whether the translation is fuzzy.
532 assert isinstance(message.string, (list, tuple)), \ 534 assert isinstance(message.string, (list, tuple)), \
533 'Expected sequence but got %s' % type(message.string) 535 'Expected sequence but got %s' % type(message.string)
534 self._messages[key] = message 536 self._messages[key] = message
535 537
536 def add(self, id, string=None, locations=(), flags=(), auto_comments=(), 538 def add(self, id, string=None, locations=(), flags=(), auto_comments=(),
537 user_comments=(), previous_id=(), lineno=None): 539 user_comments=(), previous_id=(), lineno=None, context=None):
538 """Add or update the message with the specified ID. 540 """Add or update the message with the specified ID.
539 541
540 >>> catalog = Catalog() 542 >>> catalog = Catalog()
541 >>> catalog.add(u'foo') 543 >>> catalog.add(u'foo')
542 >>> catalog[u'foo'] 544 >>> catalog[u'foo']
555 :param user_comments: a sequence of user comments 557 :param user_comments: a sequence of user comments
556 :param previous_id: the previous message ID, or a ``(singular, plural)`` 558 :param previous_id: the previous message ID, or a ``(singular, plural)``
557 tuple for pluralizable messages 559 tuple for pluralizable messages
558 :param lineno: the line number on which the msgid line was found in the 560 :param lineno: the line number on which the msgid line was found in the
559 PO file, if any 561 PO file, if any
562 :param context: the message context
560 """ 563 """
561 self[id] = Message(id, string, list(locations), flags, auto_comments, 564 self[id] = Message(id, string, list(locations), flags, auto_comments,
562 user_comments, previous_id, lineno=lineno) 565 user_comments, previous_id, lineno=lineno,
566 context=context)
563 567
564 def check(self): 568 def check(self):
565 """Run various validation checks on the translations in the catalog. 569 """Run various validation checks on the translations in the catalog.
566 570
567 For every message which fails validation, this method yield a 571 For every message which fails validation, this method yield a
Copyright (C) 2012-2017 Edgewall Software