• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to make the ResourceBundle traverse both .java and .properties at the same time?

 
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all , so as i am making some exercises on prioritization of which bundle is selected i ran into something that i am not sure i understand correctly.

Lets say that my classpath is pointing to a subfolder "classes" . There i have all the .class files for the resourcebundles from java files and all my .properties resources as well. the resource name is "test" with the appropriate suffixes for locales and languages.

If i try to access them like that:

The resource loader goes only through the .properties files.

If i access it like that:


It goes through .class files only.
It all sounds logical , but i was wandering is there a way to make it traverse everything on one go , without the need for a second resourceBundle .

At the exam , can this be a pointer to what will be used a .java or .properties, the way of access?
 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, ResourceBundle.getBundle() loads either a class or a resource, not both. Note that the approach that you use to load the class will also work for the resource, but only if a class can't be found.

Is there a reason you want to load from both? Typically you would only use one approach.
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was for exercise only to check how the hierarchy works. And i am not expecting it to load both , just to make sure it goes through both .class and .properties files while searching for best match.

My question was mostly based on my expectation of this being true.

Note that the approach that you use to load the class will also work for the resource, but only if a class can't be found.  



Aaaaaaand i just realized i had wrong locale input ofr one of my tests, which was messing it up. I have spend quite a lot of time on this before posting,  sorry for wasting your time.

So basically yes. With a "." it works for both .class and .properties files. Thanks a lot!!!
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keep in mind that it's bad practice to use slashes in resource names. Resources should use the same naming scheme as classes. If the folder they're in is on the class path or if they reside inside a JAR, they will be found automatically by the class loader.
 
Claude Sylvanshine
Greenhorn
Posts: 22
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ill have this in mind, thank you .
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Technically it could be possible to have a ResourceBundle that reads from both classes and properties files, but it would require a lot of work. You would need to provide your own implementation of ResourceBundle.Control, which would also probably need to return a custom ResourceBundle subclass. Then you'd need to call ResourceBundle.getBundle with an instance of that ResourceBundle.Control implementation.
reply
    Bookmark Topic Watch Topic
  • New Topic