Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

ResourceBundle.getBundle

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
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);
System.out.println(messages.getString("greetings"));
System.out.println(messages.getString("inquiry"));
System.out.println(messages.getString("farewell"));
MessageBundle.properties
========================
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 MessageBundle.properties file.
Rgds,
Seetesh
 
Rick DeBay
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic