Skip to content

Fixed normalize() not wrapping msgstr that exceeds width when keyword prefix is included.#1270

Open
tejasae-afk wants to merge 3 commits intopython-babel:masterfrom
tejasae-afk:fix/normalize-first-line-offset
Open

Fixed normalize() not wrapping msgstr that exceeds width when keyword prefix is included.#1270
tejasae-afk wants to merge 3 commits intopython-babel:masterfrom
tejasae-afk:fix/normalize-first-line-offset

Conversation

@tejasae-afk
Copy link
Copy Markdown

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:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Over-long msgstr not line wrapped when setting --width

1 participant