forked from hagrawal7/CustomResourceBundleControl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomResourceBundleControl.java
44 lines (40 loc) · 1.55 KB
/
CustomResourceBundleControl.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
import java.io.*;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ResourceBundle.Control;
import java.util.*;
public class CustomResourceBundleControl extends Control {
private String encodingScheme = "UTF-8";
public CustomResourceBundleControl(String encodingSchemeStr) {
encodingScheme = encodingSchemeStr;
}
public ResourceBundle newBundle (String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException {
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, "properties");
ResourceBundle bundle = null;
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
try {
// Read properties files as user defined and if not provided then use "UTF-8" encoding.
bundle = new PropertyResourceBundle(new InputStreamReader(stream, encodingScheme));
} finally {
stream.close();
}
}
return bundle;
}
}