• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Getting locale/language in struts beans  RSS feed

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
Is there a way to get the selected locale-language in a struts bean? I would like to do something like:

<logic:equal name="lang" value="en">
English selected
</logic:equal>

<logic:notEqual name="lang" value="en">
Different language selected
</logic:equal>

Please note, that this is really simplyfied, I of course use MessageResources, but I wanted to make the example simple as much as I can. So is there a way to get some block executed only if some language is selected? Or if not, what's the simplest way to set the 'lang' property right, so the code above will be functional.

Thank you.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The preferred locale of the user is set in the browser and passed in one of the request headers. Struts then finds this information and determines which locale to use.

Struts places this value in the form of a java.util.Locale object in the HttpSession. You can get the language with the following code:

((java.util.Locale)session.getAttribute(org.apache.struts.Globals.LOCALE_KEY)).getDisplayLanguage();
 
Petr Turek
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much, it works. I would have one more question on the topic of getting the locale. In jsp I have session, so I can access it to read the Locale, but what about in some servlet method with no request or session to use (assuming I don't want to pass them or locale itself to the method)? If I would call a method doSomething() from the action servlet and the method would look like:

public void doSomething() {
lang = /** get the language somehow */;
if (lang.equals("en")) {
//do something else
}
}

I can't get the locale from session or reqest, so is there some global value or anything else I could use?

Thank you again.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the request is passed as a parameter to the execute() method of the action class, You have access to the session there. To get the session from the request, use the following:

HttpSession session = request.getSession();

Once you have the session, you can get the locale, as shown in my previous post.

If you need the locale in any of your other methods, just pass it as a parameter. Since the locale could vary for each user, it makes sense that it can only be obtained from each user's session. There is a default locale, but that may not be the same as an individual user's locale.
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!