-
Notifications
You must be signed in to change notification settings - Fork 9
/
csv2locale.py
142 lines (114 loc) · 3.47 KB
/
csv2locale.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
"""
----------------------------------------------
IKE
csv2locale.py
----------------------------------------------
Generates a Lua table from a CSV containing text
translations.
Usage:
python csv2locale.py infile.csv French
----------------------------------------------
"""
import sys
import csv
# CSV DATABASE QUERIES
class db:
# create a dictionary from a CSV
def load(csvFile):
dr = None
with open(csvFile, newline='', encoding='utf8') as cvf:
dr = list(csv.DictReader(cvf, delimiter="|"))
# convert strings to numbers where appropriate
for e in dr:
for k,v in e.items():
if v.isnumeric():
if v.find(".") != -1:
e[k] = float(v)
else:
e[k] = int(v)
return db(dr)
def __init__(self, data=None):
# Make sure the DB is initialized from new memory
if data == None:
self.data = []
else:
self.data = data
def __iter__(self):
return iter(self.data)
def __len__(self):
return len(self.data)
def __delitem__(self, index):
self.data.__delitem__(index)
def insert(self, index, value):
self.data.insert(index, value)
def __setitem__(self, index, value):
self.data.__setitem__(index, value)
def __getitem__(self, index):
result = self.data.__getitem__(index)
if isinstance(result, list):
return db(result)
return result
def sort(self, keyFunc):
self.data.sort(key=keyFunc)
def getIndex(self, i):
return self.data[i]
def append(self, e):
self.data.append(e)
# copy DB and execute a function on each element
def transform(self, executeFunc):
elements = self.data.copy()
for element in elements:
executeFunc(element)
return db(elements)
# execute a function on each element of DB in place
def transformInPlace(self, executeFunc):
elements = self.data
for element in elements:
executeFunc(element)
# return results filtered by a query function
def query(self, queryFunc):
results = []
for element in self:
if queryFunc(element):
results.append(element.copy())
return db(results)
# return first element matching query function
def findFirst(self, queryFunc):
for element in self:
if queryFunc(element):
return element
return None
# DB Fields: ID, Text
def start_defs(f, csvfile, lan):
f.write(f"-- EXPORTED FROM {csvfile}\n\n")
f.write(f"\t[\"{lan}\"] = {{\n")
def add_translation(f, id, trans, line_num):
if line_num > 0:
f.write(",\n")
f.write(f"\t\t[\"{id}\"] = \"{trans.rstrip()}\"")
def end_defs(f):
f.write("\n\t},")
if __name__ == "__main__":
if len(sys.argv) < 2:
print("You must specify a CSV file!")
sys.exit()
if len(sys.argv) < 3:
print("You must specify a language name!")
sys.exit()
csv_file = sys.argv[1]
lang_name = sys.argv[2]
csv_db = db.load(csv_file)
export_file = f"locale/{lang_name}_locale.lua"
with open(export_file, 'w') as file:
start_defs(file, csv_file, lang_name)
line_num = 0
for item in csv_db:
add_translation(
file,
item['ID'],
item['Text'],
line_num
)
line_num = line_num + 1
end_defs(file)
print(f"{lang_name} localization written to {export_file}.")