Java resource

Is it possible to maintain a resource bundle in a folder structure something similar to the following:

1. us\en 2. ae\en 3. ae\ar 

The resource package file name will be the same, but supported in different folders. I know Java recommendations

myresource_en_US.properties

But I need to maintain them in folders and use resource package classes for access. I am using JDK 6. Does anyone know how I can do this?

+4
source share
2 answers

Yes, you can control the download using Control . Here is an example run:

 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; } } 

(source code is copied from the default implementation only with a modified resourceName and a change in PropertyResourceBundle to read the stream as UTF-8 - there is no need for native2ascii)

which you use as follows

 ResourceBundle bundle = ResourceBundle.getBundle("myresource", new FolderControl()); // ... 

See also:

+6
source

Take a look at http://download.oracle.com/javase/6/docs/api/java/util/ResourceBundle.Control.html . You can pass it as an argument to ResourceBundle.getBundle() as a way to "provide different conventions in organizing and packaging localized resources."

0
source

All Articles