]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Fix the way obsolete messages are stored (#1132)
authorTomas R. <tomas.roun8@gmail.com>
Sun, 20 Oct 2024 06:54:27 +0000 (08:54 +0200)
committerGitHub <noreply@github.com>
Sun, 20 Oct 2024 06:54:27 +0000 (09:54 +0300)
babel/messages/pofile.py
tests/messages/test_pofile.py

index b7a524051d7b8e033e4f6e7baef089aa08169af7..ec2d57f7d4775fc44976030e3264417b38d36c8f 100644 (file)
@@ -239,7 +239,7 @@ class PoFileParser:
                           context=msgctxt)
         if self.obsolete:
             if not self.ignore_obsolete:
-                self.catalog.obsolete[msgid] = message
+                self.catalog.obsolete[self.catalog._key_for(msgid, msgctxt)] = message
         else:
             self.catalog[msgid] = message
         self.counter += 1
index 3609b5c2b7fcc20a378ec7aa6dfcc9e29ddd1d63..f4360ecf126daf3b3d64c8c7a10c90966f538986 100644 (file)
@@ -299,10 +299,62 @@ msgstr "Bahr"
         catalog = pofile.read_po(buf)
         assert len(catalog) == 2
         assert len(catalog.obsolete) == 1
-        message = catalog.obsolete["foo"]
+        message = catalog.obsolete[("foo", "other")]
         assert message.context == 'other'
         assert message.string == 'Voh'
 
+    def test_obsolete_messages_with_context(self):
+        buf = StringIO('''
+# This is an obsolete message
+#~ msgctxt "apple"
+#~ msgid "foo"
+#~ msgstr "Foo"
+
+# This is an obsolete message with the same id but different context
+#~ msgctxt "orange"
+#~ msgid "foo"
+#~ msgstr "Bar"
+''')
+        catalog = pofile.read_po(buf)
+        assert len(catalog) == 0
+        assert len(catalog.obsolete) == 2
+        assert 'foo' not in catalog.obsolete
+
+        apple_msg = catalog.obsolete[('foo', 'apple')]
+        assert apple_msg.id == 'foo'
+        assert apple_msg.string == 'Foo'
+        assert apple_msg.user_comments == ['This is an obsolete message']
+
+        orange_msg = catalog.obsolete[('foo', 'orange')]
+        assert orange_msg.id == 'foo'
+        assert orange_msg.string == 'Bar'
+        assert orange_msg.user_comments == ['This is an obsolete message with the same id but different context']
+
+    def test_obsolete_messages_roundtrip(self):
+        buf = StringIO('''\
+# This message is not obsolete
+#: main.py:1
+msgid "bar"
+msgstr "Bahr"
+
+# This is an obsolete message
+#~ msgid "foo"
+#~ msgstr "Voh"
+
+# This is an obsolete message
+#~ msgctxt "apple"
+#~ msgid "foo"
+#~ msgstr "Foo"
+
+# This is an obsolete message with the same id but different context
+#~ msgctxt "orange"
+#~ msgid "foo"
+#~ msgstr "Bar"
+
+''')
+        generated_po_file = ''.join(pofile.generate_po(pofile.read_po(buf), omit_header=True))
+        assert buf.getvalue() == generated_po_file
+
     def test_multiline_context(self):
         buf = StringIO('''
 msgctxt "a really long "
@@ -394,7 +446,7 @@ msgstr[1] "Vohs [text]"''')
         assert len(catalog) == 0
         assert len(catalog.obsolete) == 1
         assert catalog.num_plurals == 2
-        message = catalog.obsolete[('foo', 'foos')]
+        message = catalog.obsolete['foo']
         assert len(message.string) == 2
         assert message.string[0] == 'Voh [text]'
         assert message.string[1] == 'Vohs [text]'