Skip to content

Commit

Permalink
pypo: update line wrapping on adding unit
Browse files Browse the repository at this point in the history
This ensures that newly added unit is using storage configured line
wrapping.

Fixes WeblateOrg/weblate#10392
  • Loading branch information
nijel committed Dec 13, 2023
1 parent 61912a0 commit 9f7a97d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/translate/storage/test_pypo.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,21 @@ def test_dos_newlines_typecomment(self):
POT_Creation_Date="2023-10-24 10:19+0200",
)
assert bytes(pofile) == poexpected

def test_wrap_custom(self):
posource = 'msgid "HELLO"\nmsgstr ""\n'

# Instance with default wraps
store = self.StoreClass()
store.parse(posource.encode())
assert len(store.units) == 1

store.units[0].target = " ".join(["Hello world"] * 20)

assert max(len(line) for line in bytes(store).decode().splitlines()) <= 77

outstore = self.StoreClass()
outstore.wrapper.width = -1
outstore.addunit(store.units[0])

assert max(len(line) for line in bytes(outstore).decode().splitlines()) > 77
5 changes: 5 additions & 0 deletions translate/storage/pypo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,5 +1040,10 @@ def unit_iter(self):
yield unit

def addunit(self, unit):
needs_update = (
unit.wrapper and self.wrapper and (unit.wrapper.width != self.wrapper.width)
)
unit.wrapper = self.wrapper
super().addunit(unit)
if needs_update:
unit.target = unit.target

0 comments on commit 9f7a97d

Please sign in to comment.