polib
polib copied to clipboard
customizable entries
Hi there. For our use case we have specific formatting of the occurrences section in .po files. So i made POEntry and _BaseEntry easier to customize
With these adjustments i can define a custom POFile
that creates custom POEntry
elements.
(I am no python expert. Not sure if I could write this in one line)
class CustomPOFile(polib.POFile):
def append(self, entry: polib.POEntry) -> None:
custom = CustomPOEntry(wrapwidth=self.wrapwidth)
custom.msgid = entry.msgid
custom.msgstr = entry.msgstr
custom.msgid_plural = entry.msgid_plural
custom.msgstr_plural = entry.msgstr_plural
custom.msgctxt = entry.msgctxt
custom.obsolete = entry.obsolete
custom.encoding = entry.encoding
custom.comment = entry.comment
custom.tcomment = entry.tcomment
custom.occurrences = entry.occurrences
custom.flags = entry.flags
custom.previous_msgctxt = entry.previous_msgctxt
custom.previous_msgid = entry.previous_msgid
custom.previous_msgid_plural = entry.previous_msgid_plural
custom.linenum = entry.linenum
return super().append(custom)
And in my custom POEntry i can overwrite methods with my custom implementation:
class CustomPOEntry(polib.POEntry):
def _unicode_occurrences(self, ret, **kwargs):
# custom stuff
# ret += [...]
pass
Let me know if you would do something else. (also naming) As I said: not a python expert.
To be clear. I could use this approach before. I just had replace the whole POEntry.__unicode__
implementation