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