0

I know I can call a url with the "request_locale" parameter but that won't last long. Maybe storing it in a cookie? Which I don't know how to do.

After many hours of searching I found:

  • A low of dead links in the apache API
  • Setting the locale in session attribute "org.apache.struts.action.LOCALE"
  • ... or "request_locale"
  • ... or "request_cookie_locale"
  • ... or "WW_TRANS_I18N_LOCALE"
  • Setting the locale in ActionContext.getContext()
  • Something about interceptors that make my application throws exceptions so I don't know if I should pursue...

There's a mix of Struts 1 and Struts 2 in there which is not making things easier...

Here's what I have so far:

In the XML config:

<action name="languageSelection" class="changeSelectionAction">
         <result name="input" type="redirect">${redirectUrl}</result>
</action>

I figured I'd use "Input" to go back to the previous page after the user selected a language. The redirect variable is defined in my Action Class.

In the Web Page:

<webwork:url id="url" action="languageSelection">
      <webwork:param name="lang">fr</webwork:param>
</webwork:url>
<webwork:a href="%{url}">French</webwork:a>

This sends "lang=fr" which I can read in my Action Class.

In the Action Class, I have:

HttpServletRequest request = ServletActionContext.getRequest();
String refererUrl = ServletActionContext.getRequest().getHeader("Referer"); // gives me the source page so I can go back to it.
setRedirectUrl(refererUrl);

String[] langs = request.getParameterValues("lang"); // getting the language the user selected

... followed by a lot of garbage that doesn't work and some insults for the compiler that are commented out.

I'm not the best web developer so any basic web advice will be welcome.

Philippe Carriere
  • 3,712
  • 4
  • 25
  • 46

2 Answers2

0

You can implement a i18n interceptor which can store your locale in the session or a cookie. See https://struts.apache.org/core-developers/i18n-interceptor.html

<interceptor name="i18nCookie" class="org.apache.struts2.interceptor.I18nInterceptor"/>

<action name="someAction" class="com.examples.SomeAction">
    <interceptor-ref name="i18nCookie">
        <param name="localeStorage">cookie</param>
    </interceptor-ref>
    <interceptor-ref name="basicStack"/>
    <result name="success">good_result.ftl</result>
</action>
TheSlavMan
  • 44
  • 8
0

We use the org.apache.struts2.interceptor.I18nInterceptor in our default interceptor stack. After that struts listen to the parameter request_locale and every action that gets a ?request_locale=LANGCODE parameter will change the language for the user. Normally this setting is stored in the session of the user, so make sure user sessions are enabled too.

With the link from @TheSlavMan you can read all the possible options available for this interceptor.

Of course a simple "change language and redirect to a page"-action is possible. You are on the right path, my first guess would be to change the lang-parameter to request_locale.

beendr
  • 654
  • 7
  • 21