Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions babel/messages/pofile.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def escape(string: str) -> str:
).replace('\n', '\\n').replace('"', '\\"')


def normalize(string: str, prefix: str = '', width: int = 76) -> str:
def normalize(string: str, prefix: str = '', width: int = 76, first_line_offset: int = 0) -> str:
r"""Convert a string into a format that is appropriate for .po files.

>>> print(normalize('''Say:
Expand All @@ -491,6 +491,9 @@ def normalize(string: str, prefix: str = '', width: int = 76) -> str:
:param prefix: a string that should be prepended to every line
:param width: the maximum line width; use `None`, 0, or a negative number
to completely disable line wrapping
:param first_line_offset: the number of characters (such as a ``msgstr ``
keyword) written between the prefix and the
string on its first line
"""
if width and width > 0:
prefixlen = len(prefix)
Expand Down Expand Up @@ -520,7 +523,10 @@ def normalize(string: str, prefix: str = '', width: int = 76) -> str:
lines = string.splitlines(True)

if len(lines) <= 1:
return escape(string)
linelen = len(escape(string)) + len(prefix)
# Start the string on its own line if it only fits without the keyword
if not (lines and width and width > 0 and linelen <= width < linelen + first_line_offset):
return escape(string)

# Remove empty trailing line
if lines and not lines[-1]:
Expand Down Expand Up @@ -680,24 +686,28 @@ def _format_conflict(key: str | tuple[str, str], conflicts: list[ConflictInfo],
else:
yield f"{normalize(message.string, prefix=prefix, width=width)}\n"

def _format_field(keyword, string, prefix=''):
value = normalize(string, prefix=prefix, width=width, first_line_offset=len(keyword) + 1)
return f"{prefix}{keyword} {value}\n"

def _format_message(message, prefix=''):
if isinstance(message.id, (list, tuple)):
if message.context:
yield f"{prefix}msgctxt {normalize(message.context, prefix=prefix, width=width)}\n"
yield f"{prefix}msgid {normalize(message.id[0], prefix=prefix, width=width)}\n"
yield f"{prefix}msgid_plural {normalize(message.id[1], prefix=prefix, width=width)}\n"
yield _format_field('msgctxt', message.context, prefix=prefix)
yield _format_field('msgid', message.id[0], prefix=prefix)
yield _format_field('msgid_plural', message.id[1], prefix=prefix)

for idx in range(catalog.num_plurals):
try:
string = message.string[idx]
except IndexError:
string = ''
yield f"{prefix}msgstr[{idx:d}] {normalize(string, prefix=prefix, width=width)}\n"
yield _format_field(f'msgstr[{idx:d}]', string, prefix=prefix)
else:
if message.context:
yield f"{prefix}msgctxt {normalize(message.context, prefix=prefix, width=width)}\n"
yield f"{prefix}msgid {normalize(message.id, prefix=prefix, width=width)}\n"
yield f"{prefix}msgstr {normalize(message.string or '', prefix=prefix, width=width)}\n"
yield _format_field('msgctxt', message.context, prefix=prefix)
yield _format_field('msgid', message.id, prefix=prefix)
yield _format_field('msgstr', message.string or '', prefix=prefix)

for message in _sort_messages(catalog, sort_by=sort_by):
if not message.id: # This is the header "message"
Expand Down
25 changes: 25 additions & 0 deletions tests/messages/test_pofile_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,31 @@ def test_wrap_long_lines_with_long_word():
msgstr ""'''


def test_wrap_long_msgstr_including_keyword():
text = ("Lorem Ipsum is simply dummy text of the printing and typesetting "
"industry. Lorem Ipsum has been the industrystandard")
catalog = Catalog()
catalog.add('Lorem Ipsum is', text)
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=True, width=120)
assert buf.getvalue().strip() == b'''msgid "Lorem Ipsum is"
msgstr ""
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrystandard"'''


def test_wrap_keyword_lines_within_width():
text = "The quick brown fox jumps over it." # 36 characters once quoted
catalog = Catalog(locale='de')
catalog.add(text, text, context=text)
catalog.add((f"{text} ", f"{text}!"), (text, text))
catalog.obsolete[f"{text}?"] = Message(f"{text}?", text)
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=True, width=40)
lines = buf.getvalue().decode('utf-8').splitlines()
assert len(lines) > 10
assert [line for line in lines if len(line) > 40] == []


def test_wrap_long_lines_in_header():
"""
Verify that long lines in the header comment are wrapped correctly.
Expand Down