From 5756751493ac58a676195112efc4794cca97275a Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Fri, 22 Apr 2011 19:24:06 +0300 Subject: Add ability to convert language files to and from po files. --- lang/converttopo.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ lang/new/.placeholder | 0 lang/po/.gitignore | 1 + lang/updatelang.py | 44 ++++++++++++++++++++++++++ 4 files changed, 133 insertions(+) create mode 100755 lang/converttopo.py create mode 100644 lang/new/.placeholder create mode 100644 lang/po/.gitignore (limited to 'lang') diff --git a/lang/converttopo.py b/lang/converttopo.py new file mode 100755 index 0000000..dc746bd --- /dev/null +++ b/lang/converttopo.py @@ -0,0 +1,88 @@ +#! /usr/bin/env python2.6 +# -*- coding: utf8 -*- +# +# Copyright (C) 2011 Evol Online +# Author: Andrei Karas (4144) + +import os +import re + +defaultLang = "en" +filt = re.compile(".+[.]txt", re.IGNORECASE) + +allStrings = set() +strre1 = re.compile("[\t +(]l[(][\"](?P[^\"]+)[\"]") +strre3 = re.compile("[\t +(]getitemlink[(][\"](?P[^\"]+)[\"][)]") +strre2 = re.compile("^[^/](.+)[.]gat([^\t]+)[\t](script|shop)[\t](?P[\w ]+)[\t]([\d]+),") + +langFiles = dict() +oldLangFiles = dict() +langs = set() +itemNamesByName = dict() + + +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) + +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 = "" + return (trans, firstLine) + + +def saveFiles(name): + for filen in langFiles: + saveFile(name, filen) + + +def saveFile(path, name): + print str(name) + with open (path + "/" + name + ".po", "w") as w: + lang = langFiles[name] + w.write ("# " + lang[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 lang[0].keys(): + line2 = line.replace("\"", "\\\"") + w.write ("msgid \"" + line2 + "\"\n") + line2 = lang[0][line].replace("\"", "\\\"") + w.write ("msgstr \"" + line2 + "\"\n\n") + + +rootPath = "../../gittorious/serverdata" + +loadFiles(rootPath + "/langs") +saveFiles("po") + diff --git a/lang/new/.placeholder b/lang/new/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/lang/po/.gitignore b/lang/po/.gitignore new file mode 100644 index 0000000..6d609ce --- /dev/null +++ b/lang/po/.gitignore @@ -0,0 +1 @@ +*.po diff --git a/lang/updatelang.py b/lang/updatelang.py index 41a96eb..a0ccda4 100755 --- a/lang/updatelang.py +++ b/lang/updatelang.py @@ -84,6 +84,49 @@ def parseFile(name, readFirstLine): 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] @@ -156,5 +199,6 @@ loadItemDb(rootPath + "/db") collectStrings(rootPath + "/npc") loadFiles(rootPath + "/langs") addMissingLines() +loadPoFiles("new"); sorting() saveFiles(rootPath + "/langs") -- cgit v1.2.3-70-g09d2