comparison babel/messages/catalog.py @ 86:8a703ecdba91

Some cosmetic changes for the new translator comments support.
author cmlenz
date Sun, 10 Jun 2007 18:03:46 +0000
parents f421e5576d26
children 31519c52c0fe
comparison
equal deleted inserted replaced
85:e10f82c1d4c4 86:8a703ecdba91
33 33
34 34
35 class Message(object): 35 class Message(object):
36 """Representation of a single message in a catalog.""" 36 """Representation of a single message in a catalog."""
37 37
38 def __init__(self, id, string='', locations=(), flags=(), comments=[]): 38 def __init__(self, id, string='', locations=(), flags=(), comments=()):
39 """Create the message object. 39 """Create the message object.
40 40
41 :param id: the message ID, or a ``(singular, plural)`` tuple for 41 :param id: the message ID, or a ``(singular, plural)`` tuple for
42 pluralizable messages 42 pluralizable messages
43 :param string: the translated message string, or a 43 :param string: the translated message string, or a
44 ``(singular, plural)`` tuple for pluralizable messages 44 ``(singular, plural)`` tuple for pluralizable messages
45 :param locations: a sequence of ``(filenname, lineno)`` tuples 45 :param locations: a sequence of ``(filenname, lineno)`` tuples
46 :param flags: a set or sequence of flags 46 :param flags: a set or sequence of flags
47 :param comments: a list of comments for the msgid 47 :param comments: a sequence of translator comments for the message
48 """ 48 """
49 self.id = id 49 self.id = id
50 if not string and self.pluralizable: 50 if not string and self.pluralizable:
51 string = (u'', u'') 51 string = (u'', u'')
52 self.string = string 52 self.string = string
54 self.flags = set(flags) 54 self.flags = set(flags)
55 if id and self.python_format: 55 if id and self.python_format:
56 self.flags.add('python-format') 56 self.flags.add('python-format')
57 else: 57 else:
58 self.flags.discard('python-format') 58 self.flags.discard('python-format')
59 self.comments = comments 59 self.comments = list(comments)
60 60
61 def __repr__(self): 61 def __repr__(self):
62 return '<%s %r>' % (type(self).__name__, self.id) 62 return '<%s %r>' % (type(self).__name__, self.id)
63 63
64 def fuzzy(self): 64 def fuzzy(self):
128 locale = Locale.parse(locale) 128 locale = Locale.parse(locale)
129 self.locale = locale #: the locale or `None` 129 self.locale = locale #: the locale or `None`
130 self._messages = odict() 130 self._messages = odict()
131 131
132 self.project = project or 'PROJECT' #: the project name 132 self.project = project or 'PROJECT' #: the project name
133 self.version = version or 'VERSION' #: the project version 133 self.version = version or 'VERSION' #: the project version
134 self.msgid_bugs_address = msgid_bugs_address or 'EMAIL@ADDRESS' 134 self.msgid_bugs_address = msgid_bugs_address or 'EMAIL@ADDRESS'
135 135
136 if creation_date is None: 136 if creation_date is None:
137 creation_date = time.localtime() 137 creation_date = time.localtime()
138 elif isinstance(creation_date, datetime): 138 elif isinstance(creation_date, datetime):
139 if creation_date.tzinfo is None: 139 if creation_date.tzinfo is None:
140 creation_date = creation_date.replace(tzinfo=UTC) 140 creation_date = creation_date.replace(tzinfo=UTC)
164 else: 164 else:
165 headers.append(('PO-Revision-Date', 165 headers.append(('PO-Revision-Date',
166 time.strftime('%Y-%m-%d %H:%M%z', self.revision_date))) 166 time.strftime('%Y-%m-%d %H:%M%z', self.revision_date)))
167 headers.append(('Last-Translator', self.last_translator)) 167 headers.append(('Last-Translator', self.last_translator))
168 headers.append(('Language-Team', '%s <LL@li.org>' % self.locale)) 168 headers.append(('Language-Team', '%s <LL@li.org>' % self.locale))
169 headers.append(('Plural-Forms', self.plural_forms))
169 headers.append(('MIME-Version', '1.0')) 170 headers.append(('MIME-Version', '1.0'))
170 headers.append(('Content-Type', 171 headers.append(('Content-Type',
171 'text/plain; charset=%s' % self.charset)) 172 'text/plain; charset=%s' % self.charset))
172 headers.append(('Content-Transfer-Encoding', '8bit')) 173 headers.append(('Content-Transfer-Encoding', '8bit'))
173 if self.locale is not None:
174 headers.append(('Plural-Forms', self.plural_forms))
175 headers.append(('Generated-By', 'Babel %s' % VERSION)) 174 headers.append(('Generated-By', 'Babel %s' % VERSION))
176 return headers 175 return headers
177 headers = property(headers, doc="""\ 176 headers = property(headers, doc="""\
178 The MIME headers of the catalog, used for the special ``msgid ""`` entry. 177 The MIME headers of the catalog, used for the special ``msgid ""`` entry.
179 178
210 Report-Msgid-Bugs-To: EMAIL@ADDRESS 209 Report-Msgid-Bugs-To: EMAIL@ADDRESS
211 POT-Creation-Date: 1990-04-01 15:30+0000 210 POT-Creation-Date: 1990-04-01 15:30+0000
212 PO-Revision-Date: 1990-08-03 12:00+0000 211 PO-Revision-Date: 1990-08-03 12:00+0000
213 Last-Translator: John Doe <jd@example.com> 212 Last-Translator: John Doe <jd@example.com>
214 Language-Team: de_DE <LL@li.org> 213 Language-Team: de_DE <LL@li.org>
214 Plural-Forms: nplurals=2; plural=(n != 1)
215 MIME-Version: 1.0 215 MIME-Version: 1.0
216 Content-Type: text/plain; charset=utf-8 216 Content-Type: text/plain; charset=utf-8
217 Content-Transfer-Encoding: 8bit 217 Content-Transfer-Encoding: 8bit
218 Plural-Forms: nplurals=2; plural=(n != 1)
219 Generated-By: Babel ... 218 Generated-By: Babel ...
220 219
221 :type: `list` 220 :type: `list`
222 """) 221 """)
223 222
227 if str(self.locale) in PLURALS: 226 if str(self.locale) in PLURALS:
228 num = PLURALS[str(self.locale)][0] 227 num = PLURALS[str(self.locale)][0]
229 elif self.locale.language in PLURALS: 228 elif self.locale.language in PLURALS:
230 num = PLURALS[self.locale.language][0] 229 num = PLURALS[self.locale.language][0]
231 return num 230 return num
232 num_plurals = property(num_plurals) 231 num_plurals = property(num_plurals, doc="""\
232 The number of plurals used by the locale.
233 """)
233 234
234 def plural_forms(self): 235 def plural_forms(self):
235 num, expr = ('INTEGER', 'EXPRESSION') 236 num, expr = ('INTEGER', 'EXPRESSION')
236 if self.locale: 237 if self.locale:
237 if str(self.locale) in PLURALS: 238 if str(self.locale) in PLURALS:
253 def __contains__(self, id): 254 def __contains__(self, id):
254 """Return whether the catalog has a message with the specified ID.""" 255 """Return whether the catalog has a message with the specified ID."""
255 return self._key_for(id) in self._messages 256 return self._key_for(id) in self._messages
256 257
257 def __len__(self): 258 def __len__(self):
259 """The number of messages in the catalog.
260
261 This does not include the special ``msgid ""`` entry.
262 """
258 return len(self._messages) 263 return len(self._messages)
259 264
260 def __iter__(self): 265 def __iter__(self):
261 """Iterates through all the entries in the catalog, in the order they 266 """Iterates through all the entries in the catalog, in the order they
262 were added, yielding a `Message` object for every entry. 267 were added, yielding a `Message` object for every entry.
328 else: 333 else:
329 if isinstance(id, (list, tuple)): 334 if isinstance(id, (list, tuple)):
330 assert isinstance(message.string, (list, tuple)) 335 assert isinstance(message.string, (list, tuple))
331 self._messages[key] = message 336 self._messages[key] = message
332 337
333 def add(self, id, string=None, locations=(), flags=(), comments=[]): 338 def add(self, id, string=None, locations=(), flags=(), comments=()):
334 """Add or update the message with the specified ID. 339 """Add or update the message with the specified ID.
335 340
336 >>> catalog = Catalog() 341 >>> catalog = Catalog()
337 >>> catalog.add(u'foo') 342 >>> catalog.add(u'foo')
338 >>> catalog[u'foo'] 343 >>> catalog[u'foo']
345 pluralizable messages 350 pluralizable messages
346 :param string: the translated message string, or a 351 :param string: the translated message string, or a
347 ``(singular, plural)`` tuple for pluralizable messages 352 ``(singular, plural)`` tuple for pluralizable messages
348 :param locations: a sequence of ``(filenname, lineno)`` tuples 353 :param locations: a sequence of ``(filenname, lineno)`` tuples
349 :param flags: a set or sequence of flags 354 :param flags: a set or sequence of flags
350 :param comments: a list of comments for the msgid 355 :param comments: a list of translator comments
351 """ 356 """
352 self[id] = Message(id, string, list(locations), flags, comments) 357 self[id] = Message(id, string, list(locations), flags, comments)
353 358
354 def _key_for(self, id): 359 def _key_for(self, id):
355 """The key for a message is just the singular ID even for pluralizable 360 """The key for a message is just the singular ID even for pluralizable
Copyright (C) 2012-2017 Edgewall Software