-
Notifications
You must be signed in to change notification settings - Fork 1
/
convert_text_to_csv_r.py
88 lines (77 loc) · 2.92 KB
/
convert_text_to_csv_r.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import argparse
import glob
import json
from os.path import abspath
from os.path import join as path_join
from collections import OrderedDict
import csv
def save_to_csv(path, data):
with open(path, "w", newline="", encoding="utf-8") as f:
csv_writer = csv.writer(f)
csv_writer.writerow(["Key", "English", "Japanese", "Korean", "Unknown"])
for key in data:
el = data[key]
csv_writer.writerow(
[
key,
el["en"] if "en" in el else "",
el["jp"] if "jp" in el else "",
el["kr"] if "kr" in el else "",
el["unknown"] if "unknown" in el else "",
]
)
def update_data_form_json(json_path, path_key, data, lang):
with open(json_path, "r", encoding="utf-8") as json_f:
json_data = json.load(json_f)
for entry_wrapper_index in range(len(json_data["Entries"])):
entry_wrapper = json_data["Entries"][entry_wrapper_index]
for entry_index in range(len(entry_wrapper)):
entry = entry_wrapper[entry_index].replace("\n", "\\n")
key = f"{path_key}|{entry_wrapper_index}|{entry_index}"
if key in data:
tmp = data[key]
tmp[lang] = entry
data[key] = tmp
else:
data[key] = {lang: entry}
def load_data_from_dir(path):
data = OrderedDict()
json_files = glob.glob(path_join(path, "**", "*.json"), recursive=True)
for json_file in json_files:
if json_file.endswith(".fif.json"):
continue
elif json_file.endswith("English.json"):
path_key = json_file[len(path): -len("English.json")]
update_data_form_json(json_file, path_key, data, "en")
continue
elif json_file.endswith("Japanese.json"):
path_key = json_file[len(path): -len("Japanese.json")]
update_data_form_json(json_file, path_key, data, "jp")
continue
elif json_file.endswith("Korean.json"):
path_key = json_file[len(path): -len("Korean.json")]
update_data_form_json(json_file, path_key, data, "kr")
continue
else:
path_key = json_file[len(path): -len(".json")]
update_data_form_json(json_file, path_key, data, "unknown")
return data
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Convert all text in the game to csv for translation"
)
parser.add_argument(
"-d",
"--dir_path",
help="directory path to convert",
required=True,
)
parser.add_argument(
"-c",
"--csv",
help="file path of csv to save",
required=True,
)
args = parser.parse_args()
loaded_data = load_data_from_dir(abspath(args.dir_path))
save_to_csv(args.csv, loaded_data)