-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTHelper.java
181 lines (156 loc) · 3.9 KB
/
THelper.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package tk.zeitheron.thelper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Scanner;
public class THelper
{
public static void main(String[] args)
{
try
{
File langs = new File("Localizations");
final LangFile en_us = new LangFile(new File(langs, "en_us.lang"));
for(File langFile : langs.listFiles(f -> f.getName().endsWith(".lang") && !f.getName().contains("en_us")))
{
System.out.print("Loading " + langFile.getName() + "... ");
LangFile old = new LangFile(langFile);
System.out.print("Processing scheme... ");
LangFile newLang = old.applyLineScheme(en_us);
System.out.print("Saving... ");
newLang.save(langFile);
System.out.println("Done!");
}
} catch(Throwable err)
{
err.printStackTrace();
}
}
public static class LangFile
{
public final int lineCount;
public final Map<Integer, String> lines = new HashMap<>();
public final Map<Integer, Translation> translations = new HashMap<>();
public final Map<String, String> keyToValue = new HashMap<>();
public final Map<String, Integer> keyToLine = new HashMap<>();
public LangFile(int lines)
{
this.lineCount = lines;
}
public LangFile(File langFile) throws FileNotFoundException
{
int i = 0;
try(Scanner in = new Scanner(langFile, "UTF-8"))
{
while(in.hasNextLine())
{
String ln = in.nextLine();
lines.put(i, ln);
if(ln.trim().startsWith("#"))
{
++i;
continue;
}
if(ln.contains("="))
{
String[] kv = ln.split("=", 2);
translations.put(i, new Translation(kv[0]).withValue(kv[1]));
keyToValue.put(kv[0], kv[1]);
keyToLine.put(kv[0], i);
}
++i;
}
}
this.lineCount = i;
}
public int getLineOfKey(String key)
{
return keyToLine.containsKey(key) ? keyToLine.get(key) : -1;
}
public LangFile applyLineScheme(LangFile from)
{
LangFile nf = new LangFile(from.lineCount);
for(int i = 0; i < from.lineCount; ++i)
{
if(from.translations.containsKey(i))
{
Translation t = from.translations.get(i);
String key = t.key;
String value = keyToValue.getOrDefault(key, t.value);
nf.lines.put(i, key + "=" + value);
nf.keyToLine.put(key, i);
nf.keyToValue.put(key, value);
nf.translations.put(i, t.shadeKey(value));
} else
nf.lines.put(i, from.lines.get(i));
}
return nf;
}
public void save(File langFile) throws IOException
{
try(BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(langFile), StandardCharsets.UTF_8)))
{
for(int i = 0; i < lineCount; ++i)
{
if(translations.containsKey(i))
{
Translation t = translations.get(i);
bw.write(t.getKey() + '=' + keyToValue.get(t.getKey()));
} else
{
bw.write(lines.get(i));
}
if(i < lineCount - 1)
bw.newLine();
}
}
}
}
public static class Translation implements Entry<String, String>
{
public final String key;
public String value;
public Translation(String key)
{
this.key = key;
}
public boolean keyEquals(Translation t)
{
return Objects.equals(t.key, key);
}
@Override
public String getKey()
{
return key;
}
@Override
public String getValue()
{
return value;
}
@Override
public String setValue(String value)
{
String old = this.value;
this.value = value;
return old;
}
public Translation withValue(String value)
{
setValue(value);
return this;
}
public Translation shadeKey(String value)
{
return new Translation(getKey()).withValue(value);
}
}
}