Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a3f4889

Browse filesBrowse files
[3.12] gh-85012: Properly reset msgctxt when compiling messages with msgfmt (GH-130525) (GH-131206)
Add also human-readable snapshots for tests. (cherry picked from commit 7ea6e88) Co-authored-by: Tomas R <tomas.roun8@gmail.com>
1 parent ba36389 commit a3f4889
Copy full SHA for a3f4889

6 files changed

+94Lines changed: 94 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Collapse file
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[
2+
[
3+
"",
4+
"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"
5+
],
6+
[
7+
"\n newlines \n",
8+
"\n translated \n"
9+
],
10+
[
11+
"\"escapes\"",
12+
"\"translated\""
13+
],
14+
[
15+
"Multilinestring",
16+
"Multilinetranslation"
17+
],
18+
[
19+
"abc\u0004foo",
20+
"bar"
21+
],
22+
[
23+
"bar",
24+
"baz"
25+
],
26+
[
27+
"xyz\u0004foo",
28+
"bar"
29+
],
30+
[
31+
[
32+
"One email sent.",
33+
0
34+
],
35+
"One email sent."
36+
],
37+
[
38+
[
39+
"One email sent.",
40+
1
41+
],
42+
"%d emails sent."
43+
],
44+
[
45+
[
46+
"abc\u0004One email sent.",
47+
0
48+
],
49+
"One email sent."
50+
],
51+
[
52+
[
53+
"abc\u0004One email sent.",
54+
1
55+
],
56+
"%d emails sent."
57+
]
58+
]
Collapse file
-16 Bytes
Binary file not shown.
Collapse file

‎Lib/test/test_tools/test_msgfmt.py‎

Copy file name to clipboardExpand all lines: Lib/test/test_tools/test_msgfmt.py
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for the Tools/i18n/msgfmt.py tool."""
22

3+
import json
34
import sys
45
import unittest
56
from gettext import GNUTranslations
@@ -39,6 +40,28 @@ def test_compilation(self):
3940

4041
self.assertDictEqual(actual._catalog, expected._catalog)
4142

43+
def test_translations(self):
44+
with open(data_dir / 'general.mo', 'rb') as f:
45+
t = GNUTranslations(f)
46+
47+
self.assertEqual(t.gettext('foo'), 'foo')
48+
self.assertEqual(t.gettext('bar'), 'baz')
49+
self.assertEqual(t.pgettext('abc', 'foo'), 'bar')
50+
self.assertEqual(t.pgettext('xyz', 'foo'), 'bar')
51+
self.assertEqual(t.gettext('Multilinestring'), 'Multilinetranslation')
52+
self.assertEqual(t.gettext('"escapes"'), '"translated"')
53+
self.assertEqual(t.gettext('\n newlines \n'), '\n translated \n')
54+
self.assertEqual(t.ngettext('One email sent.', '%d emails sent.', 1),
55+
'One email sent.')
56+
self.assertEqual(t.ngettext('One email sent.', '%d emails sent.', 2),
57+
'%d emails sent.')
58+
self.assertEqual(t.npgettext('abc', 'One email sent.',
59+
'%d emails sent.', 1),
60+
'One email sent.')
61+
self.assertEqual(t.npgettext('abc', 'One email sent.',
62+
'%d emails sent.', 2),
63+
'%d emails sent.')
64+
4265
def test_invalid_msgid_plural(self):
4366
with temp_cwd():
4467
Path('invalid.po').write_text('''\
@@ -117,6 +140,16 @@ def update_catalog_snapshots():
117140
for po_file in data_dir.glob('*.po'):
118141
mo_file = po_file.with_suffix('.mo')
119142
compile_messages(po_file, mo_file)
143+
# Create a human-readable JSON file which is
144+
# easier to review than the binary .mo file.
145+
with open(mo_file, 'rb') as f:
146+
translations = GNUTranslations(f)
147+
catalog_file = po_file.with_suffix('.json')
148+
with open(catalog_file, 'w') as f:
149+
data = translations._catalog.items()
150+
data = sorted(data, key=lambda x: (isinstance(x[0], tuple), x[0]))
151+
json.dump(data, f, indent=4)
152+
f.write('\n')
120153

121154

122155
if __name__ == '__main__':
Collapse file
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Correctly reset ``msgctxt`` when compiling messages in :program:`msgfmt`.
Collapse file

‎Tools/i18n/msgfmt.py‎

Copy file name to clipboardExpand all lines: Tools/i18n/msgfmt.py
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def make(filename, outfile):
149149
elif l.startswith('msgid') and not l.startswith('msgid_plural'):
150150
if section == STR:
151151
add(msgctxt, msgid, msgstr, fuzzy)
152+
msgctxt = None
152153
if not msgid:
153154
# See whether there is an encoding declaration
154155
p = HeaderParser()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.