i18n: fixed mistage in message generator which lead to some messages disappeared.
[blender-staging.git] / po / merge_po.py
1 #!/usr/bin/env python
2
3 # $Id$
4 # ***** BEGIN GPL LICENSE BLOCK *****
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #
20 # ***** END GPL LICENSE BLOCK *****
21
22 # <pep8 compliant>
23
24 # update the pot file according the POTFILES.in
25
26 import os
27 import sys
28 import collections
29
30 from codecs import open
31
32
33 def read_messages(fname):
34     def stripeol(s):
35         return s.rstrip("\n\r")
36
37     last_message = None
38
39     if hasattr(collections, 'OrderedDict'):
40         messages = collections.OrderedDict()
41         commented_messages = collections.OrderedDict()
42     else:
43         messages = {}
44         commented_messages = {}
45
46     reading_message = False
47     reading_translation = False
48     commented = False
49     message = ""
50     translation = ""
51     message_lines = []
52     translation_lines = []
53     comment_lines = []
54     with open(fname, 'r', "utf-8") as handle:
55         while True:
56             line = handle.readline()
57
58             if not line:
59                 break
60
61             line = stripeol(line)
62             if line.startswith("msgid") or line.startswith("#~ msgid"):
63                 if reading_translation:
64                     last_message['translation'] = translation
65                     translation_lines = []
66
67                 reading_message = True
68                 reading_translation = False
69
70                 if line.startswith('#~'):
71                     message = line[10:-1]
72                     commented = True
73                 else:
74                     message = line[7:-1]
75                     commented = False
76
77                 message_lines.append(message)
78             elif line.startswith("msgstr") or line.startswith("#~ msgstr"):
79                 reading_message = False
80                 reading_translation = True
81                 last_message = {'comment_lines': comment_lines,
82                                 'message_lines': message_lines,
83                                 'translation_lines': translation_lines}
84
85                 if commented:
86                     translation = line[11:-1]
87                     commented_messages[message] = last_message
88                 else:
89                     translation = line[8:-1]
90                     messages[message] = last_message
91
92                 message_lines = []
93                 comment_lines = []
94                 translation_lines.append(translation)
95             elif not line.startswith('"') and not line.startswith('#~ "'):
96                 if reading_translation:
97                     last_message['translation'] = translation
98                 else:
99                     comment_lines.append(line)
100
101                 reading_message = False
102                 reading_translation = False
103                 message_lines = []
104                 translation_lines = []
105             elif reading_message:
106                 if line.startswith('#~ "'):
107                     m = line[4:-1]
108                 else:
109                     m = line[1:-1]
110
111                 message += m
112                 message_lines.append(m)
113             elif reading_translation:
114                 if line.startswith('#~ "'):
115                     t = line[4:-1]
116                 else:
117                     t = line[1:-1]
118
119                 translation += t
120                 translation_lines.append(t)
121
122     return (messages, commented_messages)
123
124
125 def main():
126     if len(sys.argv) == 3:
127         dst_messages, tmp = read_messages(sys.argv[1])
128         from_messages, tmp = read_messages(sys.argv[2])
129
130         for msgid in dst_messages:
131             msg = dst_messages.get(msgid)
132             from_msg = from_messages.get(msgid)
133
134             if from_msg and from_msg['translation']:
135                 msg['translation'] = from_msg['translation']
136                 msg['translation_lines'] = from_msg['translation_lines']
137
138         with open(sys.argv[1], 'w', 'utf-8') as handle:
139             for msgid in dst_messages:
140                 item = dst_messages[msgid]
141
142                 for x in item['comment_lines']:
143                     handle.write(x + "\n")
144
145                 first = True
146                 for x in item['message_lines']:
147                     if first:
148                         handle.write("msgid \"%s\"\n" % (x))
149                     else:
150                         handle.write("\"%s\"\n" % (x))
151                     first = False
152
153                 first = True
154                 for x in item['translation_lines']:
155                     if first:
156                         handle.write("msgstr \"%s\"\n" % (x))
157                     else:
158                         handle.write("\"%s\"\n" % (x))
159                     first = False
160
161                 handle.write("\n")
162     else:
163         print('Usage: %s <destination-po> <source-po>' % (sys.argv[0]))
164
165
166 if __name__ == "__main__":
167     print("\n\n *** Running %r *** \n" % __file__)
168     main()