Internationalization

Internationalization

Previous Home Next

 

Internationalization or I18N is the way of designing the applications which support multiple languages and regions. It is used to display contents based on their languages, currencies and formatting conventions, So that user can use same applications in specified languages or countries.

 
There are two main i18n components in struts framework :-
  • First of components which is managed by application Controller is a message class that references a resource bundle containing Locale dependent controller.
  • Second components is a jsp custom tag which is used in view layer to present the actual string managed by the controller.
The standard method used when internationalizing a struts application begin with the creation of a set of simple java properties file.Each file contain key/value pair for each message that you expect your application to present ,in the language appropriate for the requesting client.


Directory Structure of InternationalizationExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Internationalization Example</title>
</head>
<body>
<h3>Internationalization Example</h3>
<ul>
<li><html:link page="/inter.do?method=chinese">Chinese Language</html:link></li>
<li><html:link page="/inter.do?method=english">English Language</html:link></li>
<li><html:link page="/inter.do?method=france">France Language</html:link></li>
</ul>
</body>
</html>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Maven Struts Examples</display-name> 
  <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>
    <load-on-startup>1</load-on-startup>
  </servlet> 
  <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping> 
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
	<form-beans>
		<form-bean
			name="internationalizationForm"
			type="org.r4r.struts.InternationalizationForm"/>
	</form-beans>
	<action-mappings>
		<action
			path="/inter"
			type="org.r4r.struts.InternationalizationAction"
			name="internationalizationForm"
			parameter="method"
			validate="false"
			>
			<forward name="english" path="/english.jsp"/>
			<forward name="chinese" path="/chinese.jsp"/>
			<forward name="france" path="/france.jsp"/>
		</action>
	</action-mappings>
	<message-resources
		parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

InternationalizationForm.java

package org.r4r.struts;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

@SuppressWarnings("serial")
public class InternationalizationForm extends ActionForm{	
	private String name;
	private String password;	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
	ActionErrors errors = new ActionErrors();
	if( getName() == null || ("".equals(getName())))
	    {
	       errors.add("error.name",
	    		   new ActionMessage("error.name"));
	    }
	if( getPassword() == null || ("".equals(getPassword())))
	    {
	       errors.add("error.password",
	    		   new ActionMessage("error.password"));
	    }
	return errors;
	}
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// reset properties
		name = "";
		password = "";
	}	
}

InternationalizationAction.java

package org.r4r.struts;

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 InternationalizationAction extends DispatchAction{	
	public ActionForward chinese(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
	throws Exception {		
		request.getSession().setAttribute(
		Globals.LOCALE_KEY, Locale.SIMPLIFIED_CHINESE);
		return mapping.findForward("chinese");
	}
	public ActionForward english(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
	throws Exception {
		request.getSession().setAttribute(
		Globals.LOCALE_KEY, Locale.ENGLISH);
		return mapping.findForward("english");
	}
	public ActionForward france(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
	throws Exception {
		request.getSession().setAttribute(
		Globals.LOCALE_KEY, Locale.FRANCE);
		return mapping.findForward("france");
	}	
}

ApplicationResources.properties

#error message
error.name = Username is required
error.password = Password is required

#label message
label.title=Internationalization Example
label.header = Internationalization Example
label.name = Username 
label.password = Password
label.submit = Submit

ApplicationResources_zh_CN.properties

#SIMPLIFIED_CHINESE

#error message
error.name = 请输入用户名
error.password = 请输入密码

#label message
label.title=本地化例子
label.header = 本地化例子
label.name = 用户名
label.password = 密码
label.submit = 提交

ApplicationResources_fr.properties

#error message
error.name = Nom d'utilisateur est nécessaire
error.password = Mot de passe est requis

#label message
label.title=par exemple la localisation
label.header = par exemple la localisation
label.name = Nom d'utilisateur 
label.password = Mot de passe
label.submit = Soumettre

english.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><bean:message key="label.title" /></title>
</head>
<body>
<h3><bean:message key="label.header" /></h3>
<html:messages id="errorName" property="error.name">
<div style="color:red"><bean:write name="errorName" /></div>
</html:messages>
<html:messages id="errorPassword" property="error.password">
<div style="color:red"><bean:write name="errorPassword" /></div>
</html:messages>
<html:form action="/english">
<bean:message key="label.name" /> : <html:text property="name" /><br/>
<bean:message key="label.password" /> : <html:text property="password" /><br/>
<html:submit><bean:message key="label.submit" /></html:submit>
</html:form>
</body>
</html>

chinese.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><bean:message key="label.title" /></title>
</head>
<body>
<h3><bean:message key="label.header" /></h3>
<html:messages id="errorName" property="error.name">
<div style="color:red"><bean:write name="errorName" /></div>
</html:messages>
<html:messages id="errorPassword" property="error.password">
<div style="color:red"><bean:write name="errorPassword" /></div>
</html:messages>
<html:form action="/chinese">
<bean:message key="label.name" /> : <html:text property="name" /><br/>
<bean:message key="label.password" /> : <html:text property="password" /><br/>
<html:submit><bean:message key="label.submit" /></html:submit>
</html:form>
</body>
</html>

france.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><bean:message key="label.title" /></title>
</head>
<body>
<h3><bean:message key="label.header" /></h3>
<html:messages id="errorName" property="error.name">
<div style="color:red"><bean:write name="errorName" /></div>
</html:messages>
<html:messages id="errorPassword" property="error.password">
<div style="color:red"><bean:write name="errorPassword" /></div>
</html:messages>
<html:form action="/france">
<bean:message key="label.name" /> : <html:text property="name" /><br/>
<bean:message key="label.password" /> : <html:text property="password" /><br/>
<html:submit><bean:message key="label.submit" /></html:submit>
</html:form>
</body>
</html>

Output




Previous Home Next