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