rustdesk/res/lang.py

87 lines
2.0 KiB
Python
Raw Normal View History

2022-05-14 11:46:20 +08:00
#!/usr/bin/env python3
2022-07-26 01:06:34 +08:00
import os
import glob
import sys
import csv
2022-12-26 00:23:03 +08:00
def get_lang(lang):
out = {}
2022-12-27 22:55:54 +08:00
for ln in open('./src/lang/%s.rs'%lang, encoding='utf8'):
2022-05-14 11:46:20 +08:00
ln = ln.strip()
if ln.startswith('("'):
2022-07-26 01:06:34 +08:00
k, v = line_split(ln)
out[k] = v
2022-12-26 00:23:03 +08:00
return out
2022-05-14 11:46:20 +08:00
def line_split(line):
2022-12-26 00:23:03 +08:00
toks = line.split('", "')
2022-12-04 18:39:59 +08:00
if len(toks) != 2:
print(line)
assert(0)
2022-12-26 00:23:03 +08:00
k = toks[0][2:]
2022-05-14 11:46:20 +08:00
v = toks[1][:-3]
2022-07-26 01:06:34 +08:00
return k, v
2022-05-14 11:46:20 +08:00
2022-07-26 01:06:34 +08:00
def main():
if len(sys.argv) == 1:
expand()
elif sys.argv[1] == '1':
to_csv()
else:
to_rs(sys.argv[1])
def expand():
2022-12-26 00:23:03 +08:00
for fn in glob.glob('./src/lang/*'):
lang = os.path.basename(fn)[:-3]
2022-12-04 18:39:59 +08:00
if lang in ['en','cn']: continue
print(lang)
2022-05-14 11:46:20 +08:00
dict = get_lang(lang)
2022-12-27 22:55:54 +08:00
fw = open("./src/lang/%s.rs"%lang, "wt", encoding='utf8')
for line in open('./src/lang/cn.rs', encoding='utf8'):
2022-05-14 11:46:20 +08:00
line_strip = line.strip()
if line_strip.startswith('("'):
2022-07-26 01:06:34 +08:00
k, v = line_split(line_strip)
2022-05-14 11:46:20 +08:00
if k in dict:
# embraced with " to avoid empty v
2022-12-27 23:26:11 +08:00
line = line.replace('"%s"'%v, '"%s"'%dict[k])
2022-05-14 11:46:20 +08:00
else:
2022-07-26 01:06:34 +08:00
line = line.replace(v, "")
fw.write(line)
2022-05-14 11:46:20 +08:00
else:
2022-07-26 01:06:34 +08:00
fw.write(line)
2022-05-14 11:46:20 +08:00
fw.close()
2022-12-26 00:23:03 +08:00
2022-07-26 01:06:34 +08:00
def to_csv():
2022-12-26 00:23:03 +08:00
for fn in glob.glob('./src/lang/*.rs'):
lang = os.path.basename(fn)[:-3]
2022-12-27 22:55:54 +08:00
csvfile = open('./src/lang/%s.csv'%lang, "wt", encoding='utf8')
2022-07-26 01:06:34 +08:00
csvwriter = csv.writer(csvfile)
2022-12-27 22:55:54 +08:00
for line in open(fn, encoding='utf8'):
2022-07-26 01:06:34 +08:00
line_strip = line.strip()
if line_strip.startswith('("'):
k, v = line_split(line_strip)
csvwriter.writerow([k, v])
csvfile.close()
def to_rs(lang):
2022-12-27 22:55:54 +08:00
csvfile = open('%s.csv'%lang, "rt", encoding='utf8')
fw = open("./src/lang/%s.rs"%lang, "wt", encoding='utf8')
2022-07-26 01:06:34 +08:00
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()