added update for Bosnian translation by ***
[twcon.git] / scripts / update_localization.py
blob08c1866e2b08459993e6c785f4588f5800552b60
1 import os, re, sys
2 match = re.search('(.*)/', sys.argv[0])
3 if match != None:
4 os.chdir(match.group(1))
5 os.chdir('../')
7 source_exts = [".c", ".cpp", ".h"]
9 def parse_source():
10 stringtable = {}
11 def process_line(line):
12 if 'Localize("'.encode() in line:
13 fields = line.split('Localize("'.encode(), 1)[1].split('"'.encode(), 1)
14 stringtable[fields[0]] = ""
15 process_line(fields[1])
17 for root, dirs, files in os.walk("src"):
18 for name in files:
19 filename = os.path.join(root, name)
21 if os.sep + "external" + os.sep in filename:
22 continue
24 if filename[-2:] in source_exts or filename[-4:] in source_exts:
25 for line in open(filename, "rb"):
26 process_line(line)
28 return stringtable
30 def load_languagefile(filename):
31 f = open(filename, "rb")
32 lines = f.readlines()
33 f.close()
35 stringtable = {}
37 for i in range(0, len(lines)-1):
38 l = lines[i].strip()
39 if len(l) and not l[0:1] == "=".encode() and not l[0:1] == "#".encode():
40 stringtable[l] = lines[i+1][3:].rstrip()
42 return stringtable
44 def generate_languagefile(outputfilename, srctable, loctable):
45 f = open(outputfilename, "wb")
47 num_items = 0
48 new_items = 0
49 old_items = 0
51 srctable_keys = []
52 for key in srctable:
53 srctable_keys.append(key)
54 srctable_keys.sort()
56 content = "\n##### translated strings #####\n\n".encode()
57 for k in srctable_keys:
58 if k in loctable and len(loctable[k]):
59 content += k + "\n== ".encode() + loctable[k] + "\n\n".encode()
60 num_items += 1
62 content += "##### needs translation #####\n\n".encode()
63 for k in srctable_keys:
64 if not k in loctable or len(loctable[k]) == 0:
65 content += k + "\n== \n\n".encode()
66 num_items += 1
67 new_items += 1
69 content += "##### old translations #####\n\n".encode()
70 for k in loctable:
71 if not k in srctable:
72 content += k + "\n== ".encode() + loctable[k] + "\n\n".encode()
73 num_items += 1
74 old_items += 1
76 f.write(content)
77 f.close()
78 print("%-40s %8d %8d %8d" % (outputfilename, num_items, new_items, old_items))
80 srctable = parse_source()
82 print("%-40s %8s %8s %8s" % ("filename", "total", "new", "old"))
84 for filename in os.listdir("data/languages"):
85 if not ".txt" in filename:
86 continue
87 if filename == "index.txt":
88 continue
90 filename = "data/languages/" + filename
91 generate_languagefile(filename, srctable, load_languagefile(filename))