2

I am new to Struts 2 and i am working on migrating Struts 1 code to Struts 2, where i have the scenario like, Action Servlet is extended and in the extended class "process" method, locale is been set in request as shown below,

public class TestServlet extends ActionServlet  {

    protected void process(HttpServletRequest request, HttpServletResponse response)
                    throws IOException, ServletException {
            Locale locale = Locale.ENGLISH;
        locale = Locale.FRENCH;
        request.setAttribute("_locales", locale);

        super.process(request, response);

    }

}

How can i migrate this change to work in struts 2. What is the equivalent approach for this in Struts 2?

Roman C
  • 49,761
  • 33
  • 66
  • 176
CMG
  • 69
  • 6

1 Answers1

2

In Struts2 you no longer need an actionservlet nor requestprocessor, everything is done via i18n interceptor.

Hope you'll find help in this answer.

The Struts2 internationalization interceptor i18n could be used to dynamically change the current user locale to the user specific locale for the user's session.

"Or, alternatively, only for the current request (since XWork 2.1.3)"

by making an HTTP request and supplying a request parameter request_locale with the value of the locale like "en_US" which create a locale for English, US.

This locale is saved in the session by default under "WW_TRANS_I18N_LOCALE" attribute and used as a current locale during the user session. The current locale is also pushed into the ActionContext map by this interceptor upon every request. This allows framework components that support localization all utilize the ActionContext's locale.


Also if you want to learn how to get the current locale you should read my answer for Get user locale detected by i18n interceptor in action class.

Community
  • 1
  • 1
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • do i need to configure the i18n interceptor in struts.xml file for every action OR is it automatically enabled for all the actions? and how it will set only locales "en" and "fr" into session? – CMG Jun 11 '16 at 20:26
  • Do you have another question? You can post it as a new question, a comments section is for clarifying the current question or answer. Wen you have enough reputation you can post comments on any question or answer. Don't forget to accept the answer if it helped you to solve your problem or you get learned by reading it. – Roman C Jun 12 '16 at 07:50
  • Because it's rarely upvoted here if you find this answer helpful please don't hesitate to upvote it. – Roman C Jun 24 '16 at 19:52