Show More
@@ -1,66 +1,69 | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # |
|
2 | # | |
3 | # posplit - split messages in paragraphs on .po/.pot files |
|
3 | # posplit - split messages in paragraphs on .po/.pot files | |
4 | # |
|
4 | # | |
5 | # license: MIT/X11/Expat |
|
5 | # license: MIT/X11/Expat | |
6 | # |
|
6 | # | |
7 |
|
7 | |||
8 | import re |
|
8 | import re | |
9 | import sys |
|
9 | import sys | |
10 | import polib |
|
10 | import polib | |
11 |
|
11 | |||
12 | def addentry(po, entry, cache): |
|
12 | def addentry(po, entry, cache): | |
13 | e = cache.get(entry.msgid) |
|
13 | e = cache.get(entry.msgid) | |
14 | if e: |
|
14 | if e: | |
15 | e.occurrences.extend(entry.occurrences) |
|
15 | e.occurrences.extend(entry.occurrences) | |
16 | else: |
|
16 | else: | |
17 | po.append(entry) |
|
17 | po.append(entry) | |
18 | cache[entry.msgid] = entry |
|
18 | cache[entry.msgid] = entry | |
19 |
|
19 | |||
20 | def mkentry(orig, delta, msgid, msgstr): |
|
20 | def mkentry(orig, delta, msgid, msgstr): | |
21 | entry = polib.POEntry() |
|
21 | entry = polib.POEntry() | |
22 | entry.merge(orig) |
|
22 | entry.merge(orig) | |
23 | entry.msgid = msgid or orig.msgid |
|
23 | entry.msgid = msgid or orig.msgid | |
24 | entry.msgstr = msgstr or orig.msgstr |
|
24 | entry.msgstr = msgstr or orig.msgstr | |
25 | entry.occurrences = [(p, int(l) + delta) for (p, l) in orig.occurrences] |
|
25 | entry.occurrences = [(p, int(l) + delta) for (p, l) in orig.occurrences] | |
26 | return entry |
|
26 | return entry | |
27 |
|
27 | |||
28 | if __name__ == "__main__": |
|
28 | if __name__ == "__main__": | |
29 | po = polib.pofile(sys.argv[1]) |
|
29 | po = polib.pofile(sys.argv[1]) | |
30 |
|
30 | |||
31 | cache = {} |
|
31 | cache = {} | |
32 | entries = po[:] |
|
32 | entries = po[:] | |
33 | po[:] = [] |
|
33 | po[:] = [] | |
34 | findd = re.compile(r' *\.\. (\w+)::') # for finding directives |
|
34 | findd = re.compile(r' *\.\. (\w+)::') # for finding directives | |
35 | for entry in entries: |
|
35 | for entry in entries: | |
36 | msgids = entry.msgid.split(u'\n\n') |
|
36 | msgids = entry.msgid.split(u'\n\n') | |
37 | if entry.msgstr: |
|
37 | if entry.msgstr: | |
38 | msgstrs = entry.msgstr.split(u'\n\n') |
|
38 | msgstrs = entry.msgstr.split(u'\n\n') | |
39 | else: |
|
39 | else: | |
40 | msgstrs = [u''] * len(msgids) |
|
40 | msgstrs = [u''] * len(msgids) | |
41 |
|
41 | |||
42 | if len(msgids) != len(msgstrs): |
|
42 | if len(msgids) != len(msgstrs): | |
43 | # places the whole existing translation as a fuzzy |
|
43 | # places the whole existing translation as a fuzzy | |
44 | # translation for each paragraph, to give the |
|
44 | # translation for each paragraph, to give the | |
45 | # translator a chance to recover part of the old |
|
45 | # translator a chance to recover part of the old | |
46 | # translation - erasing extra paragraphs is |
|
46 | # translation - erasing extra paragraphs is | |
47 | # probably better than retranslating all from start |
|
47 | # probably better than retranslating all from start | |
48 | if 'fuzzy' not in entry.flags: |
|
48 | if 'fuzzy' not in entry.flags: | |
49 | entry.flags.append('fuzzy') |
|
49 | entry.flags.append('fuzzy') | |
50 | msgstrs = [entry.msgstr] * len(msgids) |
|
50 | msgstrs = [entry.msgstr] * len(msgids) | |
51 |
|
51 | |||
52 | delta = 0 |
|
52 | delta = 0 | |
53 | for msgid, msgstr in zip(msgids, msgstrs): |
|
53 | for msgid, msgstr in zip(msgids, msgstrs): | |
54 | if msgid and msgid != '::': |
|
54 | if msgid and msgid != '::': | |
55 | newentry = mkentry(entry, delta, msgid, msgstr) |
|
55 | newentry = mkentry(entry, delta, msgid, msgstr) | |
56 | mdirective = findd.match(msgid) |
|
56 | mdirective = findd.match(msgid) | |
57 | if mdirective: |
|
57 | if mdirective: | |
|
58 | if not msgid[mdirective.end():].rstrip(): | |||
|
59 | # only directive, nothing to translate here | |||
|
60 | continue | |||
58 | directive = mdirective.group(1) |
|
61 | directive = mdirective.group(1) | |
59 | comment = 'do not translate: .. %s::' % directive |
|
62 | comment = 'do not translate: .. %s::' % directive | |
60 | if not newentry.comment: |
|
63 | if not newentry.comment: | |
61 | newentry.comment = comment |
|
64 | newentry.comment = comment | |
62 | elif comment not in newentry.comment: |
|
65 | elif comment not in newentry.comment: | |
63 | newentry.comment += '\n' + comment |
|
66 | newentry.comment += '\n' + comment | |
64 | addentry(po, newentry, cache) |
|
67 | addentry(po, newentry, cache) | |
65 | delta += 2 + msgid.count('\n') |
|
68 | delta += 2 + msgid.count('\n') | |
66 | po.save() |
|
69 | po.save() |
General Comments 0
You need to be logged in to leave comments.
Login now