<message-resources parameter="com/vaannila/ApplicationResource"/>
The next step is to create ApplicationResource.properties file specific to each language.
French - ApplicationResource_fr.properties
label.welcome = J'aime Struts
Italian - ApplicationResource_it.properties
label.welcome = ti amo Struts
German - ApplicationResource_de.properties
label.welcome = Ich liebe Struts
There are two ways in which you can internationalize struts application. One is by setting the org.apache.struts.action.LOCALE to the corresponding language and the other way is to set the language preference in the browser.
In this example we will see how to internationalize Struts application by setting different values to org.apache.struts.action.LOCALE variable.
Based on the language selected by the user the corresponding method in the LocaleAction is called. LocaleAction class extends DispatchAction.
public class LocaleAction extends DispatchAction {
private final static String SUCCESS = "success";
public ActionForward english(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
session.setAttribute("org.apache.struts.action.LOCALE", Locale.ENGLISH);
return mapping.findForward(SUCCESS);
}
public ActionForward french(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
session.setAttribute("org.apache.struts.action.LOCALE", Locale.FRENCH);
return mapping.findForward(SUCCESS);
}
public ActionForward german(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
session.setAttribute("org.apache.struts.action.LOCALE", Locale.GERMAN);
return mapping.findForward(SUCCESS);
}
public ActionForward italian(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpSession session = request.getSession();
session.setAttribute("org.apache.struts.action.LOCALE", Locale.ITALIAN);
return mapping.findForward(SUCCESS);
}
}
Based on the value set in the org.apache.struts.action.LOCALE variable the corresponding ApplicationResource.properties file will be used for displaying data.
Run the application. The following page will be dispalyed to the user.
On selecting the french language the following page will be displayed.
On selecting the german language the following page will be displayed.
On selecting the italian language the following page will be displayed.
|