-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathi18nhelper.py
More file actions
140 lines (115 loc) · 3.78 KB
/
i18nhelper.py
File metadata and controls
140 lines (115 loc) · 3.78 KB
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
import json
import os
import re
import sys
root = "src/main/java"
langf = "src/main/resources/lang/"
def replace_last(string, old, new):
old_idx = string.rfind(old)
return string[:old_idx] + new + string[old_idx+len(old):]
if sys.argv[1].__eq__("-skeleton"):
rawen = open(langf + "en" + ".json", "r")
print("Generating i18n Skeleton for SpotifyXP...")
if os.path.isfile(os.path.join(langf, "skeleton.json")):
os.remove(langf + "skeleton.json")
f = open(langf + "skeleton.json", "a")
c = 0
p = json.loads(rawen.read())
for key in p:
p[key] = ""
length = len(replace_last(json.dumps(p).replace("{", "", 1), "}", "").split("\","))
for li in replace_last(json.dumps(p).replace("{", "", 1), "}", "").split("\","):
if(c == 0):
li = "{\n" + replace_last(json.dumps(p).replace("{", "", 1), "}", "").split("\",")[0] + "\","
else:
if(c == length-1):
li = li + "\n}"
else:
li = li + "\","
f.write(li + "\n")
c = c+1
f.close()
print("Done!")
sys.exit()
langfile = open(langf + sys.argv[1] + ".json", "r")
files = []
def recursive(path):
for filename in os.listdir(path):
f = os.path.join(path, filename)
# checking if it is a file
if os.path.isfile(f):
files.append(f)
else:
recursive(f)
recursive(root)
used = []
for f in files:
if not str(f).lower().endswith(".java"):
continue
file = open(f, "r")
while True:
line = file.readline()
if not line:
break
if (file.name.endswith("ConfigValues.java")):
regex = r'"([^"]*)"'
parameters = re.findall(regex, line)
if len(parameters) > 0:
for parameter in parameters:
used.append(parameter)
continue
if not line.__contains__("PublicValues.language.translate"):
continue
if not line.__contains__("translate(\""):
continue
if not line.__contains__(","):
if(used.__contains__(line.split("translate(\"")[1].split("\"")[0])):
continue
used.append(line.split("translate(\"")[1].split("\"")[0])
continue
for found in line.split(","):
if not line.__contains__("translate(\""):
continue
if(found.__contains__("PublicValues.language.translate")):
if (used.__contains__(found.split("translate(\"")[1].split("\"")[0])):
continue
used.append(found.split("translate(\"")[1].split("\"")[0])
file.close()
parsed = json.loads(langfile.read())
unused = []
for key in parsed:
found = False
for u in used:
if u.__eq__(key):
found = True
break
if not found:
unused.append(key)
unused.remove("ui.about.deps")
unused.remove("ui.about.description")
unused.remove("ui.about.tested")
for u in unused:
print("Unused: " + u)
print("Found " + str(len(unused)) + " unused translations")
if len(unused) == 0:
exit(0)
inp = input("Delete those? (y/n) ")
if(inp.__eq__("y")):
for u in unused:
del parsed[u]
f = open(langf + sys.argv[1] + ".json", "w")
c = 0
length = len(replace_last(json.dumps(parsed).replace("{", "", 1), "}", "").split("\","))
for li in replace_last(json.dumps(parsed).replace("{", "", 1), "}", "").split("\","):
if(c == 0):
li = "{\n" + replace_last(json.dumps(parsed).replace("{", "", 1), "}", "").split("\",")[0] + "\","
else:
if(c == length-1):
li = li + "\n}"
else:
li = li + "\","
f.write(li + "\n")
c = c+1
f.close()
else:
print("Nothing was deleted")