summaryrefslogtreecommitdiff
path: root/lang/updatelang.py
blob: ce824c4f4258f1be569f9c3f91729cace818e847 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#! /usr/bin/env python2.7
# -*- coding: utf8 -*-
#
# Copyright (C) 2010-2015  Evol Online
# Author: Andrei Karas (4144)

import os
import re

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

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

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

def addStr(text, comment, fileName, lineNum, addNoC):
    allStrings.add(text)
    if comment[-1:] == "\n":
        comment = comment[:-1]
    if text not in strComments:
        strComments[text] = set()

    strComments[text].add("#. code: " + comment.strip() + "\n")
    strComments[text].add("#: " + fileName + ":" + str(lineNum) + "\n")
    if addNoC == True:
        strComments[text].add("#, no-c-format\n")

def collectScriptStrings(parentDir, relativeDir):
    global itemNamesByName
    files = os.listdir(parentDir) 
    for file1 in files:
        if file1[0] == ".":
            continue
        file2 = os.path.abspath(parentDir + os.path.sep + file1)
        relativeDir2 = relativeDir + os.path.sep + file1
        if not os.path.isfile(file2):
            collectScriptStrings(file2, relativeDir2)
        elif filt.search(file1):
            codeFile = relativeDir + os.path.sep + file1
            with open(file2, "r") as f:
                cnt = -1
                for line in f:
                    cnt = cnt + 1
                    m = strre1.findall(line)
                    if len(m) > 0:
                        for str in m:
                            addStr(str, line, codeFile, cnt, True)
                    m = strre4.findall(line)
                    if len(m) > 0:
                        for str in m:
                            addStr(str + "#0", line, codeFile, cnt, True)
                            addStr(str + "#1", line, codeFile, cnt, True)
                    m = strre2.search(line)
                    if m is not None and m.group("str")[0] != "#":
                        addStr(m.group("str"), line, codeFile, cnt, True)
                    m = strre3.findall(line)
                    if len(m) > 0:
                        for str in m:
                            if str.lower() in itemNamesByName:
                                addStr(itemNamesByName[str.lower()], line, codeFile, cnt, True)
                    m = strre5.findall(line)
                    if len(m) > 0:
                        for str in m:
                            if str.lower() in itemNamesByName:
                                addStr(itemNamesByName[str.lower()], line, codeFile, cnt, True)
                    m = strre6.findall(line)
                    if len(m) > 0:
                        for str in m:
                            addStr(str, line, codeFile, cnt, True)
                    m = strre7.findall(line)
                    if len(m) > 0:
                        for str in m:
                            addStr(str[0] + "#0", line, codeFile, cnt, True)
                            addStr(str[2] + "#1", line, codeFile, cnt, True)


def collectMessages(messagesDir):
    with open(messagesDir, "r") as r:
        cnt = -1
        for line in r:
            cnt = cnt + 1
            if line[:2] == "//":
                continue
            idx = line.find(": ")
            if idx < 1:
                continue
            msgId = line[:idx]
            msgStr = line[idx + 2:]
            if msgStr[-1:] == "\n":
                msgStr = msgStr[:-1]
            addStr(msgStr, line, "conf/messages.conf", cnt, False)

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

    for file in langs:
        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-2015  Evol Online\n"
    return (trans, firstLine)


def loadPoFiles(podir):
    files = os.listdir(podir)
    for name in files:
        if name[-3:] == ".po":
            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 and line2 != "":
                            langFile[line1] = line2.replace("\\\"", "\"");
                    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]
            oldFile = oldLangFiles[trans][0]
            if str not in newFile:
#                print "lang: " + trans + ", str: " + str
                if trans == defaultLang:
#                    print "newFile[str] = str"
                    newFile[str] = str
                elif str in oldFile:
                    newFile[str] = oldFile[str]
#                    print "newFile[str] = oldFile[str]: " + newFile[str]
                else:
#                    print "newFile[str] = """
                    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(langDir, poDir):
    for trans in langFiles:
        writeLangFile (langDir + "/lang_" + trans + ".txt", langFiles[trans], trans, False)
        writeLangFile (langDir + "/lang_" + trans + ".old", oldLangFiles[trans], trans, True)
        writePoFile (poDir, langFiles[trans], trans)

