techage_modpack/minecart/i18n.py

80 lines
2.3 KiB
Python
Raw Normal View History

2020-05-31 23:31:18 +03:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
2020-06-19 00:23:53 +03: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.
2020-05-31 23:31:18 +03:00
2020-06-19 00:23:53 +03:00
from __future__ import print_function
import os, fnmatch, re, shutil
2020-05-31 23:31:18 +03:00
2020-06-19 00:23:53 +03:00
pattern_lua = re.compile(r'[ \.=^\t]S\("(.+?)"\)', re.DOTALL)
pattern_tr = re.compile(r'(.+?[^@])=(.+)')
2020-05-31 23:31:18 +03:00
def gen_template(templ_file, lkeyStrings):
lOut = []
lkeyStrings.sort()
for s in lkeyStrings:
lOut.append("%s=" % s)
2020-06-19 00:23:53 +03:00
open(templ_file, "wt").write("\n".join(lOut))
2020-05-31 23:31:18 +03:00
2020-06-19 00:23:53 +03:00
def read_lua_file_strings(lua_file):
2020-05-31 23:31:18 +03:00
lOut = []
2020-06-19 00:23:53 +03:00
text = open(lua_file).read()
for s in pattern_lua.findall(text):
s = re.sub(r'"\.\.\s+"', "", s)
s = re.sub("@[^@=n]", "@@", s)
s = s.replace("\n", "@n")
s = s.replace("\\n", "@n")
s = s.replace("=", "@=")
2020-05-31 23:31:18 +03:00
lOut.append(s)
return lOut
2020-06-19 00:23:53 +03:00
def inport_tr_file(tr_file):
dOut = {}
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)
return dOut
def generate_template(templ_file):
2020-05-31 23:31:18 +03:00
lOut = []
for root, dirs, files in os.walk('./'):
for name in files:
if fnmatch.fnmatch(name, "*.lua"):
fname = os.path.join(root, name)
2020-06-19 00:23:53 +03:00
found = read_lua_file_strings(fname)
print(fname, len(found))
lOut.extend(found)
lOut = list(set(lOut))
lOut.sort()
2020-05-31 23:31:18 +03:00
gen_template(templ_file, lOut)
2020-06-19 00:23:53 +03:00
return lOut
def update_tr_file(lNew, mod_name, tr_file):
lOut = ["# textdomain: %s\n" % mod_name]
if os.path.exists(tr_file):
shutil.copyfile(tr_file, tr_file+".old")
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]))
open(tr_file, "w").write("\n".join(lOut))
2020-05-31 23:31:18 +03:00
2020-06-19 00:23:53 +03:00
data = generate_template("./locale/template.txt")
update_tr_file(data, "minecart", "./locale/minecart.de.tr")
print("Done.\n")