izimobil / polib

Pure python library to manipulate, create, modify gettext files (pot, po and mo files).
MIT License
103 stars 29 forks source link

Saving accidentally removes obsolete Entry's previous_msgid #158

Open planetis-m opened 1 month ago

planetis-m commented 1 month ago

Repro:

# File: a.py
import polib

po_filepath = 'test_po_file.po'
# Create a new PO file
po = polib.POFile()

# Add a fuzzy entry
fuzzy_entry = polib.POEntry(
  msgid='fuzzy_message',
  msgstr='ασαφής_εικόνα',
  previous_msgid='fuzzy_image',
  fuzzy=True,  # Mark as fuzzy
  obsolete=True  # Mark as obsolete
)
po.append(fuzzy_entry)

# Save the PO file
po.save(po_filepath)

The .po file's contents:

#
msgid ""
msgstr ""

#~| msgid "fuzzy_image"
#~ msgid "fuzzy_message"
#~ msgstr "ασαφής_εικόνα"
# file: b.py
import polib

po_filepath = 'test_po_file.po'

# Reopen the PO file
po = polib.pofile(po_filepath)
po.save(po_filepath)

After running b.py

#
msgid ""
msgstr ""

#~ msgid "fuzzy_message"
#~ msgstr "ασαφής_εικόνα"
planetis-m commented 1 month ago

Oh boy: https://github.com/izimobil/polib/blob/85f6d1cf727452c2fcc7cf15f4c21e273d704b79/polib.py#L1360-L1361 Edit: dirty hack:

                if tokens[0] == '#~|':
                    # continue
                    tokens[0] = '#|'
                    line = '#|' + line[3:]
                    self.entry_obsolete = 1