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
|
package com.earth2me.essentials;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import net.ess3.api.IEssentials;
import org.bukkit.Bukkit;
public class I18n implements net.ess3.api.II18n
{
private static I18n instance;
private static final String MESSAGES = "messages";
private final transient Locale defaultLocale = Locale.getDefault();
private transient Locale currentLocale = defaultLocale;
private transient ResourceBundle customBundle;
private transient ResourceBundle localeBundle;
private final transient ResourceBundle defaultBundle;
private transient Map<String, MessageFormat> messageFormatCache = new HashMap<String, MessageFormat>();
private final transient IEssentials ess;
private static final Pattern NODOUBLEMARK = Pattern.compile("''");
private static final ResourceBundle NULL_BUNDLE = new ResourceBundle()
{
public Enumeration<String> getKeys()
{
return null;
}
protected Object handleGetObject(String key)
{
return null;
}
};
public I18n(final IEssentials ess)
{
this.ess = ess;
customBundle = NULL_BUNDLE;
localeBundle = NULL_BUNDLE;
defaultBundle = ResourceBundle.getBundle(MESSAGES, Locale.ENGLISH);
}
public void onEnable()
{
instance = this;
}
public void onDisable()
{
instance = null;
}
@Override
public Locale getCurrentLocale()
{
return currentLocale;
}
private String translate(final String string)
{
try
{
try
{
return customBundle.getString(string);
}
catch (MissingResourceException ex)
{
return localeBundle.getString(string);
}
}
catch (MissingResourceException ex)
{
Logger.getLogger("Essentials").log(Level.WARNING, String.format("Missing translation key \"%s\" in translation file %s", ex.getKey(), localeBundle.getLocale().toString()), ex);
return defaultBundle.getString(string);
}
}
public static String tl(final String string, final Object... objects)
{
if (instance == null)
{
return "";
}
if (objects.length == 0)
{
return NODOUBLEMARK.matcher(instance.translate(string)).replaceAll("'");
}
else
{
return instance.format(string, objects);
}
}
public String format(final String string, final Object... objects)
{
String format = translate(string);
MessageFormat messageFormat = messageFormatCache.get(format);
if (messageFormat == null)
{
try
{
messageFormat = new MessageFormat(format);
}
catch (IllegalArgumentException e)
{
ess.getLogger().log(Level.SEVERE, "Invalid Translation key for '" + string + "': " + e.getMessage());
format = format.replaceAll("\\{(\\D*?)\\}", "\\[$1\\]");
messageFormat = new MessageFormat(format);
}
messageFormatCache.put(format, messageFormat);
}
return messageFormat.format(objects);
}
public void updateLocale(final String loc)
{
if (loc == null || loc.isEmpty())
{
return;
}
final String[] parts = loc.split("[_\\.]");
if (parts.length == 1)
{
currentLocale = new Locale(parts[0]);
}
if (parts.length == 2)
{
currentLocale = new Locale(parts[0], parts[1]);
}
if (parts.length == 3)
{
currentLocale = new Locale(parts[0], parts[1], parts[2]);
}
ResourceBundle.clearCache();
messageFormatCache = new HashMap<String, MessageFormat>();
Logger.getLogger("Essentials").log(Level.INFO, String.format("Using locale %s", currentLocale.toString()));
try
{
localeBundle = ResourceBundle.getBundle(MESSAGES, currentLocale);
}
catch (MissingResourceException ex)
{
localeBundle = NULL_BUNDLE;
}
try
{
customBundle = ResourceBundle.getBundle(MESSAGES, currentLocale, new FileResClassLoader(I18n.class.getClassLoader(), ess));
}
catch (MissingResourceException ex)
{
customBundle = NULL_BUNDLE;
}
}
public static String capitalCase(final String input)
{
return input == null || input.length() == 0
? input
: input.toUpperCase(Locale.ENGLISH).charAt(0)
+ input.toLowerCase(Locale.ENGLISH).substring(1);
}
private static class FileResClassLoader extends ClassLoader
{
private final transient File dataFolder;
FileResClassLoader(final ClassLoader classLoader, final IEssentials ess)
{
super(classLoader);
this.dataFolder = ess.getDataFolder();
}
@Override
public URL getResource(final String string)
{
final File file = new File(dataFolder, string);
if (file.exists())
{
try
{
return file.toURI().toURL();
}
catch (MalformedURLException ex)
{
}
}
return null;
}
@Override
public InputStream getResourceAsStream(final String string)
{
final File file = new File(dataFolder, string);
if (file.exists())
{
try
{
return new FileInputStream(file);
}
catch (FileNotFoundException ex)
{
}
}
return null;
}
}
}
|