This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Getting locale/language in struts beans

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic