Fixed normalize() not wrapping msgstr that exceeds width when keyword prefix is included. by tejas-ae · Pull Request #1270 · python-babel/babel · GitHub
Root cause: normalize() checks whether each line of the string exceeds width, but it does not account for the keyword prefix (msgstr , msgid , msgctxt ) that the caller prepends on the same line. A 116-char string with width=120 escapes to 118 chars — under the limit — so it is returned as a single line. The combined output msgstr "..." is 125 chars, which exceeds width=120.
Fix: Add a first_line_offset parameter (default 0) to normalize(). When the escaped string plus first_line_offset would exceed width, the multi-line ""\n"..." form is used, placing the content on its own line where the keyword prefix is absent.
All call sites in generate_po are updated to pass first_line_offset=len(prefix) + len("msgstr ") (and equivalents for msgid, msgctxt, msgid_plural, msgstr[N]).
Before:
msgstr "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrystandard"
# ↑ 125 chars — exceeds -w 120
After:
msgstr ""
"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industrystandard"
# ↑ msgstr line = 9 chars; content line = 118 chars — both within 120
Two regression tests added to tests/messages/test_pofile.py.
Used an LLM to help trace the root cause and draft the fix; all output verified manually.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1266.
Root cause: normalize() checks whether each line of the string exceeds width, but it does not account for the keyword prefix (msgstr , msgid , msgctxt ) that the caller prepends on the same line. A 116-char string with width=120 escapes to 118 chars — under the limit — so it is returned as a single line. The combined output msgstr "..." is 125 chars, which exceeds width=120.
Fix: Add a first_line_offset parameter (default 0) to normalize(). When the escaped string plus first_line_offset would exceed width, the multi-line ""\n"..." form is used, placing the content on its own line where the keyword prefix is absent.
All call sites in generate_po are updated to pass first_line_offset=len(prefix) + len("msgstr ") (and equivalents for msgid, msgctxt, msgid_plural, msgstr[N]).
Before:
After:
Two regression tests added to tests/messages/test_pofile.py.
Used an LLM to help trace the root cause and draft the fix; all output verified manually.