csv <-> rs translator
This commit is contained in:
parent
435ff56e6c
commit
0a420dd04e
74
lang.py
74
lang.py
@ -2,16 +2,18 @@
|
|||||||
|
|
||||||
# Based on 'cn.rs', generate entries that are not completed in other languages
|
# Based on 'cn.rs', generate entries that are not completed in other languages
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import glob
|
import glob
|
||||||
|
import sys
|
||||||
|
import csv
|
||||||
|
|
||||||
def get_lang(lang):
|
def get_lang(lang):
|
||||||
out = {}
|
out = {}
|
||||||
for ln in open('./src/lang/%s.rs'%lang):
|
for ln in open('./src/lang/%s.rs'%lang):
|
||||||
ln = ln.strip()
|
ln = ln.strip()
|
||||||
if ln.startswith('("'):
|
if ln.startswith('("'):
|
||||||
k,v = line_split(ln)
|
k, v = line_split(ln)
|
||||||
out[k] = v
|
out[k] = v
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def line_split(line):
|
def line_split(line):
|
||||||
@ -19,28 +21,64 @@ def line_split(line):
|
|||||||
assert(len(toks) == 2)
|
assert(len(toks) == 2)
|
||||||
k = toks[0][2:]
|
k = toks[0][2:]
|
||||||
v = toks[1][:-3]
|
v = toks[1][:-3]
|
||||||
return k,v
|
return k, v
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
if len(sys.argv) == 1:
|
||||||
|
expand()
|
||||||
|
elif sys.argv[1] == '1':
|
||||||
|
to_csv()
|
||||||
|
else:
|
||||||
|
to_rs(sys.argv[1])
|
||||||
|
|
||||||
|
|
||||||
|
def expand():
|
||||||
for fn in glob.glob('./src/lang/*'):
|
for fn in glob.glob('./src/lang/*'):
|
||||||
lang = os.path.basename(fn)[:-3]
|
lang = os.path.basename(fn)[:-3]
|
||||||
if lang in ['en','cn']: continue
|
if lang in ['en','cn']: continue
|
||||||
fw = open("%s.rs.gen"%lang, "wb+")
|
|
||||||
dict = get_lang(lang)
|
dict = get_lang(lang)
|
||||||
|
fw = open("%s.rs"%lang, "wt")
|
||||||
for line in open('./src/lang/cn.rs'):
|
for line in open('./src/lang/cn.rs'):
|
||||||
line_strip = line.strip()
|
line_strip = line.strip()
|
||||||
if line_strip.startswith('("'):
|
if line_strip.startswith('("'):
|
||||||
k,v = line_split(line_strip)
|
k, v = line_split(line_strip)
|
||||||
if k in dict:
|
if k in dict:
|
||||||
line = line.replace(v, dict[k])
|
line = line.replace(v, dict[k])
|
||||||
else:
|
else:
|
||||||
line = line.replace(v, "")
|
line = line.replace(v, "")
|
||||||
fw.write(line.encode())
|
fw.write(line)
|
||||||
else:
|
else:
|
||||||
fw.write(line.encode())
|
fw.write(line)
|
||||||
fw.close()
|
fw.close()
|
||||||
os.remove("./src/lang/%s.rs"%lang)
|
|
||||||
os.rename(fw.name, "./src/lang/%s.rs"%lang)
|
|
||||||
|
def to_csv():
|
||||||
main()
|
for fn in glob.glob('./src/lang/*.rs'):
|
||||||
|
lang = os.path.basename(fn)[:-3]
|
||||||
|
csvfile = open('./src/lang/%s.csv'%lang, "wt")
|
||||||
|
csvwriter = csv.writer(csvfile)
|
||||||
|
for line in open(fn):
|
||||||
|
line_strip = line.strip()
|
||||||
|
if line_strip.startswith('("'):
|
||||||
|
k, v = line_split(line_strip)
|
||||||
|
csvwriter.writerow([k, v])
|
||||||
|
csvfile.close()
|
||||||
|
|
||||||
|
|
||||||
|
def to_rs(lang):
|
||||||
|
csvfile = open('%s.csv'%lang, "rt")
|
||||||
|
fw = open("./src/lang/%s.rs"%lang, "wt")
|
||||||
|
fw.write('''lazy_static::lazy_static! {
|
||||||
|
pub static ref T: std::collections::HashMap<&'static str, &'static str> =
|
||||||
|
[
|
||||||
|
''')
|
||||||
|
for row in csv.reader(csvfile):
|
||||||
|
fw.write(' ("%s", "%s"),\n'%(row[0].replace('"', '\"'), row[1].replace('"', '\"')))
|
||||||
|
fw.write(''' ].iter().cloned().collect();
|
||||||
|
}
|
||||||
|
''')
|
||||||
|
fw.close()
|
||||||
|
|
||||||
|
|
||||||
|
main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user