Reputation: 2497
Is it possible to maintain the resource bundle in a folder structure something similar to the following;
1. us\en
2. ae\en
3. ae\ar
The resource bundle file name will be the same but maintained in different folders. I know Java recomends
myresource_en_US.properties
But I need to maintained them in folders and use the resource bundle classes to access. I am using JDK 6. Does anyone know how I can do it?
Upvotes: 5
Views: 5905
Reputation: 10843
Have a look at http://download.oracle.com/javase/6/docs/api/java/util/ResourceBundle.Control.html. You can pass one as an argument to ResourceBundle.getBundle()
as a way "to provide different conventions in the organization and packaging of localized resources".
Upvotes: 0
Reputation: 1108537
Yes, you can control the loading with a custom Control
. Here's a kickoff example:
public class FolderControl extends Control {
public ResourceBundle newBundle
(String baseName, Locale locale, String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException, IOException
{
String resourceName = "/" + locale.getCountry() + "/" + locale.getLanguage()
+ "/" baseName + ".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 {
bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
} finally {
stream.close();
}
}
return bundle;
}
}
(the source code is copied from the default implementation with only the resourceName
changed and the PropertyResourceBundle
being changed to read the stream as UTF-8 --no need for native2ascii anymore)
which you use as follows
ResourceBundle bundle = ResourceBundle.getBundle("myresource", new FolderControl());
// ...
Upvotes: 7