Show More
@@ -1,81 +1,89 | |||||
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 | from __future__ import absolute_import, print_function |
|
8 | from __future__ import absolute_import, print_function | |
9 |
|
9 | |||
10 | import polib |
|
10 | import polib | |
11 | import re |
|
11 | import re | |
12 | import sys |
|
12 | import sys | |
13 |
|
13 | |||
14 | def addentry(po, entry, cache): |
|
14 | def addentry(po, entry, cache): | |
15 | e = cache.get(entry.msgid) |
|
15 | e = cache.get(entry.msgid) | |
16 | if e: |
|
16 | if e: | |
17 | e.occurrences.extend(entry.occurrences) |
|
17 | e.occurrences.extend(entry.occurrences) | |
|
18 | ||||
|
19 | # merge comments from entry | |||
|
20 | for comment in entry.comment.split('\n'): | |||
|
21 | if comment and comment not in e.comment: | |||
|
22 | if not e.comment: | |||
|
23 | e.comment = comment | |||
|
24 | else: | |||
|
25 | e.comment += '\n' + comment | |||
18 | else: |
|
26 | else: | |
19 | po.append(entry) |
|
27 | po.append(entry) | |
20 | cache[entry.msgid] = entry |
|
28 | cache[entry.msgid] = entry | |
21 |
|
29 | |||
22 | def mkentry(orig, delta, msgid, msgstr): |
|
30 | def mkentry(orig, delta, msgid, msgstr): | |
23 | entry = polib.POEntry() |
|
31 | entry = polib.POEntry() | |
24 | entry.merge(orig) |
|
32 | entry.merge(orig) | |
25 | entry.msgid = msgid or orig.msgid |
|
33 | entry.msgid = msgid or orig.msgid | |
26 | entry.msgstr = msgstr or orig.msgstr |
|
34 | entry.msgstr = msgstr or orig.msgstr | |
27 | entry.occurrences = [(p, int(l) + delta) for (p, l) in orig.occurrences] |
|
35 | entry.occurrences = [(p, int(l) + delta) for (p, l) in orig.occurrences] | |
28 | return entry |
|
36 | return entry | |
29 |
|
37 | |||
30 | if __name__ == "__main__": |
|
38 | if __name__ == "__main__": | |
31 | po = polib.pofile(sys.argv[1]) |
|
39 | po = polib.pofile(sys.argv[1]) | |
32 |
|
40 | |||
33 | cache = {} |
|
41 | cache = {} | |
34 | entries = po[:] |
|
42 | entries = po[:] | |
35 | po[:] = [] |
|
43 | po[:] = [] | |
36 | findd = re.compile(r' *\.\. (\w+)::') # for finding directives |
|
44 | findd = re.compile(r' *\.\. (\w+)::') # for finding directives | |
37 | for entry in entries: |
|
45 | for entry in entries: | |
38 | msgids = entry.msgid.split(u'\n\n') |
|
46 | msgids = entry.msgid.split(u'\n\n') | |
39 | if entry.msgstr: |
|
47 | if entry.msgstr: | |
40 | msgstrs = entry.msgstr.split(u'\n\n') |
|
48 | msgstrs = entry.msgstr.split(u'\n\n') | |
41 | else: |
|
49 | else: | |
42 | msgstrs = [u''] * len(msgids) |
|
50 | msgstrs = [u''] * len(msgids) | |
43 |
|
51 | |||
44 | if len(msgids) != len(msgstrs): |
|
52 | if len(msgids) != len(msgstrs): | |
45 | # places the whole existing translation as a fuzzy |
|
53 | # places the whole existing translation as a fuzzy | |
46 | # translation for each paragraph, to give the |
|
54 | # translation for each paragraph, to give the | |
47 | # translator a chance to recover part of the old |
|
55 | # translator a chance to recover part of the old | |
48 | # translation - erasing extra paragraphs is |
|
56 | # translation - erasing extra paragraphs is | |
49 | # probably better than retranslating all from start |
|
57 | # probably better than retranslating all from start | |
50 | if 'fuzzy' not in entry.flags: |
|
58 | if 'fuzzy' not in entry.flags: | |
51 | entry.flags.append('fuzzy') |
|
59 | entry.flags.append('fuzzy') | |
52 | msgstrs = [entry.msgstr] * len(msgids) |
|
60 | msgstrs = [entry.msgstr] * len(msgids) | |
53 |
|
61 | |||
54 | delta = 0 |
|
62 | delta = 0 | |
55 | for msgid, msgstr in zip(msgids, msgstrs): |
|
63 | for msgid, msgstr in zip(msgids, msgstrs): | |
56 | if msgid and msgid != '::': |
|
64 | if msgid and msgid != '::': | |
57 | newentry = mkentry(entry, delta, msgid, msgstr) |
|
65 | newentry = mkentry(entry, delta, msgid, msgstr) | |
58 | mdirective = findd.match(msgid) |
|
66 | mdirective = findd.match(msgid) | |
59 | if mdirective: |
|
67 | if mdirective: | |
60 | if not msgid[mdirective.end():].rstrip(): |
|
68 | if not msgid[mdirective.end():].rstrip(): | |
61 | # only directive, nothing to translate here |
|
69 | # only directive, nothing to translate here | |
62 | delta += 2 |
|
70 | delta += 2 | |
63 | continue |
|
71 | continue | |
64 | directive = mdirective.group(1) |
|
72 | directive = mdirective.group(1) | |
65 | if directive in ('container', 'include'): |
|
73 | if directive in ('container', 'include'): | |
66 | if msgid.rstrip('\n').count('\n') == 0: |
|
74 | if msgid.rstrip('\n').count('\n') == 0: | |
67 | # only rst syntax, nothing to translate |
|
75 | # only rst syntax, nothing to translate | |
68 | delta += 2 |
|
76 | delta += 2 | |
69 | continue |
|
77 | continue | |
70 | else: |
|
78 | else: | |
71 | # lines following directly, unexpected |
|
79 | # lines following directly, unexpected | |
72 | print('Warning: text follows line with directive' \ |
|
80 | print('Warning: text follows line with directive' \ | |
73 | ' %s' % directive) |
|
81 | ' %s' % directive) | |
74 | comment = 'do not translate: .. %s::' % directive |
|
82 | comment = 'do not translate: .. %s::' % directive | |
75 | if not newentry.comment: |
|
83 | if not newentry.comment: | |
76 | newentry.comment = comment |
|
84 | newentry.comment = comment | |
77 | elif comment not in newentry.comment: |
|
85 | elif comment not in newentry.comment: | |
78 | newentry.comment += '\n' + comment |
|
86 | newentry.comment += '\n' + comment | |
79 | addentry(po, newentry, cache) |
|
87 | addentry(po, newentry, cache) | |
80 | delta += 2 + msgid.count('\n') |
|
88 | delta += 2 + msgid.count('\n') | |
81 | po.save() |
|
89 | po.save() |
General Comments 0
You need to be logged in to leave comments.
Login now