]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Catalog update: keep user comments from destination by default
authorAarni Koskela <akx@iki.fi>
Mon, 27 May 2019 10:50:52 +0000 (13:50 +0300)
committerAarni Koskela <akx@iki.fi>
Mon, 27 May 2019 11:17:30 +0000 (14:17 +0300)
Closes #418

babel/messages/catalog.py
tests/messages/test_catalog.py

index 649d0e2fe887e8bb1b82df07c890358ede5ee1bd..136ef36238f348c947af4238795b8979f63789c2 100644 (file)
@@ -700,7 +700,7 @@ class Catalog(object):
         if key in self._messages:
             del self._messages[key]
 
-    def update(self, template, no_fuzzy_matching=False, update_header_comment=False):
+    def update(self, template, no_fuzzy_matching=False, update_header_comment=False, keep_user_comments=True):
         """Update the catalog based on the given template catalog.
 
         >>> from babel.messages import Catalog
@@ -780,6 +780,10 @@ class Catalog(object):
             else:
                 oldmsg = remaining.pop(oldkey, None)
             message.string = oldmsg.string
+
+            if keep_user_comments:
+                message.user_comments = list(distinct(oldmsg.user_comments))
+
             if isinstance(message.id, (list, tuple)):
                 if not isinstance(message.string, (list, tuple)):
                     fuzzy = True
index 1b23832a68ba061fc2e39fa7425dc32de1098f64..f583810c9689d8c370ccbce4ba1e25a1eb693b85 100644 (file)
@@ -15,8 +15,9 @@ import copy
 import datetime
 import unittest
 
+from babel._compat import StringIO
 from babel.dates import format_datetime, UTC
-from babel.messages import catalog
+from babel.messages import catalog, pofile
 from babel.util import FixedOffsetTimezone
 
 
@@ -475,3 +476,31 @@ def test_datetime_parsing():
     assert val2.month == 6
     assert val2.day == 28
     assert val2.tzinfo is None
+
+
+def test_update_catalog_comments():
+    # Based on https://web.archive.org/web/20100710131029/http://babel.edgewall.org/attachment/ticket/163/cat-update-comments.py
+
+    catalog = pofile.read_po(StringIO('''
+    # A user comment
+    #. An auto comment
+    #: main.py:1
+    #, fuzzy, python-format
+    msgid "foo %(name)s"
+    msgstr "foo %(name)s"
+    '''))
+
+    assert all(message.user_comments and message.auto_comments for message in catalog if message.id)
+
+    # NOTE: in the POT file, there are no comments
+    template = pofile.read_po(StringIO('''
+    #: main.py:1
+    #, fuzzy, python-format
+    msgid "bar %(name)s"
+    msgstr ""
+    '''))
+
+    catalog.update(template)
+
+    # Auto comments will be obliterated here
+    assert all(message.user_comments for message in catalog if message.id)