Skip to content

Instantly share code, notes, and snippets.

@ezdiy
Created May 4, 2018 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezdiy/d8ff0582ad2cacbbc48b9b47104ed0b9 to your computer and use it in GitHub Desktop.
Save ezdiy/d8ff0582ad2cacbbc48b9b47104ed0b9 to your computer and use it in GitHub Desktop.
from glob import glob
from os.path import basename
import codecs
import subprocess
import pickle
import sys
seen = set()
tr = subprocess.Popen(["./trans","-s","japanese","-no-auto","-brief"],stdout=subprocess.PIPE,stdin=subprocess.PIPE,bufsize=1)
transcache = pickle.load(open("transcache.p","rb"))
sys.stdout.write((u"\ufeff").encode('utf-8'))
def do_trans(l):
if l.strip() == "":
return l
if l in transcache:
return transcache[l]
ol = l
l=l.encode('utf-8') + "\r\n"
tr.stdin.write(l)
res=tr.stdout.readline().strip().decode('utf-8').replace('"',"'")
transcache[ol] = res
pickle.dump(transcache, open("transcache.p","wb"))
return res
def trans(l):
if l.strip()=="":
return ""
parts = []
# has a tag in it
while '[' in l:
p, l = l.split('[',1)
parts.append(do_trans(p) + '[')
p, l = l.split(']',1)
parts.append(p + ']')
parts.append(do_trans(l))
return "".join(parts)
def pp(x):
print(x.encode('utf-8'))
def quotes(l):
return '"' + l.replace('"','""') + '"'
def body_flush(body):
if not len(body[1]): return
parts = []
for ln in body[1]:
parts.append(trans(ln))
pp('"'+body[0]+'",'+quotes("\n".join(body[1]).rstrip()) + "," + quotes("\n".join(parts)))
body[0] = None
body[1] = []
def process_file(base,path):
f = codecs.open(path, encoding='utf-16')
special = [';', '@', '*','[','/']
body = [None,[]]
lineno = 0
inscript = False
for tl in f:
tl = tl.rstrip('\n').rstrip('\r')
l=tl.strip()
goodtag = l.startswith('[r]') or l.startswith('[font')
lineno+=1
if body[0]:
if not goodtag and (len(l) and (l[0] in special)):
body_flush(body)
continue
body[1].append(tl)
else:
if l == "@iscript" or l == '[iscript]':
inscript = True
if l == "@endscript" or l == '[endscript]':
inscript = False
continue
if inscript:
continue
if not len(l): continue
if not goodtag and (l[0] in special):
continue
body[0] = "%s:%d" % (base,lineno)
body[1] = []
body[1].append(tl)
body_flush(body)
for d in glob("data/patch/scenario/*.ks") + glob("data/scenario/*.ks") + glob("data/map/*.ks"):
bn = basename(d)
if bn in seen: continue
seen.add(bn)
process_file(bn,d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment