views:

141

answers:

2

Hello all ,

i am using ResourceBundle and i want to give the user an option to select a language for the GUI.

i want to get a list of all resource files that are under a specific package.

i don't know what resource i will have since this application based on plug-ins is there an option to ask from java to search all available resources under a package ?

(if no i guess the plug-in should give all available local for it)

thank you all

+1  A: 

The files may reside on a web server. There is no standard way of listing files (pages) on a web server. So, in general, what you need to do is remember which locales you have resources for (perhaps create a list as part of your build process).

Tom Hawtin - tackline
i think this is what i do ,that way i could manage all locals also
shay
+1  A: 

Unfortunately, the ResourceBundle API doesn't offer facilities for that. For every requested locale it just tries to locate it and if none is found, then the closest candiate/fallback locale will be tried repeatedly.

One of the ways to reveal them all would be to just scan the classpath for the files yourself.

ClassLoader loader = Thread.currentThread().getContextClassLoader();
final String bundlepackage = "com.example.i18n";
final String bundlename = "messages";

File root = new File(loader.getResource(bundlepackage.replace('.', '/')).getFile());
File[] files = root.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.matches("^" + bundlename + "(_\\w{2}(_\\w{2})?)?\\.properties$");
    }
});

Set<String> languages = new TreeSet<String>();
for (File file : files) {
    languages.add(file.getName().replaceAll("^" + bundlename + "(_)?|\\.properties$", ""));
}
BalusC
this is a greate sample. i am sure i will use it in the future , thank you
shay
What happen if jar's are not in classpath and i am loading my plugins class's using URLClassLoader how can i add a resource file inside a plugin to my classpath i will post a new one for this , its hard to see comments here
shay
Grab *that* classloader instead. As to reading ussues, just enlarge the browser font through browser settings or `Ctrl+MouseWheelUp`.
BalusC
what i did was , holding all the locales supported by each pluginand loading the resource bundle fileby the path provided by the plugin ,locale's supported by the plug-in ,and the Plugin.getClass().getClassLoader()and its working like magic thank you BalusC
shay