-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Translator.java
318 lines (286 loc) · 10.5 KB
/
Translator.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package net.citizensnpcs.api.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.ListResourceBundle;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import com.google.common.collect.Maps;
import com.google.common.io.Closeables;
import com.google.common.io.FileWriteMode;
import com.google.common.io.Files;
import com.google.common.io.Resources;
public class Translator {
private final Locale defaultLocale;
private final Map<String, MessageFormat> messageFormatCache = Maps.newHashMap();
private ResourceBundle preferredBundle;
private final File resourceFile;
private Translator(File resourceFile, Locale locale) {
this.resourceFile = resourceFile;
this.defaultLocale = locale;
try {
preferredBundle = ResourceBundle.getBundle(PREFIX, defaultLocale,
new FileClassLoader(Translator.class.getClassLoader(), resourceFile));
} catch (MissingResourceException e) {
preferredBundle = getDefaultBundle();
Messaging.severe("Missing preferred location bundle.");
}
}
private String format(String key, Locale locale, Object... msg) {
String unreplaced = translate(key, locale);
MessageFormat formatter = getFormatter(unreplaced);
return formatter.format(msg);
}
private ResourceBundle getBundle(Locale locale) {
try {
ResourceBundle bundle = ResourceBundle.getBundle(PREFIX, locale,
new FileClassLoader(Translator.class.getClassLoader(), resourceFile));
return bundle == null ? preferredBundle : bundle;
} catch (MissingResourceException e) {
return preferredBundle;
}
}
private ResourceBundle getDefaultBundle() {
return getDefaultResourceBundle(resourceFile, PREFIX + "_en.properties");
}
private MessageFormat getFormatter(String unreplaced) {
MessageFormat formatter = messageFormatCache.get(unreplaced);
if (formatter == null)
messageFormatCache.put(unreplaced, formatter = new MessageFormat(unreplaced));
return formatter;
}
private String translate(String key, Locale locale) {
ResourceBundle bundle = preferredBundle;
if (locale != defaultLocale)
bundle = getBundle(locale);
try {
return bundle.getString(key);
} catch (MissingResourceException e) {
try {
return getDefaultBundle().getString(key);
} catch (MissingResourceException ex) {
return "?" + key + "?";
}
}
}
private static class FileClassLoader extends ClassLoader {
private final File folder;
public FileClassLoader(ClassLoader classLoader, File folder) {
super(classLoader);
this.folder = folder;
}
@Override
public URL getResource(String string) {
File file = new File(folder, string);
if (file.exists()) {
try {
return file.toURI().toURL();
} catch (MalformedURLException ex) {
}
} else {
string = string.replaceFirst("/", "");
URL test = Translator.class.getResource('/' + string);
if (test != null)
return test;
}
return super.getResource(string);
}
@Override
public InputStream getResourceAsStream(String string) {
File file = new File(folder, string);
if (file.exists()) {
try {
return new FileInputStream(file);
} catch (FileNotFoundException ex) {
}
} else {
string = string.replaceFirst("/", "");
InputStream stream = Translator.class.getResourceAsStream('/' + string);
if (stream != null) {
new Thread(new SaveResource(folder, string)).start();
return stream;
}
}
return super.getResourceAsStream(string);
}
}
private static class SaveResource implements Runnable {
private final String fileName;
private final File rootFolder;
private SaveResource(File rootFolder, String fileName) {
this.rootFolder = rootFolder;
this.fileName = fileName;
}
@Override
public void run() {
File file = new File(rootFolder, fileName);
if (file.exists())
return;
try {
rootFolder.mkdirs();
File to = File.createTempFile(fileName, null, rootFolder);
to.deleteOnExit();
Resources.asByteSource(Resources.getResource(Translator.class, '/' + fileName))
.copyTo(Files.asByteSink(to, FileWriteMode.APPEND));
if (!file.exists()) {
to.renameTo(file);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static interface TranslationProvider {
InputStream createInputStream();
String getName();
}
private static void addTranslation(TranslationProvider from, File to) {
Properties props = new Properties();
InputStream in = from.createInputStream();
try {
props.load(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
Closeables.closeQuietly(in);
}
if (to.exists()) {
try {
props.load(in = new FileInputStream(to));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
Closeables.closeQuietly(in);
}
}
OutputStream out = null;
try {
props.store(out = new FileOutputStream(to), "");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {
e.getCause(); // Do nothing
}
}
}
public static void addTranslations(Collection<TranslationProvider> providers) {
for (TranslationProvider provider : providers) {
addTranslation(provider, new File(instance.resourceFile, provider.getName()));
}
defaultBundle = null;
setInstance(instance.resourceFile, instance.preferredBundle.getLocale());
}
public static void addTranslations(TranslationProvider... providers) {
addTranslations(Arrays.asList(providers));
}
private static Properties getDefaultBundleProperties() {
Properties defaults = new Properties();
InputStream in = null;
try {
in = Translator.class.getResourceAsStream("/" + PREFIX + "_en.properties");
defaults.load(in);
} catch (IOException e) {
} finally {
Closeables.closeQuietly(in);
}
return defaults;
}
private static ResourceBundle getDefaultResourceBundle(File resourceDirectory, String fileName) {
if (Translator.defaultBundle != null)
return Translator.defaultBundle;
resourceDirectory.mkdirs();
File bundleFile = new File(resourceDirectory, fileName);
if (!bundleFile.exists()) {
try {
bundleFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Translator.populateDefaults(bundleFile);
FileInputStream stream = null;
try {
stream = new FileInputStream(bundleFile);
Translator.defaultBundle = new PropertyResourceBundle(stream);
} catch (Exception e) {
e.printStackTrace();
Translator.defaultBundle = Translator.getFallbackResourceBundle();
} finally {
Closeables.closeQuietly(stream);
}
return Translator.defaultBundle;
}
private static ResourceBundle getFallbackResourceBundle() {
return new ListResourceBundle() {
@Override
protected Object[][] getContents() {
return new Object[0][0];
}
};
}
private static void populateDefaults(File bundleFile) {
Properties properties = new Properties();
InputStream in = null;
try {
in = new FileInputStream(bundleFile);
properties.load(in);
} catch (IOException e) {
} finally {
Closeables.closeQuietly(in);
}
Properties defaults = getDefaultBundleProperties();
for (Entry<Object, Object> entry : defaults.entrySet()) {
if (!properties.containsKey(entry.getKey()))
properties.put(entry.getKey(), entry.getValue());
}
OutputStream stream = null;
try {
stream = new FileOutputStream(bundleFile);
properties.store(stream, "");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (Exception e) {
e.getCause(); // Do nothing
}
}
}
public static void setInstance(File dataFolder, Locale preferredLocale) {
instance = new Translator(dataFolder, preferredLocale);
}
public static String translate(String key, Locale preferredLocale, Object... msg) {
return Colorizer.parseColors(msg.length == 0 ? instance.translate(key, preferredLocale)
: instance.format(key, preferredLocale, msg));
}
public static String translate(String key, Object... msg) {
return translate(key, instance.defaultLocale, msg);
}
private static ResourceBundle defaultBundle;
private static Translator instance;
public static final String PREFIX = "messages";
}