Skip to content

Commit 92f227f

Browse files
Use a modified version of pybabel's code in normalize
1 parent 0e35e36 commit 92f227f

File tree

4 files changed

+60
-56
lines changed

4 files changed

+60
-56
lines changed

Lib/test/test_tools/i18n_data/messages.pot

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
msgid ""
66
msgstr ""
77
"Project-Id-Version: PACKAGE VERSION\n"
8-
"POT-Creation-Date: 2000-01-01 00:00+0000\n"
8+
"POT-Creation-Date: 2025-03-01 09:36+0000\n"
99
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1010
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1111
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -33,65 +33,71 @@ msgid ""
3333
" multiline!\n"
3434
msgstr ""
3535

36-
#: messages.py:46 messages.py:89 messages.py:90 messages.py:93 messages.py:94
37-
#: messages.py:99 messages.py:100 messages.py:101
36+
#: messages.py:32
37+
msgid ""
38+
"this is a very very very very very very very very very very very very very"
39+
"long string!"
40+
msgstr ""
41+
42+
#: messages.py:49 messages.py:92 messages.py:93 messages.py:96 messages.py:97
43+
#: messages.py:102 messages.py:103 messages.py:104
3844
msgid "foo"
3945
msgid_plural "foos"
4046
msgstr[0] ""
4147
msgstr[1] ""
4248

43-
#: messages.py:47
49+
#: messages.py:50
4450
msgid "something"
4551
msgstr ""
4652

47-
#: messages.py:50
53+
#: messages.py:53
4854
msgid "Hello, {}!"
4955
msgstr ""
5056

51-
#: messages.py:54
57+
#: messages.py:57
5258
msgid "1"
5359
msgstr ""
5460

55-
#: messages.py:54
61+
#: messages.py:57
5662
msgid "2"
5763
msgstr ""
5864

59-
#: messages.py:55 messages.py:56
65+
#: messages.py:58 messages.py:59
6066
msgid "A"
6167
msgstr ""
6268

63-
#: messages.py:55 messages.py:56
69+
#: messages.py:58 messages.py:59
6470
msgid "B"
6571
msgstr ""
6672

67-
#: messages.py:57
73+
#: messages.py:60
6874
msgid "set"
6975
msgstr ""
7076

71-
#: messages.py:62 messages.py:63
77+
#: messages.py:65 messages.py:66
7278
msgid "nested string"
7379
msgstr ""
7480

75-
#: messages.py:68
81+
#: messages.py:71
7682
msgid "baz"
7783
msgstr ""
7884

79-
#: messages.py:71 messages.py:75
85+
#: messages.py:74 messages.py:78
8086
msgid "default value"
8187
msgstr ""
8288

83-
#: messages.py:91 messages.py:92 messages.py:95 messages.py:96
89+
#: messages.py:94 messages.py:95 messages.py:98 messages.py:99
8490
msgctxt "context"
8591
msgid "foo"
8692
msgid_plural "foos"
8793
msgstr[0] ""
8894
msgstr[1] ""
8995

90-
#: messages.py:102
96+
#: messages.py:105
9197
msgid "domain foo"
9298
msgstr ""
9399

94-
#: messages.py:118 messages.py:119
100+
#: messages.py:121 messages.py:122
95101
msgid "world"
96102
msgid_plural "worlds"
97103
msgstr[0] ""

