i18n: fixed mistage in message generator which lead to some messages disappeared.
[blender.git] / po / update_pot.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 subprocess
27 import os
28 from codecs import open
29
30 GETTEXT_XGETTEXT_EXECUTABLE = "xgettext"
31 CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
32 SOURCE_DIR = os.path.normpath(os.path.abspath(os.path.join(CURRENT_DIR, "..")))
33 DOMAIN = "blender"
34 COMMENT_PREFIX = "#~ "  # from update_msg.py
35
36 FILE_NAME_POT = os.path.join(CURRENT_DIR, "blender.pot")
37 FILE_NAME_MESSAGES = os.path.join(CURRENT_DIR, "messages.txt")
38
39
40 def main():
41     cmd = (GETTEXT_XGETTEXT_EXECUTABLE,
42            "--files-from=%s" % os.path.join(SOURCE_DIR, "po", "POTFILES.in"),
43            "--keyword=_",
44            "--keyword=N_",
45            "--directory=%s" % SOURCE_DIR,
46            "--output=%s" % os.path.join(SOURCE_DIR, "po", "%s.pot" % DOMAIN),
47            "--from-code=utf-8",
48            )
49
50     print(" ".join(cmd))
51     process = subprocess.Popen(cmd)
52     process.wait()
53
54     def stripeol(s):
55         return s.rstrip("\n\r")
56
57     pot_messages = {}
58     reading_message = False
59     message = ""
60     with open(FILE_NAME_POT, 'r', "utf-8") as handle:
61         while True:
62             line = handle.readline()
63
64             if not line:
65                 break
66
67             line = stripeol(line)
68             if line.startswith("msgid"):
69                 reading_message = True
70                 message = line[7:-1]
71             elif line.startswith("msgstr"):
72                 reading_message = False
73                 pot_messages[message] = True
74             elif reading_message:
75                 message += line[1:-1]
76
77     # add messages collected automatically from RNA
78     with open(FILE_NAME_POT, "a", "utf-8") as pot_handle:
79         with open(FILE_NAME_MESSAGES, 'r', "utf-8") as handle:
80             msgsrc_ls = []
81             while True:
82                 line = handle.readline()
83
84                 if not line:
85                     break
86
87                 line = stripeol(line)
88
89                 # COMMENT_PREFIX
90                 if line.startswith(COMMENT_PREFIX):
91                     msgsrc_ls.append(line[len(COMMENT_PREFIX):].strip())
92                 else:
93                     line = line.replace("\\", "\\\\")
94                     line = line.replace("\"", "\\\"")
95                     line = line.replace("\t", "\\t")
96
97                     if not pot_messages.get(line):
98                         for msgsrc in msgsrc_ls:
99                             pot_handle.write("#: %s\n" % msgsrc)
100                         pot_handle.write("msgid \"%s\"\n" % line)
101                         pot_handle.write("msgstr \"\"\n\n")
102                     msgsrc_ls[:] = []
103
104
105 if __name__ == "__main__":
106     print("\n\n *** Running %r *** \n" % __file__)
107     main()