-
Notifications
You must be signed in to change notification settings - Fork 18
/
extract_strings.py
45 lines (38 loc) · 1010 Bytes
/
extract_strings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import sys
import re
pattern = re.compile(r"(translate|_)\((.*?)\)")
lisp_template_begin = """(msgids
""";
lisp_template_end = ")"
lisp_content = lisp_template_begin
if(len(sys.argv) < 3):
print("Usage: " + sys.argv[0] + " <filename> <output file>")
else:
filename = sys.argv[1]
out_file = sys.argv[2]
strings_found = False
with open(filename, 'r') as f:
s = f.read()
matches = pattern.findall(s)
if len(matches) > 0:
strings_found = True
for match in matches:
lines = match[1].split("\\n")
num_lines = len(lines)
if num_lines == 1:
lisp_content += " (msgid "
lisp_content += "(_ " + match[1] + ")"
lisp_content += ")\r\n"
else:
lisp_content += " (msgid (_ "
for i, line in enumerate(lines):
lisp_content += line
if i < num_lines:
if i == num_lines - 1:
lisp_content += "))"
lisp_content += "\n"
lisp_content += lisp_template_end
if strings_found:
output_file = open(out_file, "w")
output_file.write(lisp_content)
output_file.close()