def writeLangFile(langDir, texts, trans, isold):
    with open (langDir, "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")
                trLine = line[1]
                if trans == "en":
                    if len(trLine) > 2 and (trLine[-2:] == "#0" or trLine[-2:] == "#1"):
                        trLine = trLine[:-2]
                f.write (trLine + "\n\n")

def writePoComments(w, comments):
    for line in comments:
        if line[:3] == "#. ":
            w.write (line)
    for line in comments:
        if line[:3] == "#: ":
            w.write (line)

    for line in comments:
        if line[:3] == "#, ":
            w.write (line)
            break

def writePoFile(poDir, texts, trans):
    if trans == "en":
        langDir = poDir + "/" + trans + ".pot"
    else:
        langDir = poDir + "/" + trans + ".po"

    print langDir
    with open (langDir, "w") as w:
        w.write ("# " + texts[1] + "")
        w.write ("#\n\n")
        w.write ("msgid \"\"\n")
        w.write ("msgstr \"\"\n")
        w.write ("\"Project-Id-Version: EvolOnline\\n\"\n")
        w.write ("\"MIME-Version: 1.0\\n\"\n")
        w.write ("\"Content-Type: text/plain; charset=UTF-8\\n\"\n")
        w.write ("\"Content-Transfer-Encoding: 8bit\\n\"\n")
        w.write ("\n")
        for line in texts[0]:
            if line[0] in strComments.keys():
                writePoComments(w, strComments[line[0]])

            w.write ("msgid \"" + line[0].replace("\"", "\\\"") + "\"\n")
            trLine = line[1]
            if trans == "en":
                if len(trLine) > 2 and (trLine[-2:] == "#0" or trLine[-2:] == "#1"):
                    trLine = trLine[:-2]
            trLine = trLine.replace("\"", "\\\"")
            w.write ("msgstr \"" + trLine + "\"\n\n")

def stripQuotes(data):
    if len(data) == 0:
        return data
    if data[-1] == "\"":
        data = data[:-1]
    if data[0] == "\"":
        data = data[1:]
    if data[-1] == "'":
        data = data[:-1]
    if data[0] == "'":
         data = data[1:]
    return data

def loadItemDb(dir):
    global itemNamesByName
    with open(dir + "/item_db.conf", "r") as f:
        for line in f:
            line = line.strip()
            if line == "{":
                itemId = ""
                itemName = ""
                for line in f:
                    line = line.strip()
                    if len(line) < 1:
                        continue
                    if line[0] == "}":
                        if itemId != "" and itemName != "":
                            itemNamesByName[itemName.lower()] = itemName
                        break
                    if len(line) > 6 and line[:5] == "Name:":
                        itemName = stripQuotes(line[5:].strip())
                    if len(line) > 4 and line[:3] == "Id:":
                        itemId = stripQuotes(line[3:].strip())

def dumpTranslations():
    for trans in oldLangFiles:
        print "old lang: " + trans
        newFile = oldLangFiles[trans][0]
        for line in newFile:
            print line
            if line in newFile:
                print newFile[line]
            print "\n"
    for trans in langFiles:
        print "new lang: " + trans
        newFile = langFiles[trans][0]
        for line in newFile:
            print line
            if line in newFile:
                print newFile[line]
            print "\n"


def loadMobNames(dir):
    with open(dir + "/mob_db.conf", "r") as r:
        cnt = -1
        for line in r:
            cnt = cnt + 1
            if len(line) < 1 or line[0:2] == "//":
                continue
            line = line.strip()
            idx = line.find("Name: \"")
            if idx != 0 or line[len(line) - 1] != "\"":
                continue
            addStr(line[idx + 7: len(line) - 1], line, "mob_db.txt", cnt, True)

rootPath = "../../server-data/"

loadItemDb(rootPath + "db/re")
loadMobNames(rootPath + "db/re")
collectScriptStrings(rootPath + "/npc", "npc")
collectMessages(rootPath + "/conf/messages.conf")
loadFiles(rootPath + "/langs")
addMissingLines()
loadPoFiles("in");
#dumpTranslations();
sorting()
saveFiles(rootPath + "/langs", "out")