Reputation: 69
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?
Upvotes: 2
Views: 827
Reputation: 1
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.
Upvotes: 2