Lib/test/test_tools/i18n_data/messages.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
multiline!
2929
""")
3030

31+
# very long string that should be wrapped
32+
_("this is a very very very very very very very very very very very very very long string!")
33+
3134
# Invalid arguments
3235
_()
3336
_(None)

Lib/test/test_tools/test_i18n.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,6 @@ def test_POT_Creation_Date(self):
161161
# This will raise if the date format does not exactly match.
162162
datetime.strptime(creationDate, '%Y-%m-%d %H:%M%z')
163163

164-
def test_wrap_to_width(self):
165-
msgid = self.extract_from_str(
166-
'''_("thisisaveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverlongstring")''')
167-
self.assertIn('\nlongstring', msgid[1])
168-
169164
def test_funcdocstring(self):
170165
for doc in ('"""doc"""', "r'''doc'''", "R'doc'", 'u"doc"'):
171166
with self.subTest(doc):

Tools/i18n/pygettext.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,39 @@ def escape_nonascii(s, encoding):
213213
return ''.join(escapes[b] for b in s.encode(encoding))
214214

215215

216-
def normalize(s, encoding):
216+
def normalize(s, encoding, options):
217217
# This converts the various Python string types into a format that is
218-
# appropriate for .po files, namely much closer to C style.
219-
lines = s.split('\n')
220-
if len(lines) == 1:
221-
s = '"' + escape(s, encoding) + '"'
222-
else:
223-
if not lines[-1]:
224-
del lines[-1]
225-
lines[-1] = lines[-1] + '\n'
226-
for i in range(len(lines)):
227-
lines[i] = escape(lines[i], encoding)
228-
lineterm = '\\n"\n"'
229-
s = '""\n"' + lineterm.join(lines) + '"'
230-
return s
218+
# appropriate for .po files, namely much closer to C style. While wrapping
219+
# to options.width.
220+
lines = []
221+
for line in s.splitlines(True):
222+
if len(escape(line, encoding)) > options.width:
223+
words = line.split()
224+
words.reverse()
225+
while words:
226+
buf = []
227+
size = 2
228+
while words:
229+
word = words[-1]
230+
escaped_word = escape(word, encoding)
231+
add_space = 1 if buf else 0
232+
if size + len(escaped_word) + add_space <= options.width:
233+
buf.append(words.pop())
234+
size += len(escaped_word) + add_space
235+
else:
236+
if not buf:
237+
buf.append(words.pop())
238+
break
239+
lines.append(' '.join(buf))
240+
else:
241+
lines.append(line)
242+
if len(lines) <= 1:
243+
return '"' + escape(s, encoding) + '"'
244+
if lines and not lines[-1]:
245+
del lines[-1]
246+
lines[-1] += '\n'
247+
return '""\n' + '\n'.join(
248+
[f'"{escape(line, encoding)}"' for line in lines])
231249

232250

233251
def containsAny(str, set):
@@ -618,28 +636,10 @@ def write_pot_file(messages, options, fp):
618636
# to skip translating some unimportant docstrings.
619637
print('#, docstring', file=fp)
620638
if msg.msgctxt is not None:
621-
print('msgctxt', normalize(msg.msgctxt, encoding), file=fp)
622-
623-
# If msgid is longer than width wrap
624-
msgid = normalize(msg.msgid, encoding)[1:-1] # normalize returns "msg"
625-
if len(msgid) > options.width:
626-
print('msgid ""', file=fp)
627-
while msgid:
628-
print(f'"{msgid[:options.width]}"', file=fp)
629-
msgid = msgid[options.width:]
630-
else:
631-
print(f'msgid "{msgid}"', file=fp)
632-
633-
# If msgid_plural is longer than width wrap
639+
print('msgctxt', normalize(msg.msgctxt, encoding, options), file=fp)
640+
print('msgid', normalize(msg.msgid, encoding, options), file=fp)
634641
if msg.msgid_plural is not None:
635-
msgid_plural = normalize(msg.msgid_plural, encoding)[1:-1] # normalize returns "msg"
636-
if len(msgid_plural) > options.width:
637-
print('msgid_plural ""', file=fp)
638-
while msgid_plural:
639-
print(f'"{msgid_plural[:options.width]}"', file=fp)
640-
msgid_plural = msgid_plural[options.width:]
641-
else:
642-
print(f'msgid_plural "{msgid_plural}"', file=fp)
642+
print('msgid_plural', normalize(msg.msgid_plural, encoding, options), file=fp)
643643
print('msgstr[0] ""', file=fp)
644644
print('msgstr[1] ""\n', file=fp)
645645
else:

0 commit comments

Comments
 (0)