Internationalization in Struts2

The Struts2 provides feature t for web application can created for global use. The web content will be changed according to the language selection by the user at run time. To implement the above concept, Struts2 Internationalization and localization is used. Internationalization is known i18n and localization is known l10n.The main purpose of localization is to translate web content region specific information in the application into corresponding language and country code which is unique.

ISO Language code and Country Codes

Each language and country has unique language code and country code which is assigned by ISO (International Organization for Standardization). ISO is the network of the national standard institutes of various countries that define common standards for all the member countries.

Below is the list of some country code and their language code -
Country Name Country Code Language Language Code
ItelyITItalianit
ChinaCNChinesezh
FranceFRFrenchfr
GermanyDEGermande
IndiaINHindihi
JapanJPJapaneseja
SpainESSpanishes
United States US Englishen

A locale is an identifier and represents the local details of the user to the application. The local can be created by java.util.Locale class. When a user session begins, the Locale object is sent in the HTTP request as parameter.

Lets demostrate Internationalization in Struts2.
I created home page and there are three links for different languages like - English, German and French. By default all the content will display in English and content will be change according to the language selection by the user.

Follow the following steps to create this project :

Step 1: Create a Dynamic Web Project in eclipse - "Struts2I18N".
Step 2: Edit deployment descriptor web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Struts2I18N</display-name>
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

Step 3: Copy required Struts2 jar files in WEB-INFlib folder in your project :
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.8.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.1.jar
xwork-core-2.3.4.1.jar


Step 4: Create a View page index.jsp :
<%taglib uri="/struts-tags" prefix="s" %>
<html>
  <head>
    <title><s:text name="app.title"/></title>
     </head>
  <body> 
  <table align="center" width=100%>
  <tr>
  <td>
  <h2 align="center"><s:text name="app.head"/></h2>
  </td>
  <td align="right">
   <s:text name="app.language"/> :
   <a href="home.action?request_locale=de">German</a> |
   <a href="home.action?request_locale=en">English</a> |  
     <a href="home.action?request_locale=fr">French</a> 
   </td>
</tr>
<tr>
<td colspan="2"><hr></td>
</tr>
<tr>
<td colspan="2">
  <b><s:text name="app.message"/></b>
    </td>    
    </tr>
    <tr>
    <td colspan="2" align="center">
  <hr>
  <s:text name="app.footer"/>
  </td></tr>
  </table>  
  </body>
</html>

Step 5: Create action class HomeAction.java :
package techmyguru.actions;
import com.opensymphony.xwork2.ActionSupport;
public class HomeAction extends ActionSupport 
{
      public String execute() throws Exception 
      {
  return SUCCESS;
  }
}

Step 6: Create resource bundeled files
ResourceBundle_en.properties

app.title=Struts 2 I18N demo
app.head=Struts 2 Internationalization demo
app.language=Select Language
app.message=In a Struts 2 web application you may associate a message resource property file with each Struts 2 Action class by creating a properties file with the same name as the Action class and having the .properties extension. This properties file must go in the same package as the Action class. For our tutorial example, let's say we want to place the form field labels into a separate file where we can easily change them and also provide the capability to display the labels in other languages.
app.footer=Copyright message of this content
app.logout=Logout

ResourceBundle_de.properties
app.title=Struts 2 I18N demoö
app.head=Struts 2 Internationalisierung Demo
app.language=Sprache auswählen
app.message=In einer Struts 2 Web-Anwendung können Sie eine Nachricht Ressourceneigenschaft Datei mit jedem Struts 2 Action-Klasse durch Erstellen einer Properties-Datei mit dem gleichen Namen wie die Action-Klasse und mit der. Erweiterung Eigenschaften zuzuordnen. Diese Eigenschaften Datei muss sich im selben Paket wie die Action-Klasse zu gehen. Für unser Tutorial Beispiel, sagen wir, wir wollen die Form Feld Etiketten in einer separaten Datei zu platzieren, wo wir leicht ändern und sie bieten auch die Möglichkeit, die Etiketten in anderen Sprachen anzuzeigen.
app.footer=Urheberrecht Botschaft dieser Inhalte
app.logout=Abmelden

ResourceBundle_fr.properties
app.title=Montants 2 démonstration I18N
app.head=Montants 2 démonstration d'Internationalisation
app.language=Choisissez la Langue
app.message=Dans des Montants 2 application de web vous pouvez associer un dossier de propriété de ressource de message à chacun les Montants 2 classe d'Action en créant un dossier de propriétés avec le même nom que la classe d'Action et en ayant l'extension .properties. Ce dossier de propriétés doit entrer dans le même paquet que la classe d'Action. Pour notre exemple de classe de travaux dirigés, disons que nous voulons placer les étiquettes de champ de forme dans un dossier séparé où nous pouvons facilement les changer et fournir aussi la capacité d'afficher les étiquettes dans d'autres langues.
app.footer=Message de copyright de ce contenu
app.logout=Logout

Step 7: Create struts.properties
struts.properties
struts.custom.i18n.resources=techmyguru.actions.ResourceBundle


Step 8: Configure Action in struts.xml file :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="devMode" value="true" />
<package name="default" extends="struts-default">
<action name="home" class="techmyguru.actions.HomeAction">
  <result name="success">index.jsp</result>
</action>
</package>
</struts>


Step 9: Verify the project directory structure.
Struts2 Internationalization example

Step 10: Run the project and get output.
Struts2 Internationalization example

Select language German and get below output -
Struts2 Internationalization example

Select language French and get below output -
Struts2 Internationalization example

Previous Download Source Code Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code