Mercurial > babel > old > mirror
comparison babel/messages/catalog.py @ 165:eafaa302dde1
Added preliminary catalog updating/merging functionality.
author | cmlenz |
---|---|
date | Fri, 22 Jun 2007 00:33:22 +0000 |
parents | 12e5f21dfcda |
children | 533baef258bb |
comparison
equal
deleted
inserted
replaced
164:543d5626c9ef | 165:eafaa302dde1 |
---|---|
469 :param user_comments: a sequence of user comments | 469 :param user_comments: a sequence of user comments |
470 """ | 470 """ |
471 self[id] = Message(id, string, list(locations), flags, auto_comments, | 471 self[id] = Message(id, string, list(locations), flags, auto_comments, |
472 user_comments) | 472 user_comments) |
473 | 473 |
474 def update(self, template): | |
475 """Update the catalog based on the given template catalog. | |
476 | |
477 >>> from babel.messages import Catalog | |
478 >>> template = Catalog() | |
479 >>> template.add('blue', locations=[('main.py', 100)]) | |
480 >>> template.add(('salad', 'salads'), locations=[('util.py', 42)]) | |
481 >>> catalog = Catalog(locale='de_DE') | |
482 >>> catalog.add('blue', u'blau', locations=[('main.py', 98)]) | |
483 >>> catalog.add('head', u'Kopf', locations=[('util.py', 33)]) | |
484 >>> catalog.add(('salad', 'salads'), (u'Salat', u'Salate'), | |
485 ... locations=[('util.py', 38)]) | |
486 | |
487 >>> rest = catalog.update(template) | |
488 >>> len(catalog) | |
489 2 | |
490 | |
491 >>> msg1 = catalog['blue'] | |
492 >>> msg1.string | |
493 u'blau' | |
494 >>> msg1.locations | |
495 [('main.py', 100)] | |
496 | |
497 >>> msg2 = catalog['salad'] | |
498 >>> msg2.string | |
499 (u'Salat', u'Salate') | |
500 >>> msg2.locations | |
501 [('util.py', 42)] | |
502 | |
503 >>> 'head' in catalog | |
504 False | |
505 >>> rest | |
506 [<Message 'head'>] | |
507 | |
508 :param template: the reference catalog, usually read from a POT file | |
509 :return: a list of `Message` objects that the catalog contained before | |
510 the updated, but couldn't be found in the template | |
511 """ | |
512 rest = odict([(message.id, message) for message in self if message.id]) | |
513 messages = self._messages | |
514 self._messages = odict() | |
515 | |
516 for message in template: | |
517 if message.id: | |
518 key = self._key_for(message.id) | |
519 if key in messages: | |
520 oldmsg = messages.pop(key) | |
521 message.string = oldmsg.string | |
522 message.flags |= oldmsg.flags | |
523 self[message.id] = message | |
524 del rest[message.id] | |
525 else: | |
526 for oldmsg in messages: | |
527 # TODO: fuzzy matching | |
528 pass | |
529 else: | |
530 self[message.id] = message | |
531 | |
532 return rest.values() | |
533 | |
474 def _key_for(self, id): | 534 def _key_for(self, id): |
475 """The key for a message is just the singular ID even for pluralizable | 535 """The key for a message is just the singular ID even for pluralizable |
476 messages. | 536 messages. |
477 """ | 537 """ |
478 key = id | 538 key = id |