4

My Java app has been localised into several locales. For each locale there is a properties file with the translations. I load it as follows:

ResourceBundle bundle = ResourceBundle.getBundle("MessagesBundle", Locale.getDefault());

I'd like to determine at runtime all available resource bundles. The bundles are in my application's jar file.

How can I do this?

Steve McLeod
  • 50,632
  • 46
  • 122
  • 180

3 Answers3

1

Why you don't try to find all property files in your class path that start with "MessageBundle" and end with ".properties". It's dirty, but it'll work.

Here is useful code how to accomplish this- Code to find files on your classpath

Valchev
  • 1,442
  • 14
  • 15
1

There is no standard method with ResourceBundle class. You may use (hackish solution) - Thread.currentThread().getContextClassLoader().getResource() method or this.getClass().getResource("/"). Have look at another SO thread having same title.

Community
  • 1
  • 1
KV Prajapati
  • 92,042
  • 19
  • 143
  • 183
0

If you use Spring you can try with PathMatchingResourcePatternResolver:

ClassLoader cl = this.getClass().getClassLoader(); 
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
Resource[] resources = resolver.getResources("classpath*:/MessagesBundle.properties") ;
for (Resource resource: resources){
    logger.info(resource.getFilename());
}
mgsCatDev
  • 126
  • 1
  • 1
  • 9