4 # ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 # ***** END GPL LICENSE BLOCK *****
24 # update the pot file according the POTFILES.in
29 from codecs import open
32 def read_messages(fname):
34 return s.rstrip("\n\r")
38 if hasattr(collections, 'OrderedDict'):
39 messages = collections.OrderedDict()
40 commented_messages = collections.OrderedDict()
43 commented_messages = {}
45 reading_message = False
46 reading_translation = False
51 translation_lines = []
53 with open(fname, 'r', "utf-8") as handle:
55 line = handle.readline()
61 if line.startswith("msgid") or line.startswith("#~ msgid"):
62 if reading_translation:
63 last_message['translation'] = translation
64 translation_lines = []
66 reading_message = True
67 reading_translation = False
69 if line.startswith('#~'):
76 message_lines.append(message)
77 elif line.startswith("msgstr") or line.startswith("#~ msgstr"):
78 reading_message = False
79 reading_translation = True
80 last_message = {'comment_lines': comment_lines,
81 'message_lines': message_lines,
82 'translation_lines': translation_lines}
85 translation = line[11:-1]
86 commented_messages[message] = last_message
88 translation = line[8:-1]
89 messages[message] = last_message
93 translation_lines.append(translation)
94 elif not line.startswith('"') and not line.startswith('#~ "'):
95 if reading_translation:
96 last_message['translation'] = translation
98 comment_lines.append(line)
100 reading_message = False
101 reading_translation = False
103 translation_lines = []
104 elif reading_message:
105 if line.startswith('#~ "'):
111 message_lines.append(m)
112 elif reading_translation:
113 if line.startswith('#~ "'):
119 translation_lines.append(t)
121 return (messages, commented_messages)
125 if len(sys.argv) == 3:
126 dst_messages, tmp = read_messages(sys.argv[1])
127 from_messages, tmp = read_messages(sys.argv[2])
129 for msgid in dst_messages:
130 msg = dst_messages.get(msgid)
131 from_msg = from_messages.get(msgid)
133 if from_msg and from_msg['translation']:
134 msg['translation'] = from_msg['translation']
135 msg['translation_lines'] = from_msg['translation_lines']
137 with open(sys.argv[1], 'w', 'utf-8') as handle:
138 for msgid in dst_messages:
139 item = dst_messages[msgid]
141 for x in item['comment_lines']:
142 handle.write(x + "\n")
145 for x in item['message_lines']:
147 handle.write("msgid \"%s\"\n" % x)
149 handle.write("\"%s\"\n" % x)
153 for x in item['translation_lines']:
155 handle.write("msgstr \"%s\"\n" % x)
157 handle.write("\"%s\"\n" % x)
162 print('Usage: %s <destination-po> <source-po>' % (sys.argv[0]))
165 if __name__ == "__main__":
166 print("\n\n *** Running %r *** \n" % __file__)