| 
									
										
										
										
											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-25 19:23:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-28 10:50:39 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-25 19:23:03 +03:00
										 |  |  | def get_lang(lang): | 
					
						
							| 
									
										
										
										
											2023-10-28 10:50:39 +02:00
										 |  |  |     out = {} | 
					
						
							|  |  |  |     for ln in open('./src/lang/%s.rs' % lang, encoding='utf8'): | 
					
						
							|  |  |  |         ln = ln.strip() | 
					
						
							|  |  |  |         if ln.startswith('("'): | 
					
						
							|  |  |  |             k, v = line_split(ln) | 
					
						
							|  |  |  |             out[k] = v | 
					
						
							|  |  |  |     return out | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-14 11:46:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def line_split(line): | 
					
						
							| 
									
										
										
										
											2022-12-25 19:23:03 +03:00
										 |  |  |     toks = line.split('", "') | 
					
						
							| 
									
										
										
										
											2022-12-04 18:39:59 +08:00
										 |  |  |     if len(toks) != 2: | 
					
						
							|  |  |  |         print(line) | 
					
						
							| 
									
										
										
										
											2023-10-28 10:59:33 +02:00
										 |  |  |         assert 0 | 
					
						
							| 
									
										
										
										
											2023-05-12 09:43:27 +08:00
										 |  |  |     # Replace fixed position. | 
					
						
							|  |  |  |     # Because toks[1] may be v") or v"), | 
					
						
							|  |  |  |     k = toks[0][toks[0].find('"') + 1:] | 
					
						
							|  |  |  |     v = toks[1][:toks[1].rfind('"')] | 
					
						
							| 
									
										
										
										
											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(): | 
					
						
							| 
									
										
										
										
											2023-10-28 10:50:39 +02:00
										 |  |  |     if len(sys.argv) == 1: | 
					
						
							|  |  |  |         expand() | 
					
						
							|  |  |  |     elif sys.argv[1] == '1': | 
					
						
							|  |  |  |         to_csv() | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         to_rs(sys.argv[1]) | 
					
						
							| 
									
										
										
										
											2022-07-26 01:06:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def expand(): | 
					
						
							| 
									
										
										
										
											2023-10-28 10:50:39 +02:00
										 |  |  |     for fn in glob.glob('./src/lang/*.rs'): | 
					
						
							|  |  |  |         lang = os.path.basename(fn)[:-3] | 
					
						
							|  |  |  |         if lang in ['en', 'template']: continue | 
					
						
							|  |  |  |         print(lang) | 
					
						
							|  |  |  |         dict = get_lang(lang) | 
					
						
							|  |  |  |         fw = open("./src/lang/%s.rs" % lang, "wt", encoding='utf8') | 
					
						
							|  |  |  |         for line in open('./src/lang/template.rs', encoding='utf8'): | 
					
						
							|  |  |  |             line_strip = line.strip() | 
					
						
							|  |  |  |             if line_strip.startswith('("'): | 
					
						
							|  |  |  |                 k, v = line_split(line_strip) | 
					
						
							|  |  |  |                 if k in dict: | 
					
						
							|  |  |  |                     # embraced with " to avoid empty v | 
					
						
							|  |  |  |                     line = line.replace('"%s"' % v, '"%s"' % dict[k]) | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     line = line.replace(v, "") | 
					
						
							|  |  |  |                 fw.write(line) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 fw.write(line) | 
					
						
							|  |  |  |         fw.close() | 
					
						
							| 
									
										
										
										
											2022-12-25 19:23:03 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-26 01:06:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def to_csv(): | 
					
						
							| 
									
										
										
										
											2023-10-28 10:50:39 +02:00
										 |  |  |     for fn in glob.glob('./src/lang/*.rs'): | 
					
						
							|  |  |  |         lang = os.path.basename(fn)[:-3] | 
					
						
							|  |  |  |         csvfile = open('./src/lang/%s.csv' % lang, "wt", encoding='utf8') | 
					
						
							|  |  |  |         csvwriter = csv.writer(csvfile) | 
					
						
							|  |  |  |         for line in open(fn, encoding='utf8'): | 
					
						
							|  |  |  |             line_strip = line.strip() | 
					
						
							|  |  |  |             if line_strip.startswith('("'): | 
					
						
							|  |  |  |                 k, v = line_split(line_strip) | 
					
						
							|  |  |  |                 csvwriter.writerow([k, v]) | 
					
						
							|  |  |  |         csvfile.close() | 
					
						
							| 
									
										
										
										
											2022-07-26 01:06:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def to_rs(lang): | 
					
						
							| 
									
										
										
										
											2023-10-28 10:50:39 +02:00
										 |  |  |     csvfile = open('%s.csv' % lang, "rt", encoding='utf8') | 
					
						
							|  |  |  |     fw = open("./src/lang/%s.rs" % lang, "wt", encoding='utf8') | 
					
						
							|  |  |  |     fw.write('''lazy_static::lazy_static! {
 | 
					
						
							| 
									
										
										
										
											2022-07-26 01:06:34 +08:00
										 |  |  | pub static ref T: std::collections::HashMap<&'static str, &'static str> = | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  | ''')
 | 
					
						
							| 
									
										
										
										
											2023-10-28 10:50:39 +02:00
										 |  |  |     for row in csv.reader(csvfile): | 
					
						
							|  |  |  |         fw.write('        ("%s", "%s"),\n' % (row[0].replace('"', '\"'), row[1].replace('"', '\"'))) | 
					
						
							|  |  |  |     fw.write('''    ].iter().cloned().collect();
 | 
					
						
							| 
									
										
										
										
											2022-07-26 01:06:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | ''')
 | 
					
						
							| 
									
										
										
										
											2023-10-28 10:50:39 +02:00
										 |  |  |     fw.close() | 
					
						
							| 
									
										
										
										
											2022-07-26 01:06:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | main() |