Meyyappan has posted 101 posts at DZone. View Full User Profile

Internationalizing Struts Application From JSP Page Tutorial

06.15.2012
| 15830 views |
  • submit to reddit

Internationalization also known as I18N is used for displaying content specific to the locale based on their languages, currencies and formatting conventions.

Resource bundle is the file that contains the key/value pairs for the default language of your application. The naming format for this file is

bundlename_language_country_variant.properties

For example, if you have a bundle named ApplicationResource for the English language in the Unites States for the Windows platform, then your properties file name would be ApplicationResource_en_US_WIN.properties.

Lets take a senario in which the user can see the same page in four different languages like English, French, German and Italian. The jsp page gets displayed according to the language selected by the user.

Our default resource bundle is ApplicationResource.properties file. To link the resouce bundle with the application the following tag should be added in the struts-config.xml file.

<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.

You can download the source code of the Struts Internationalization example by clicking on the Download link below.

Source: Download

Source + Lib: Download

Published at DZone with permission of its author, Meyyappan Muthuraman.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: