DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Internationalizing Struts Application From JSP Page Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · · Tutorial
Like (0)
Save
Tweet
35.21K Views

Join the DZone community and get the full member experience.

Join For Free

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

application

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 6 Things Startups Can Do to Avoid Tech Debt
  • 10 Steps to Become an Outstanding Java Developer
  • Ultra-Fast Microservices: When Microstream Meets Wildfly
  • How to Submit a Post to DZone

Comments

Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo