File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes ResourceBundle.getBundle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ResourceBundle.getBundle" Watch "ResourceBundle.getBundle" New topic


Rick DeBay
Ranch Hand

Joined: Jul 11, 2003
Posts: 70
I'm loading all my messages from a properties file using the JSTL fmt:message tags. But now I have a case where I need to load a message from the same file, but from a Struts Action class.
How do I load a localized message from a properties file? I looked at ResourceBundle.getBundle, but I don't know how to specify the file. The API states that it must be a fully qualified class name, but where would it start looking for the file? In the WEB-INF/classes directory?
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Code snippet to explain the example.
String language = "en";
String country = "US";
Locale currentLocale;
java.util.ResourceBundle messages;
currentLocale = new Locale(language, country);
messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
greetings = Hello.
farewell = Goodbye.
inquiry = How are you?
Depending on the MessageBundle properties file, the output will vary. Now its upto u to code to retrieve the file.
Rick DeBay
Ranch Hand

Joined: Jul 11, 2003
Posts: 70
Thanks, I had that part. What I wanted to avoid was hunting around as to where it would start loading the resource from. It turns out it does base the path on WEB-INF/classes. Now if I can only get the user's locale, JSTL isn't setting a session attribute after determining the browser-based locale. Over to the JSP forum...
I agree. Here's the link:
subject: ResourceBundle.getBundle
It's not a secret anymore!