3

I have a webapp that uses Struts 2. I use the i18n interceptor with all the default settings. I thought this interceptor worked as follow:

  1. If there is a parameter request_locale, remove this parameter and place this locale in the session for future use.
  2. If there is NO parameter, search the httpheader for the locale and place this locale in the session for future use.

So in my action class (which implements sessionAware) I have the following method:

public String getUserLocale()
{
    return (String) session.get("WW_TRANS_I18N_LOCALE");
}

However, this method does not work in situation #2, it just returns null. So my question is: how can I let my action know the user locale that the i18n interceptor detected if there is no explicit request parameter? Because it is not stored in the session?

Roman C
  • 49,761
  • 33
  • 66
  • 176
user1884155
  • 3,616
  • 4
  • 55
  • 108

1 Answers1

5

You shouldn't use a session key defined by the interceptor for internal usage to get an action locale.

If a request_locale parameter is detected by the i18n interceptor the requested locale is created and put to the action context and also to the session under the key specified by the interceptor.

If the next request to the interceptor doesn't include request_locale parameter then it gets a locale saved in the session, if not found the request locale is used.

Again the interceptor put returned locale to the action context. So, to get a locale used by the struts tags you should use

 Locale locale = ActionContext.getContext().getLocale(); 

Roman C
  • 49,761
  • 33
  • 66
  • 176