0

Can I rewrite the session attribute WW_TRANS_I18N_LOCALE in Struts2? I want to set locale in cookies, for future use, because by default session have a timeout of 30 minutes, this is to small amount of time for the locale, if user isn't using the site. I am trying to set WW_TRANS_I18N_LOCALE depending on cookies value, but without any luck, the value remains the same as it is.

I found here a question like mine, but all of my jsp's pass through Actions https://stackoverflow.com/questions/5291271/struts-2-internationalisation-problem , and this is not a solution ..

Community
  • 1
  • 1
Denees
  • 9,100
  • 13
  • 47
  • 76

1 Answers1

1

So you want to change a session value in an action? I'm not clear on the last line, but just implement SessionAware then it should be straight forward. The best place to set the value would probably where ever the user logs in (if any).

Something like...

import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

public class MyAction extends ActionSupport implements SessionAware{
    Map<String, Object> session;

    @Override
    public String execute(){
        session.put("WW_TRANS_I18N_LOCALE", "fr");
        return SUCCESS;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }
}

should do. There are aware interfaces for most other scopes see the top of this page: http://struts.apache.org/2.0.11/struts2-core/apidocs/org/apache/struts2/interceptor/package-summary.html

Edit: Thinking about this I can't recommend using cookies to store the language preferences. But if you were to do this... the default i18n interceptor will check if a parameter called "request_locale" exists and set the value on the session to that value. You don't want a value stored on the session. But since this is how struts2 handles this by default you could do something like the following:

  • create your own i18n interceptor which is a copy of the existing but removes the language data saved into the session (thus the value pushed into the session, really only exists for the request duration).
  • create a javascript function which is added to every page (probably through some template system), which after the page has loaded looks for all anchor tags and form tags. In the case of anchor tags is adds the "request_local" parameter to the end from the value that it finds in your cookie, and it will add a hidden field to every form which will also set the "request_local" parameter from the cookie.
  • I'd probably put a language selection drop down on every page too, which sets the cookie value and then would reload the current page. For an idea of creating such a list (although a drop down would be better than links): Tiles2 Struts Switch Locale

For information on the i18n interceptor: http://struts.apache.org/2.1.2/struts2-core/apidocs/com/opensymphony/xwork2/interceptor/I18nInterceptor.html

For the JS function, since I'm partial to jQuery I'd start with either this: http://plugins.jquery.com/project/jsper

or this...

http://plugins.jquery.com/project/Cookie

Community
  • 1
  • 1
Quaternion
  • 10,380
  • 6
  • 51
  • 102
  • No, I have declared a I18N Interceptor in struts.xml. Now I have to put Locale to cookies, for future use, because the Interceptor is working with session, but session by default is set to 30 minutes, and Locale in session it's not the best option. BUT I don't know why, I cannot set the new value for the session.setAttribute("WW_TRANS_I18N_LOCALE", "en"); (for example), when I try to set it, she remains the same as it has been set by the Interceptor. Hope now you understood :) – Denees Jul 01 '11 at 17:48
  • I don't understand why you want to hand roll your own cookie for this. The session is about maintaining state, if you want the state to exist longer then change the time for the session like so: http://www.mkyong.com/servlet/how-to-configure-the-session-timeout-in-servlet/ where are you calling session.setAttribute()? Are you using the built in I18N interceptor or did you write your own? – Quaternion Jul 01 '11 at 18:43
  • I don't want to change the session timeout, I have some reasons. The cookies was the best idea. I'm using the default Struts2 Interceptor. – Denees Jul 01 '11 at 20:34
  • Out of interest what would those be? – Quaternion Jul 02 '11 at 01:13
  • It's not so efficient to have hundred of sessions in your application, well the whole thing is that I want to improve the locales thing because of users, they don't have to select the language each time when they enter the site. – Denees Jul 02 '11 at 09:42
  • Well it's only the users that don't have the correct locale set for their OS (because the browser would pick that up and pass it along), and the users that didn't then fix the browsers locale if the OS was not correct. By efficient you surely don't mean processing, it is just a key value look up for which you'll be doing many of those just to look up the property values when processing the JSP for a locale. For efficient, you aren't grudging a few byes per session? I mean I wouldn't think anything of thousands of sessions... Thats only maybe an extra MB? My desktop system has 8 GB! – Quaternion Jul 02 '11 at 17:18
  • @hoss let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1086/discussion-between-quaternion-and-hoss) – Quaternion Jul 02 '11 at 17:18