3 # ***** BEGIN GPL LICENSE BLOCK *****
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # ***** END GPL LICENSE BLOCK *****
23 # update the pot file according the POTFILES.in
29 from codecs import open
31 CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
33 FILE_NAME_POT = os.path.join(CURRENT_DIR, "blender.pot")
36 def read_messages(fname):
38 return s.rstrip("\n\r")
42 if hasattr(collections, 'OrderedDict'):
43 messages = collections.OrderedDict()
44 commented_messages = collections.OrderedDict()
47 commented_messages = {}
49 reading_message = False
50 reading_translation = False
55 translation_lines = []
57 with open(fname, 'r', "utf-8") as handle:
59 line = handle.readline()
65 if line.startswith("msgid") or line.startswith("#~ msgid"):
66 if reading_translation:
67 last_message['translation'] = translation
68 translation_lines = []
70 reading_message = True
71 reading_translation = False
73 if line.startswith('#~'):
80 message_lines.append(message)
81 elif line.startswith("msgstr") or line.startswith("#~ msgstr"):
82 reading_message = False
83 reading_translation = True
84 last_message = {'comment_lines': comment_lines,
85 'message_lines': message_lines,
86 'translation_lines': translation_lines}
89 translation = line[11:-1]
90 commented_messages[message] = last_message
92 translation = line[8:-1]
93 messages[message] = last_message
97 translation_lines.append(translation)
98 elif not line.startswith('"') and not line.startswith('#~ "'):
99 if reading_translation:
100 last_message['translation'] = translation
102 comment_lines.append(line)
104 reading_message = False
105 reading_translation = False
107 translation_lines = []
108 elif reading_message:
109 if line.startswith('#~ "'):
115 message_lines.append(m)
116 elif reading_translation:
117 if line.startswith('#~ "'):
123 translation_lines.append(t)
125 return (messages, commented_messages)
128 def do_clean(po, pot_messages):
129 po_messages, commented_messages = read_messages(po)
131 for msgid in commented_messages:
132 if pot_messages.get(msgid):
133 t = po_messages.get(msgid)
135 print("Reusing full item from commented "
136 "lines for msgid '%s'" % msgid)
137 po_messages[msgid] = commented_messages[msgid]
138 elif not t['translation']:
139 print("Reusing translation from commented "
140 "lines for msgid '%s'" % msgid)
141 m = commented_messages[msgid]
142 t['translation'] = m['translation']
143 t['translation_lines'] = m['translation_lines']
145 with open(po, 'w', 'utf-8') as handle:
146 for msgid in po_messages:
147 item = po_messages[msgid]
149 for x in item['comment_lines']:
150 handle.write(x + "\n")
153 for x in item['message_lines']:
155 handle.write("msgid \"%s\"\n" % x)
157 handle.write("\"%s\"\n" % x)
161 for x in item['translation_lines']:
163 handle.write("msgstr \"%s\"\n" % x)
165 handle.write("\"%s\"\n" % x)
172 pot_messages, commented_messages = read_messages(FILE_NAME_POT)
174 if len(sys.argv) > 1:
175 for lang in sys.argv[1:]:
176 po = os.path.join(CURRENT_DIR, lang + '.po')
178 if os.path.exists(po):
179 do_clean(po, pot_messages)
181 for po in os.listdir(CURRENT_DIR):
182 if po.endswith('.po'):
183 print('Processing %s...' % (po))
184 do_clean(po, pot_messages)
187 if __name__ == "__main__":
188 print("\n\n *** Running %r *** \n" % __file__)