Get user localization found by i18n interceptor in action class

I have a webapp that uses Struts 2. I am using the i18n interceptor with all default settings. I thought this interceptor worked like this:

  • If there is a request_locale parameter, remove that parameter and put that language in the session for future use.
  • If there is no NO option, find the httpheader for the locale and put that 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 tell my actions to the user's locale that an i18n interceptor has been detected if there is no explicit request parameter? Is it because it is not saved in the session?

+3


source to share


1 answer


You should not use the session key defined internally by the interceptor to get the locale of the action.

If the parameter is request_locale

found by the interceptor i18n

, the requested locale is created and placed in the action context as well as in the session under the key specified by the interceptor.

If the next interceptor request does not include a parameter request_locale

, then it gets the locale stored in the session, if not found, the locale of the request is used.



Again, the interceptor moved the language code to the action context. So, to get the locale used by struts tags you have to use

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

      


+4


source







All Articles