summaryrefslogtreecommitdiff
path: root/lang/updatelang.py
blob: d1e9bc60cf3f3cb087570cf98f4449d824231389 (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#! /usr/bin/env python2.6
# -*- coding: utf8 -*-
#
# Copyright (C) 2010-2011  Evol Online
# Author: Andrei Karas (4144)

import os
import re
#from sets import Set

defaultLang = "en"
filt = re.compile(".+[.]txt", re.IGNORECASE)

allStrings = set()
strre1 = re.compile("[\t +(]l[(][\"](?P<str>[^\"]+)[\"]")
strre3 = re.compile("[\t +(]getitemlink[(][\"](?P<str>[^\"]+)[\"][)]")
strre2 =  re.compile("^[^/](.+)[.]gat([^\t]+)[\t](script|shop)[\t](?P<str>[\w ]+)[\t]([\d]+),")
strre4 = re.compile("[\t +(]lg[(][\"](?P<str>[^\"]+)[\"]")
strre5 = re.compile("[\t +(]getitemname[(][\"](?P<str>[^\"]+)[\"][)]")
strre6 = re.compile("[\t ]mesn[ ][\"](?P<str>[^\"]+)[\"]")
itemsplit = re.compile(",")

langFiles = dict() 
oldLangFiles = dict()
langs = set()
itemNamesByName = dict()

def collectStrings(parentDir):
	global itemNamesByName
	files = os.listdir(parentDir) 
	for file1 in files:
		if file1[0] == ".":
			continue
		file2 = os.path.abspath(parentDir + os.path.sep + file1)
		if not os.path.isfile(file2):
			collectStrings(file2)
		elif filt.search(file1):
			with open(file2, "r") as f:
				for line in f:
					m = strre1.findall(line)
					if len(m) > 0:
						for str in m:
							allStrings.add(str)
					m = strre4.findall(line)
					if len(m) > 0:
						for str in m:
							allStrings.add(str + "#0")
							allStrings.add(str + "#1")
					m = strre2.search(line)
					if m is not None:
						 allStrings.add(m.group("str"))
					m = strre3.findall(line)
					if len(m) > 0:
						for str in m:
							if str.lower() in itemNamesByName:
								allStrings.add(itemNamesByName[str.lower()])
					m = strre5.findall(line)
					if len(m) > 0:
						for str in m:
							if str.lower() in itemNamesByName:
								allStrings.add(itemNamesByName[str.lower()])
					m = strre6.findall(line)
					if len(m) > 0:
						for str in m:
							allStrings.add(str)




def loadFiles(dir):
	with open(dir + "/langs.txt", "r") as f:
		for line in f:
			langs.add(line[:-1])

	for file in langs:
		#print dir + "/lang_" + file + ".txt"
		langFiles[file] = parseFile(dir + "/lang_" + file + ".txt", True)
		oldLangFiles[file] = parseFile(dir + "/lang_" + file + ".old", False)

def parseFile(name, readFirstLine):
	trans = dict()
	firstLine = None
	if os.path.exists(name):
		with open(name, "r") as f:
			line1 = "";
			line2 = "";
			for line in f:
				if readFirstLine is True and firstLine is None:
					firstLine = line
					continue

				if (line == ""):
					line1 = ""
					line2 = ""
					continue
				elif (line1 == ""):
					line1 = line[:-1]
					continue

				line2 = line[:-1]
				trans[line1] = line2
				line1 = ""
				line2 = ""
	elif readFirstLine:
		firstLine = "Copyright (C) 2010-2012  Evol Online\n"
	return (trans, firstLine)


def loadPoFiles(podir):
    files = os.listdir(podir)
    for name in files:
	    parsePoFile(name[:-3], podir + "/" + name)


def parsePoFile(name, path):
	langFile = langFiles[name][0]
	with open(path, "r") as f:
		flag = 0
		line1 = ""
		line2 = ""
		for line in f:
			if flag == 0:
				idx = line.find ("msgid ")
				if idx == 0:
					line2 = ""
					line1 = line[len("msgid "):]
					line1 = line1[1:len(line1) - 2]
					flag = 1
			elif flag == 1:
				idx = line.find ("msgstr ")
				if idx == 0:
					line2 = line[len("msgstr "):]
					line2 = line2[1:len(line2) - 2]
					flag = 2
			if line == "\n":
				if flag == 2:
					if line1 != "":
						if line1 in langFile:
							langFile[line1] = line2
					flag = 0

			idx = line.find ("\"")
			if idx == 0:
				line = line[1:len(line) - 2]
				if flag == 1:
					line1 = line1 + line
				elif flag == 2:
					line2 = line2 + line



def addMissingLines():
	for trans in langFiles:
		newFile = langFiles[trans][0]
		oldFile = oldLangFiles[trans][0]
		for str in newFile:
			if str not in allStrings:
#				if newFile[str] != "":
				oldFile[str] = newFile[str]
		for str in oldFile:
			if str in newFile:
				del newFile[str]
				print trans + ":moved to old: " + str

	for str in allStrings:
		for trans in langFiles:
			newFile = langFiles[trans][0]
			if str not in newFile:
				if trans == defaultLang:
					newFile[str] = str
				else:
					newFile[str] = ""
				print trans + ":new string: " + str


def sorting():
	for trans in langFiles:
		newFile = langFiles[trans]
		newFile = sortDict (newFile)
		langFiles[trans] = newFile
		oldFile = oldLangFiles[trans]
		oldFile = sortDict (oldFile)
		oldLangFiles[trans] = oldFile

def sortDict(adict):
	d2 = [] 
	keys = adict[0].keys()
	keys.sort()

	for key in keys:
		d2.append ((key, adict[0][key]))
	return (d2, adict[1])

def saveFiles(dir):
	for trans in langFiles:
		writeFile (dir + "/lang_" + trans + ".txt", langFiles[trans], False)
		writeFile (dir + "/lang_" + trans + ".old", oldLangFiles[trans], True)

def writeFile(dir, texts, isold):
	with open (dir, "w") as f:
		if texts[1] is not None:
			f.write(texts[1])
		for line in texts[0]:
			if not isold or (line[1] is not None and len(line[1]) > 0):
				f.write (line[0] + "\n")
				f.write (line[1] + "\n\n")


def loadItemDb(dir):
	global itemNamesByName
	with open(dir + "/item_db.txt", "r") as f:
		for line in f:
			rows = itemsplit.split(line)
			if len(rows) < 5:
				continue
			itemNamesByName[rows[1].lower().strip()] = rows[2].strip()

rootPath = "../../gittorious/serverdata-beta"

loadItemDb(rootPath + "/db")
collectStrings(rootPath + "/npc")
loadFiles(rootPath + "/langs")
addMissingLines()
loadPoFiles("new");
sorting()
saveFiles(rootPath + "/langs")