"""
Merges two Uniforum style .po files together. The def.po file is an
existing PO file with translations which will be taken over to the
newly created file as long as they still match; comments will be
preserved, but extracted comments and file positions will be discarded.
The ref.pot file is the last created PO file with up-to-date source
references but old translations, or a PO Template file (generally
created by xgettext); any translations or comments in the file will
be discarded, however dot comments and file positions will be
preserved. Where an exact match cannot be found, fuzzy matching is used
to produce better results.
"""
I am unsure about what it is meant by "fuzzy matching" (I'll have too
look at the C source of msgmerge), but so far I came with this
implementation (this is a POFile method):
(this is commited in svn with the corresponding test files).
def merge(self, refpot):
"""
XXX this could not work if encodings are different, needs thinking
and general refactoring of how polib handles encoding...
Convenience method that merge the current pofile with the pot file
provided. It behaves exactly as the gettext msgmerge utility:
* comments of this file will be preserved, but extracted comments
and occurrences will be discarded.
* any translations or comments in the file will be discarded,
however dot comments and file positions will be preserved.
**Keyword argument**:
- *refpot*: object POFile, the reference catalog.
**Example**:
>>> import polib
>>> refpot = polib.pofile('tests/test_merge.pot')
>>> po = polib.pofile('tests/test_merge_before.po')
>>> po.merge(refpot)
>>> expected_po = polib.pofile('tests/test_merge_after.po')
>>> str(po) == str(expected_po)
True
"""
for entry in refpot:
e = self.find(entry.msgid)
if e is None:
# entry is not in the po file, we must add it
# entry is created with msgid, occurrences and comment
self.append(POEntry(
msgid=entry.msgid,
occurrences=entry.occurrences,
comment=entry.comment
))
else:
# entry found, we update it...
e.occurrences = entry.occurrences
e.comment = entry.comment
# ok, now we must "obsolete" entries that are not in the refpot
# anymore
for entry in self:
if refpot.find(entry.msgid) is None:
entry.obsolete = True