Monday, October 8, 2012

struts 1x i18n internationalization tutorial with example program(Using Link)

This struts tutorial explains how to archive internationalization(i18n) without using or modifying any browser setting, Here we have loaded four different languages English, France, Germany and  Italy, by clicking a link in the webpage languages can be changed.
Controller class will load the language for the  application based on their user request
Project Structure:
JAR Files: 
TLD Files:

Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from

JAR FILES + TLD FILES

Model class which handles data from view page
Form.java:



package com.candid;

public class Form extends org.apache.struts.action.ActionForm {

 private static final long serialVersionUID = 1L;

 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

}
Controller class which loads language based on user action

Inter_WebPage.java:

package com.candid;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession; //import org.apache.struts.actions.DispatchAction;
import org.apache.struts.actions.LookupDispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author balamurugan@candidjava.com
 */
/*
 * our program is Loopup Dispatch Action so class must extends
 * LookupDispatchAction
 */
public class Inter_WebPage extends LookupDispatchAction {

 /* forward name="success" path="" */
 private final static String SUCCESS = "success";

 protected Map getKeyMethodMap() {
  Map map = new HashMap();
  map.put("label.submit", "submit");
  map.put("label.language.english", "english");
  map.put("label.language.italian", "italian");
  map.put("label.language.german", "german");
  map.put("label.language.french", "french");

  return map;
 }

 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);
 }

 public ActionForward submit(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {

  return mapping.findForward("output");
 }

}

ApplicationResource_de.properties:



# To change this template, choose Tools | Templates
# and open the template in the editor.

label.string1 = Geben ihren Namen.
label.string2=Willkommen.
label.language.english=english
label.language.italian=italiano
label.language.german=deutsch
label.language.french=francaise

label.submit=Einreichen.

ApplicationResource_fr.properties:



# To change this template, choose Tools | Templates
# and open the template in the editor.

label.string1 = Entrez votre nom.
label.string2=Bienvenue.
label.language.english=english
label.language.italian=italiano
label.language.german=deutsch
label.language.french=francaise

label.submit=Soumettre des.

ApplicationResource_it.properties:



# To change this template, choose Tools | Templates
# and open the template in the editor.

label.string1 = Inserisci il tuo nome.
label.string2=Benvenuto.
label.language.english=english
label.language.italian=italiano
label.language.german=deutsch
label.language.french=francaise

label.submit=presentare

ApplicationResource.properties:



# -- standard errors --
errors.header=<UL>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</UL>
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.
# -- other --
errors.cancel=Operation cancelled.
errors.detail={0}
errors.general=The process did not complete. Details should follow.
errors.token=Request could not be completed. Operation is not in sequence.
# -- welcome --
welcome.title=Struts Application
welcome.heading=Struts Applications in Netbeans!
welcome.message=It's easy to create Struts applications with NetBeans.

label.string1 = Enter your name
label.string2=  welcome
label.language.english=english
label.language.italian=italian
label.language.german=german
label.language.french=french

label.submit=submit

web.xml:



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <init-param>
   <param-name>debug</param-name>
   <param-value>2</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>2</param-value>
  </init-param>
  <load-on-startup>2</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>30</session-timeout>
 </session-config>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <jsp-config>
  <taglib>
   <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-nested.tld</taglib-location>
  </taglib>
  <taglib>
   <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri>
   <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
  </taglib>
 </jsp-config>
</web-app>

struts-config.xml:



<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
 <form-beans>
  <form-bean name="form1" type="com.candid.Form"></form-bean>
 </form-beans>

 <action-mappings>
  <action path="/changeLocale" name="form1" parameter="method"
   type="com.candid.Inter_WebPage">
   <forward name="success" path="/index.jsp" />
   <forward name="output" path="/output.jsp"></forward>
  </action>
  <action path="/Welcome" forward="/welcomeStruts.jsp" />
 </action-mappings>
 <message-resources parameter="com/candid/ApplicationResource" />

</struts-config> 
 
Index page Provides a link to change language

index.jsp:



<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html>
<head>
<title>Struts - I18N</title>
</head>
<body>
<html:form action="changeLocale">
 <table>
  <tr>
   <td>Select your language : <a
    href="changeLocale.do?method=<bean:message key="label.language.english" />"><bean:message
    key="label.language.english" /></a> <a
    href="changeLocale.do?method=<bean:message key="label.language.french" />"><bean:message
    key="label.language.french" /></a> <a
    href="changeLocale.do?method=<bean:message key="label.language.german" />"><bean:message
    key="label.language.german" /></a> <a
    href="changeLocale.do?method=<bean:message key="label.language.italian" />"><bean:message
    key="label.language.italian" /></a></td>
  </tr>
  <tr>
   <td><bean:message key="label.string1" /></td>
   <td><html:text property="name"></html:text>
  </tr>
  <tr>
   <td><html:submit property="method">
    <bean:message key="label.submit" />
   </html:submit></td>
  </tr>
 </table>
</html:form>
</body>
</html>

output.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>output</title>
</head>
<body>
<table>
 <tr>
  <td><bean:message key="label.string2" /></td>
  <TD><bean:write name="form1" property="name" /></TD>
 </tr>
</table>
</body>
</html> 
 




Screenshot for internationalization

OUTPUT:    

      

                                                                                                                
DOWNLOAD+SOURCE CODE

1 comments:

Java is high level programming language .Due to its stability and scalability, you can find Java on mobiles, desktops, large scale applications etc. Java is also gaining big in the field of Internet of Things (IoT).

Spring 4 mvc hello world example

Post a Comment