2019-07-02 12:33:12 -07:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-09-26 14:44:34 -07:00
|
|
|
# Script to generate the template file and update the translation files.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2019 Joachim Stolberg
|
|
|
|
# LGPLv2.1+
|
|
|
|
#
|
|
|
|
# Copy the script into the mod root folder and adapt the last code lines to you needs.
|
2019-07-02 12:33:12 -07:00
|
|
|
|
2019-09-29 14:37:35 -07:00
|
|
|
from __future__ import print_function
|
2019-09-26 13:32:37 -07:00
|
|
|
import os, fnmatch, re, shutil
|
2019-07-02 12:33:12 -07:00
|
|
|
|
2019-09-26 13:32:37 -07:00
|
|
|
pattern_lua = re.compile(r'[ \.=^\t]S\("(.+?)"\)', re.DOTALL)
|
|
|
|
pattern_tr = re.compile(r'(.+?[^@])=(.+)')
|
2019-07-02 12:33:12 -07:00
|
|
|
|
|
|
|
def gen_template(templ_file, lkeyStrings):
|
|
|
|
lOut = []
|
|
|
|
lkeyStrings.sort()
|
|
|
|
for s in lkeyStrings:
|
|
|
|
lOut.append("%s=" % s)
|
2019-09-26 14:44:34 -07:00
|
|
|
open(templ_file, "wt").write("\n".join(lOut))
|
2019-07-02 12:33:12 -07:00
|
|
|
|
2019-09-26 13:32:37 -07:00
|
|
|
def read_lua_file_strings(lua_file):
|
2019-07-02 12:33:12 -07:00
|
|
|
lOut = []
|
2019-09-26 14:44:34 -07:00
|
|
|
text = open(lua_file).read()
|
2019-09-26 13:32:37 -07:00
|
|
|
for s in pattern_lua.findall(text):
|
2019-07-02 12:33:12 -07:00
|
|
|
s = re.sub(r'"\.\.\s+"', "", s)
|
2019-09-26 13:32:37 -07:00
|
|
|
s = re.sub("@[^@=n]", "@@", s)
|
|
|
|
s = s.replace("\n", "@n")
|
|
|
|
s = s.replace("\\n", "@n")
|
|
|
|
s = s.replace("=", "@=")
|
2019-07-02 12:33:12 -07:00
|
|
|
lOut.append(s)
|
|
|
|
return lOut
|
|
|
|
|
2019-09-26 13:32:37 -07:00
|
|
|
def inport_tr_file(tr_file):
|
|
|
|
dOut = {}
|
2019-09-28 10:47:06 -07:00
|
|
|
if os.path.exists(tr_file):
|
|
|
|
for line in open(tr_file, "r").readlines():
|
|
|
|
s = line.strip()
|
|
|
|
if s == "" or s[0] == "#":
|
|
|
|
continue
|
|
|
|
match = pattern_tr.match(s)
|
|
|
|
if match:
|
|
|
|
dOut[match.group(1)] = match.group(2)
|
2019-09-26 13:32:37 -07:00
|
|
|
return dOut
|
|
|
|
|
|
|
|
def generate_template(templ_file):
|
2019-07-02 12:33:12 -07:00
|
|
|
lOut = []
|
|
|
|
for root, dirs, files in os.walk('./'):
|
|
|
|
for name in files:
|
|
|
|
if fnmatch.fnmatch(name, "*.lua"):
|
|
|
|
fname = os.path.join(root, name)
|
2019-09-26 13:32:37 -07:00
|
|
|
found = read_lua_file_strings(fname)
|
2019-09-26 14:44:34 -07:00
|
|
|
print(fname, len(found))
|
2019-07-02 12:33:12 -07:00
|
|
|
lOut.extend(found)
|
2019-09-26 13:32:37 -07:00
|
|
|
lOut = list(set(lOut))
|
|
|
|
lOut.sort()
|
2019-07-02 12:33:12 -07:00
|
|
|
gen_template(templ_file, lOut)
|
2019-09-26 13:32:37 -07:00
|
|
|
return lOut
|
|
|
|
|
|
|
|
def update_tr_file(lNew, mod_name, tr_file):
|
|
|
|
lOut = ["# textdomain: %s\n" % mod_name]
|
2019-09-28 10:47:06 -07:00
|
|
|
if os.path.exists(tr_file):
|
|
|
|
shutil.copyfile(tr_file, tr_file+".old")
|
2019-09-26 13:32:37 -07:00
|
|
|
dOld = inport_tr_file(tr_file)
|
|
|
|
for key in lNew:
|
|
|
|
val = dOld.get(key, "")
|
|
|
|
lOut.append("%s=%s" % (key, val))
|
|
|
|
lOut.append("##### not used anymore #####")
|
|
|
|
for key in dOld:
|
|
|
|
if key not in lNew:
|
|
|
|
lOut.append("%s=%s" % (key, dOld[key]))
|
2019-09-26 14:44:34 -07:00
|
|
|
open(tr_file, "w").write("\n".join(lOut))
|
2019-07-02 12:33:12 -07:00
|
|
|
|
2019-09-26 13:32:37 -07:00
|
|
|
data = generate_template("./locale/template.txt")
|
2019-09-26 14:44:34 -07:00
|
|
|
#update_tr_file(data, "mymod", "./locale/mymod.de.tr")
|
|
|
|
#update_tr_file(data, "mymod", "./locale/mymod.fr.tr")
|
2019-09-28 10:47:06 -07:00
|
|
|
update_tr_file(data, "techage", "./locale/techage.de.tr")
|
|
|
|
update_tr_file(data, "techage", "./locale/techage.fr.tr")
|
2019-09-26 14:44:34 -07:00
|
|
|
print("Done.\n")
|