Skip to content

Commit fbe5b93

Browse files
Address Serhiy's suggestions
1 parent 8d03cbf commit fbe5b93

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

Lib/test/test_tools/test_i18n.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ def test_normalize_multiline(self):
525525
s = 'multi-line\n translation'
526526
s_expected = '""\n"multi-line\\n"\n" translation"'
527527

528-
data = normalize(s, 'UTF-8', options)
528+
data = normalize(s, 'UTF-8', 'msgid', options)
529529
self.assertEqual(s_expected, data)
530530

531531
def test_normalize_wrap(self):
@@ -534,9 +534,9 @@ def test_normalize_wrap(self):
534534
make_escapes(True)
535535

536536
s = 'this string should be wrapped to 30 chars'
537-
s_expected = '""\n"this string should be wrapped "\n"to 30 chars"'
537+
s_expected = '""\n"this string should be "\n"wrapped to 30 chars"'
538538

539-
data = normalize(s, 'UTF-8', options)
539+
data = normalize(s, 'UTF-8', 'msgid', options)
540540
self.assertEqual(s_expected, data)
541541

542542
def test_normalize_nostr(self):
@@ -547,7 +547,7 @@ def test_normalize_nostr(self):
547547
s = ''
548548
s_expected = '""'
549549

550-
data = normalize(s, 'UTF-8', options)
550+
data = normalize(s, 'UTF-8', 'msgid', options)
551551
self.assertEqual(s_expected, data)
552552

553553
def test_normalize_short_width(self):
@@ -558,7 +558,7 @@ def test_normalize_short_width(self):
558558
s = 'foos'
559559
s_expected = '"foos"'
560560

561-
data = normalize(s, 'UTF-8', options)
561+
data = normalize(s, 'UTF-8', 'msgid', options)
562562
self.assertEqual(s_expected, data)
563563

564564

Tools/i18n/pygettext.py

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

216-
# Split a string according to whitespaces and keep
217-
# the whitespaces in the resulting array thanks to
218-
# the capturing group.
219-
_space_splitter = re.compile(r'(\s+)').split
220216

221-
def normalize(s, encoding, options):
217+
_space_splitter = re.compile(r'(\s+)')
218+
219+
def normalize(s, encoding, prefix, options):
222220
# This converts the various Python string types into a format that is
223221
# appropriate for .po files, namely much closer to C style,
224222
# while wrapping to options.width.
225223
lines = []
226224
for line in s.splitlines(True):
227-
if len(escape(line, encoding)) > options.width and ' ' in line: # don't wrap single words
228-
words = _space_splitter(line)
225+
escaped_line = escape(line, encoding)
226+
if len(escaped_line) + len(prefix) + 2 > options.width and _space_splitter.search(line): # don't wrap single words
227+
words = _space_splitter.split(line)
229228
words.reverse()
230229
buf = []
231230
size = 0
232231
while words:
233232
word = words.pop()
234-
escaped_word_len = len(escape(word, encoding))
233+
escaped_word = escape(word, encoding)
234+
escaped_word_len = len(escaped_word)
235235
new_size = size + escaped_word_len
236-
if new_size <= options.width:
237-
buf.append(word)
236+
if new_size + 2 <= options.width:
237+
buf.append(escaped_word)
238+
size = new_size
239+
elif not buf:
240+
buf.append(escaped_word)
238241
size = new_size
239242
else:
240243
lines.append(''.join(buf))
241-
buf = [word]
244+
buf = [escaped_word]
242245
size = escaped_word_len
243246
lines.append(''.join(buf))
244247
else:
245-
lines.append(line)
248+
lines.append(escaped_line)
246249
if len(lines) <= 1:
247250
return f'"{escape(s, encoding)}"'
248-
return '""\n' + '\n'.join(f'"{escape(line, encoding)}"' for line in lines)
251+
return '""\n' + '\n'.join(f'"{line}"' for line in lines)
249252

250253

251254
def containsAny(str, set):
@@ -636,10 +639,10 @@ def write_pot_file(messages, options, fp):
636639
# to skip translating some unimportant docstrings.
637640
print('#, docstring', file=fp)
638641
if msg.msgctxt is not None:
639-
print('msgctxt', normalize(msg.msgctxt, encoding, options), file=fp)
640-
print('msgid', normalize(msg.msgid, encoding, options), file=fp)
642+
print('msgctxt', normalize(msg.msgctxt, encoding, 'msgctxt', options), file=fp)
643+
print('msgid', normalize(msg.msgid, encoding, 'msgid', options), file=fp)
641644
if msg.msgid_plural is not None:
642-
print('msgid_plural', normalize(msg.msgid_plural, encoding, options), file=fp)
645+
print('msgid_plural', normalize(msg.msgid_plural, encoding, 'msgid_plural', options), file=fp)
643646
print('msgstr[0] ""', file=fp)
644647
print('msgstr[1] ""\n', file=fp)
645648
else:

0 commit comments

Comments
 (0)