2022-05-14 06:46:20 +03:00
#!/usr/bin/env python3
2022-07-25 20:06:34 +03:00
import os
import glob
import sys
import csv
2022-05-14 06:46:20 +03:00
2022-07-25 20:06:34 +03:00
def get_lang ( lang ) :
2022-05-14 06:46:20 +03:00
out = { }
2022-07-25 20:06:34 +03:00
for ln in open ( ' ./src/lang/ %s .rs ' % lang ) :
2022-05-14 06:46:20 +03:00
ln = ln . strip ( )
if ln . startswith ( ' ( " ' ) :
2022-07-25 20:06:34 +03:00
k , v = line_split ( ln )
out [ k ] = v
2022-05-14 06:46:20 +03:00
return out
def line_split ( line ) :
toks = line . split ( ' " , " ' )
assert ( len ( toks ) == 2 )
k = toks [ 0 ] [ 2 : ]
v = toks [ 1 ] [ : - 3 ]
2022-07-25 20:06:34 +03:00
return k , v
2022-05-14 06:46:20 +03:00
2022-07-25 20:06:34 +03: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-05-14 06:46:20 +03:00
for fn in glob . glob ( ' ./src/lang/* ' ) :
lang = os . path . basename ( fn ) [ : - 3 ]
if lang in [ ' en ' , ' cn ' ] : continue
dict = get_lang ( lang )
2022-07-25 20:06:34 +03:00
fw = open ( " %s .rs " % lang , " wt " )
2022-05-14 06:46:20 +03:00
for line in open ( ' ./src/lang/cn.rs ' ) :
line_strip = line . strip ( )
if line_strip . startswith ( ' ( " ' ) :
2022-07-25 20:06:34 +03:00
k , v = line_split ( line_strip )
2022-05-14 06:46:20 +03:00
if k in dict :
2022-07-25 20:06:34 +03:00
line = line . replace ( v , dict [ k ] )
2022-05-14 06:46:20 +03:00
else :
2022-07-25 20:06:34 +03:00
line = line . replace ( v , " " )
fw . write ( line )
2022-05-14 06:46:20 +03:00
else :
2022-07-25 20:06:34 +03:00
fw . write ( line )
2022-05-14 06:46:20 +03:00
fw . close ( )
2022-07-25 20:06:34 +03:00
def to_csv ( ) :
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 ( )