]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-85012: Properly reset msgctxt when compiling messages with msgfmt (GH-130525)
authorTomas R. <tomas.roun8@gmail.com>
Thu, 13 Mar 2025 18:40:40 +0000 (19:40 +0100)
committerGitHub <noreply@github.com>
Thu, 13 Mar 2025 18:40:40 +0000 (20:40 +0200)
Add also human-readable snapshots for tests.

Lib/test/test_tools/msgfmt_data/fuzzy.json [new file with mode: 0644]
Lib/test/test_tools/msgfmt_data/general.json [new file with mode: 0644]
Lib/test/test_tools/msgfmt_data/general.mo
Lib/test/test_tools/test_msgfmt.py
Misc/NEWS.d/next/Tools-Demos/2025-02-24-21-36-23.gh-issue-85012.9K1U0E.rst [new file with mode: 0644]
Tools/i18n/msgfmt.py

diff --git a/Lib/test/test_tools/msgfmt_data/fuzzy.json b/Lib/test/test_tools/msgfmt_data/fuzzy.json
new file mode 100644 (file)
index 0000000..fe51488
--- /dev/null
@@ -0,0 +1 @@
+[]
diff --git a/Lib/test/test_tools/msgfmt_data/general.json b/Lib/test/test_tools/msgfmt_data/general.json
new file mode 100644 (file)
index 0000000..8ceb34c
--- /dev/null
@@ -0,0 +1,58 @@
+[
+    [
+        "",
+        "Project-Id-Version: PACKAGE VERSION\nPOT-Creation-Date: 2024-10-26 18:06+0200\nPO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\nLast-Translator: FULL NAME <EMAIL@ADDRESS>\nLanguage-Team: LANGUAGE <LL@li.org>\nMIME-Version: 1.0\nContent-Type: text/plain; charset=UTF-8\nContent-Transfer-Encoding: 8bit\n"
+    ],
+    [
+        "\n newlines \n",
+        "\n translated \n"
+    ],
+    [
+        "\"escapes\"",
+        "\"translated\""
+    ],
+    [
+        "Multilinestring",
+        "Multilinetranslation"
+    ],
+    [
+        "abc\u0004foo",
+        "bar"
+    ],
+    [
+        "bar",
+        "baz"
+    ],
+    [
+        "xyz\u0004foo",
+        "bar"
+    ],
+    [
+        [
+            "One email sent.",
+            0
+        ],
+        "One email sent."
+    ],
+    [
+        [
+            "One email sent.",
+            1
+        ],
+        "%d emails sent."
+    ],
+    [
+        [
+            "abc\u0004One email sent.",
+            0
+        ],
+        "One email sent."
+    ],
+    [
+        [
+            "abc\u0004One email sent.",
+            1
+        ],
+        "%d emails sent."
+    ]
+]
index bc0683a62d0ddaecbc751b9a59eb1fe19bb7619c..44b7363071a98bfe61eadc6adb0b434f1fd36125 100644 (file)
Binary files a/Lib/test/test_tools/msgfmt_data/general.mo and b/Lib/test/test_tools/msgfmt_data/general.mo differ
index a6073b8be030731588497310bdab2c0d72a4e429..55a10888c21f70b4598d4e8bcf24eea413991a65 100644 (file)
@@ -1,5 +1,6 @@
 """Tests for the Tools/i18n/msgfmt.py tool."""
 
+import json
 import sys
 import unittest
 from gettext import GNUTranslations
@@ -39,6 +40,28 @@ class CompilationTest(unittest.TestCase):
 
                     self.assertDictEqual(actual._catalog, expected._catalog)
 
+    def test_translations(self):
+        with open(data_dir / 'general.mo', 'rb') as f:
+            t = GNUTranslations(f)
+
+        self.assertEqual(t.gettext('foo'), 'foo')
+        self.assertEqual(t.gettext('bar'), 'baz')
+        self.assertEqual(t.pgettext('abc', 'foo'), 'bar')
+        self.assertEqual(t.pgettext('xyz', 'foo'), 'bar')
+        self.assertEqual(t.gettext('Multilinestring'), 'Multilinetranslation')
+        self.assertEqual(t.gettext('"escapes"'), '"translated"')
+        self.assertEqual(t.gettext('\n newlines \n'), '\n translated \n')
+        self.assertEqual(t.ngettext('One email sent.', '%d emails sent.', 1),
+                         'One email sent.')
+        self.assertEqual(t.ngettext('One email sent.', '%d emails sent.', 2),
+                         '%d emails sent.')
+        self.assertEqual(t.npgettext('abc', 'One email sent.',
+                                     '%d emails sent.', 1),
+                         'One email sent.')
+        self.assertEqual(t.npgettext('abc', 'One email sent.',
+                                     '%d emails sent.', 2),
+                         '%d emails sent.')
+
     def test_po_with_bom(self):
         with temp_cwd():
             Path('bom.po').write_bytes(b'\xef\xbb\xbfmsgid "Python"\nmsgstr "Pioton"\n')
@@ -125,6 +148,16 @@ def update_catalog_snapshots():
     for po_file in data_dir.glob('*.po'):
         mo_file = po_file.with_suffix('.mo')
         compile_messages(po_file, mo_file)
+        # Create a human-readable JSON file which is
+        # easier to review than the binary .mo file.
+        with open(mo_file, 'rb') as f:
+            translations = GNUTranslations(f)
+        catalog_file = po_file.with_suffix('.json')
+        with open(catalog_file, 'w') as f:
+            data = translations._catalog.items()
+            data = sorted(data, key=lambda x: (isinstance(x[0], tuple), x[0]))
+            json.dump(data, f, indent=4)
+            f.write('\n')
 
 
 if __name__ == '__main__':
diff --git a/Misc/NEWS.d/next/Tools-Demos/2025-02-24-21-36-23.gh-issue-85012.9K1U0E.rst b/Misc/NEWS.d/next/Tools-Demos/2025-02-24-21-36-23.gh-issue-85012.9K1U0E.rst
new file mode 100644 (file)
index 0000000..5ec2058
--- /dev/null
@@ -0,0 +1 @@
+Correctly reset ``msgctxt`` when compiling messages in :program:`msgfmt`.
index f005c4e7b5b79eaf3de26559ff5390dfdac776b3..878d3e290778c6c78338e8bc0b83fd5090294827 100755 (executable)
@@ -159,6 +159,7 @@ def make(filename, outfile):
         elif l.startswith('msgid') and not l.startswith('msgid_plural'):
             if section == STR:
                 add(msgctxt, msgid, msgstr, fuzzy)
+                msgctxt = None
                 if not msgid:
                     # See whether there is an encoding declaration
                     p = HeaderParser()