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