Struts Internationalization

In this article, you will learn how to implement Internationalization (I18N) in Struts. Internationalization in struts can be used when you want to support multiple languages, currency, date and time to your web sites. Internationalization is a process of designing the software to support multiple languages. For support Internationalization you have to create property files for all the locales which you want support.

First create a new Dynamic Web Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.2</version>
      <scope>test</scope>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-core</artifactId>
      <version>1.3.10</version>
    </dependency>
  
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-taglib</artifactId>
      <version>1.3.10</version>
    </dependency>
     
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts-extras</artifactId>
      <version>1.3.10</version>
    </dependency>
</dependencies>

1. Create Action

Create a class LocaleAction.java which extends org.apache.struts.actions.DispatchAction and write methods to execute your business logic.

LoginForm.java

package com.kruders.action;
 
import java.util.Locale;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
 
public class LocaleAction extends DispatchAction{
     
    public ActionForward english(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
         
        request.getSession().setAttribute(
                Globals.LOCALE_KEY, Locale.ENGLISH);
         
        return mapping.findForward("success");
    }
     
    public ActionForward german(ActionMapping mapping,ActionForm form,
            HttpServletRequest request,HttpServletResponse response) 
    throws Exception {
         
        request.getSession().setAttribute(
                Globals.LOCALE_KEY, Locale.GERMAN);
         
        return mapping.findForward("success");
    }
}

2. Create ActionMapping

Next we create an action mapping for this action handler.

<action
    path="/changeLocale"
    type="com.kruders.action.LocaleAction"
    parameter="method"
    validate="false">
    <forward name="success" path="/locale.jsp"/>
</action>

3. Create JSP Page

Now create the jsp page that delegates requests to different jsp pages.

locale.jsp

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Login Page</title>
</head>
<body>
<bean:message key="label.selectlanguage" />     
<html:link page="/changeLocale.do?method=english">English</html:link>
<html:link page="/changeLocale.do?method=german">German</html:link><br>
<bean:message key="label.message" />     
</body>
</html>

4. Properties File

Create ApplicationResources.properties for English and write the following text.

label.selectlanguage = Select Language
label.message = Hello World...


Create ApplicationResources_de.properties for German and write the following text.

label.selectlanguage = Wahlen Sie Sprache
label.message = Hallo Welt ...

5. Struts Config

struts-config.xml file contains the following code.

<global-forwards>
    <forward name="locale" path="/locale.do"/>
</global-forwards>
<action-mappings>
 
    <action
        path="/locale"
        type="org.apache.struts.actions.ForwardAction"
        parameter="/locale.jsp"/>
     
    <action
        path="/changeLocale"
        type="com.kruders.action.LocaleAction"
        parameter="method"
        validate="false">
        <forward name="success" path="/locale.jsp"/>
    </action>
     
</action-mappings>
 
<message-resources parameter="ApplicationResources" />

6. Run Project

Now when you run the project, following screen will be displayed as shown in Figure 8.1

Figure 8.1 Figure 8.1


Now when you click on German link, following screen will be displayed as shown in Figure 8.2

Figure 8.2 Figure 8.2

The folder structure of the example is shown below in Figure 8.3

Figure 8.3 Figure 8.3

You can download the source code of this example here.

No comments yet.

Leave a Reply