Repository navigation
Count the keyword when wrapping PO values - #1420
Open
ryanchou1994 wants to merge 1 commit into
Open
ryanchou1994 wants to merge 1 commit into
ryanchou1994 wants to merge 1 commit into
Conversation
write_po() decided whether a value fits on one line without the keyword written before it (msgid, msgstr, msgstr[N], msgctxt), so a value that fit only on its own line was written as a single over-long line. With width=120 the example in the issue produced a 125-character msgstr line. normalize() gets an optional first_line_offset argument. When a single-line value fits by itself but not after the keyword, it is written in the multi-line form starting with an empty string. Values that cannot fit even on their own line keep the old output, and callers that don't pass the argument are unchanged. Fixes python-babel#1266
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
write_po()puts each value on the same line as its keyword (msgstr "..."), butnormalize()only compared the quoted string plus the line prefix againstwidth. The keyword itself was never counted. With the 116-character msgstr from the issue andpybabel update -w 120, the quoted string is 118 characters, so it was treated as fitting and written as a single 125-character line (and an already wrapped value was un-wrapped).Change
normalize()gets an optionalfirst_line_offsetargument (default0, so existing callers get the same output as before).msgstr ""followed by the value on the next line), the same layout already used for wrapped values._format_messagepasses the keyword length through a small_format_fieldhelper, so msgctxt, msgid, msgid_plural, msgstr, msgstr[N] and obsolete (#~) entries are all covered.Note that this changes output for values whose length falls in the last few columns before
width(counting the keyword): they now start on their own line. Runningpybabel updateon an existing catalog will reflow those entries once.Not changed on purpose: the conflict writer
_format_conflict(used byconcat/merge), and#| msgidprevious-id comments.Tests
test_wrap_long_msgstr_including_keyword: the case from the issue atwidth=120.test_wrap_keyword_lines_within_width: context, plural and obsolete entries atwidth=40, asserting no line exceeds the width.Both fail on
master(ecf1da8) and pass with this change. Full run on Linux (Python 3.14, CLDR 48.2 imported withscripts/download_import_cldr.py):pytest tests babel/messages/pofile.py --doctest-modulesgoes from 6618 to 6620 passed, no failures.