msgmerge functionality

1 view
Skip to first unread message

wpbasti

unread,
Dec 15, 2007, 6:32:09 PM12/15/07
to python-polib
Hi!

Are there any plans to integrate the feature of msgmerge to the tool
chain? If not, what is the preferred way to merge update a set of po
files from a pot files from Python? Suggestions are highly
appreciated.

Thanks for your great library! We plan to use it for the
internationalisation support in qooxdoo (http://qooxdoo.org).

Cheers,

Sebastian

David Jean Louis

unread,
Dec 16, 2007, 6:23:22 AM12/16/07
to python...@googlegroups.com
Hi Sebastian,

> Are there any plans to integrate the feature of msgmerge to the tool
> chain? If not, what is the preferred way to merge update a set of po
> files from a pot files from Python? Suggestions are highly
> appreciated.
>
I think this would be very handy indeed.
The correct way to do it is to do exactly what msgmerge do (from the man
page):

"""
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

wpbasti

unread,
Dec 16, 2007, 8:54:11 AM12/16/07
to python-polib
Thanks a lot David. I've just upgraded to the SVN version. Would be
great to have the full functionality. I am not a massive user of
gettext, but I think the fuzzy logic could me useful in large and long-
living projects.

Sebastian
Reply all
Reply to author
Forward
0 new messages