Search code examples
sessionjakarta-eestruts2url-rewritinglocale

Struts2 locale session rewrite


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 ..


Solution

  • 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