about summary refs log tree commit diff
path: root/scripts/update_localization.py
blob: 087f55a4fca8848c1ab7f3db91543dfc7cf187c7 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import sys, os

source_exts = [".c", ".cpp", ".h", ".hpp"]

def parse_source():
	stringtable = {}
	def process_line(line):
		if 'localize("' in line:
			fields = line.split('localize("', 2)[1].split('"', 2)
			stringtable[fields[0]] = ""
			process_line(fields[1])

	for root, dirs, files in os.walk("src"):
		for name in files:
			filename = os.path.join(root, name)
			
			if os.sep + "external" + os.sep in filename:
				continue
			
			if filename[-2:] in source_exts or filename[-4:] in source_exts:
				for line in file(filename):
					process_line(line)
	
	return stringtable

def load_languagefile(filename):
	f = file(filename)
	lines = f.readlines()
	f.close()
	stringtable = {}

	for i in xrange(0, len(lines)-1):
		l = lines[i].strip()
		if len(l) and not l[0] == '=' and not l[0] == '#':
			stringtable[l] = lines[i+1][3:].strip()
			
	return stringtable


def generate_languagefile(outputfilename, srctable, loctable):
	f = file(outputfilename, "w")

	num_items = 0
	new_items = 0
	old_items = 0

	print >>f, ""
	print >>f, "##### translated strings #####"
	print >>f, ""
	for k in srctable:
		if k in loctable and len(loctable[k]):
			print >>f, k
			print >>f, "==", loctable[k]
			print >>f, ""
			num_items += 1


	print >>f, "##### needs translation ####"
	print >>f,  ""
	for k in srctable:
		if not k in loctable or len(loctable[k]) == 0:
			print >>f, k
			print >>f, "==", srctable[k]
			print >>f, ""
			num_items += 1
			new_items += 1

	print >>f, "##### old translations ####"
	print >>f, ""
	for k in loctable:
		if not k in srctable:
			print >>f, k
			print >>f, "==", loctable[k]
			print >>f, ""
			num_items += 1
			old_items += 1

	print "%-40s %8s %8s %8s" % ("filename", "total", "new", "old")
	print "%-40s %8d %8d %8d" % (outputfilename, num_items, new_items, old_items)
	f.close()

srctable = parse_source()

for filename in os.listdir("data/languages"):
	if not ".txt" in filename:
		continue
		
	filename = "data/languages/" + filename
	generate_languagefile(filename, srctable, load_languagefile(filename))