##// END OF EJS Templates
i18n: calculate correct line number in source of messages to be translated...
FUJIWARA Katsunori -
r28074:a1924bc6 default
parent child Browse files
Show More
@@ -1,77 +1,79
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 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 if not msgid[mdirective.end():].rstrip():
59 59 # only directive, nothing to translate here
60 delta += 2
60 61 continue
61 62 directive = mdirective.group(1)
62 63 if directive in ('container', 'include'):
63 64 if msgid.rstrip('\n').count('\n') == 0:
64 65 # only rst syntax, nothing to translate
66 delta += 2
65 67 continue
66 68 else:
67 69 # lines following directly, unexpected
68 70 print 'Warning: text follows line with directive' \
69 71 ' %s' % directive
70 72 comment = 'do not translate: .. %s::' % directive
71 73 if not newentry.comment:
72 74 newentry.comment = comment
73 75 elif comment not in newentry.comment:
74 76 newentry.comment += '\n' + comment
75 77 addentry(po, newentry, cache)
76 78 delta += 2 + msgid.count('\n')
77 79 po.save()
General Comments 0
You need to be logged in to leave comments. Login now