I have an old web application made with struts 1.3 and I need to change the way the multilanguage works inside. Currently I have an action that handles the change of language this way:
this.setLocale(request, new Locale("es", "", "")); this.setLocale(request, new Locale("en", "", "")); this.setLocale(request, new Locale("fr", "", "")); this.setLocale(request, new Locale("pt", "", "")); // this is and instance of org.apache.struts.action.Action
Everything works fine, but now I'm requiered to change the language from the URL, that is, http://www.example.com/en/index.do
should show the page in English and http://www.example.com/es/index.do
would do so in Spanish.
Do you know how can I implement this?
I've been thinking in setting a servlet before the org.apache.struts.action.ActionServlet (by extending ActionServlet) that parses the URL, changes the language if necessary and then continue with super.doGet(request, response);
but the rest of the website's links will not have the language in the URL. http://www.example.com/index.do
----- Edit: I've found a partial solution for this in another answer:
I've created a filter and added it to the web.xml file
<filter>
<filter-name>LangFilter</filter-name>
<filter-class>com.company.struts.LanguageFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LangFilter</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
and a javax.servlet.Filter
class
public class LanguageFilter implements Filter {
[...]
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String requestURI = request.getRequestURI();
if (requestURI.indexOf("/context/en/") != -1) {
String newURI = requestURI.replace("/context/en/", "/");
req.getRequestDispatcher(newURI).forward(req, res);
}
else {
chain.doFilter(req, res);
}
}
}
But now I have to change all the html:form and html:link tags from
<html:link action="/test2">Old link</html:link>
to
<a href="${pageContext.request.contextPath}/${sessionScope["org.apache.struts.action.LOCALE"]}/test2.do">New link</a>
Is there a way to intercept the response generated so I can modify those *.do URIs and add them the language parameter??
Thanks.