616443edc4a267b93ff6b1064edfc599a9df72b9
[blender-staging.git] / po / update_pot.py
1 #!/usr/bin/python
2
3 # update the pot file according the POTFILES.in
4
5 import os
6
7 GETTEXT_XGETTEXT_EXECUTABLE="xgettext"
8 SOURCE_DIR=".."
9 DOMAIN="blender"
10
11 cmd = "%s --files-from=%s/po/POTFILES.in --keyword=_ --keyword=N_ --directory=%s --output=%s/po/%s.pot --from-code=utf-8" % (
12     GETTEXT_XGETTEXT_EXECUTABLE, SOURCE_DIR, SOURCE_DIR, SOURCE_DIR, DOMAIN)
13
14 os.system( cmd )
15
16 def stripeol(s):
17     if line.endswith("\n"):
18         s = s[:-1]
19
20     if line.endswith("\r"):
21         s = s[:-1]
22
23     return s
24
25 pot_messages = {}
26 reading_message = False
27 message = ""
28 with open("blender.pot", 'r') as handle:
29     while True:
30         line = handle.readline()
31
32         if not line:
33             break
34
35         line = stripeol(line)
36         if line.startswith("msgid"):
37             reading_message = True
38             message = line[7:-1]
39         elif line.startswith("msgstr"):
40             reading_message = False
41             pot_messages[message] = True
42         elif reading_message:
43             message += line[1:-1]
44
45 # add messages collected automatically from RNA
46 with open("blender.pot", "a") as pot_handle:
47     with open("messages.txt", 'r') as handle:
48         while True:
49             line = handle.readline()
50
51             if not line:
52                 break
53
54             line = stripeol(line)
55             line = line.replace("\\", "\\\\")
56             line = line.replace("\"", "\\\"")
57
58             if not pot_messages.get(line):
59                 pot_handle.write("\n#: Automatically collected from RNA\n")
60                 pot_handle.write("msgid \"%s\"\n" % (line))
61                 pot_handle.write("msgstr \"\"\n")