i18n/posplit
changeset 11389 4fd49329a1b5
child 20359 ff6ab0b2ebf7
equal deleted inserted replaced
11388:db957a72fbd7 11389:4fd49329a1b5
       
     1 #!/usr/bin/env python
       
     2 #
       
     3 # posplit - split messages in paragraphs on .po/.pot files
       
     4 #
       
     5 # license: MIT/X11/Expat
       
     6 #
       
     7 
       
     8 import sys
       
     9 import polib
       
    10 
       
    11 def addentry(po, entry, cache):
       
    12     e = cache.get(entry.msgid)
       
    13     if e:
       
    14         e.occurrences.extend(entry.occurrences)
       
    15     else:
       
    16         po.append(entry)
       
    17         cache[entry.msgid] = entry
       
    18 
       
    19 def mkentry(orig, delta, msgid, msgstr):
       
    20     entry = polib.POEntry()
       
    21     entry.merge(orig)
       
    22     entry.msgid = msgid or orig.msgid
       
    23     entry.msgstr = msgstr or orig.msgstr
       
    24     entry.occurrences = [(p, int(l) + delta) for (p, l) in orig.occurrences]
       
    25     return entry
       
    26 
       
    27 if __name__ == "__main__":
       
    28     po = polib.pofile(sys.argv[1])
       
    29 
       
    30     cache = {}
       
    31     entries = po[:]
       
    32     po[:] = []
       
    33     for entry in entries:
       
    34         msgids = entry.msgid.split(u'\n\n')
       
    35         if entry.msgstr:
       
    36             msgstrs = entry.msgstr.split(u'\n\n')
       
    37         else:
       
    38             msgstrs = [u''] * len(msgids)
       
    39 
       
    40         if len(msgids) != len(msgstrs):
       
    41             # places the whole existing translation as a fuzzy
       
    42             # translation for each paragraph, to give the
       
    43             # translator a chance to recover part of the old
       
    44             # translation - erasing extra paragraphs is
       
    45             # probably better than retranslating all from start
       
    46             if 'fuzzy' not in entry.flags:
       
    47                 entry.flags.append('fuzzy')
       
    48             msgstrs = [entry.msgstr] * len(msgids)
       
    49 
       
    50         delta = 0
       
    51         for msgid, msgstr in zip(msgids, msgstrs):
       
    52             if msgid:
       
    53                 newentry = mkentry(entry, delta, msgid, msgstr)
       
    54                 addentry(po, newentry, cache)
       
    55             delta += 2 + msgid.count('\n')
       
    56     po.save()