Struts 2 i18n

Internationalization or i18n is the process of designing a software application in such a way so that it can potentially be adapted to various languages and regions without changes.

Struts framework provides the i18n interceptor to achieve internationalization.

Struts 2 i18n example:

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
 
<html>
	<head>
		<title>Struts 2 i18n localization example.</title>
	</head>
 
	<body>
		<h3>Struts 2 i18n localization example.</h3>
 
		<s:form action="login" namespace="/user">
 
		  <s:textfield key="label.username" name="userName" />
		  <s:password key="label.password" name="password"/>	
		  <s:submit key="label.submit" 
                                     name="submit" align="center"/>
 
		</s:form>
 
		<s:url id="localeEN" namespace="/" action="locale" >
		   <s:param name="request_locale" >en</s:param>
		</s:url>
		<s:url id="localeDE" namespace="/" action="locale" >
		   <s:param name="request_locale" >de</s:param>
		</s:url>
 
		<s:a href="%{localeEN}" >English</s:a>
		<s:a href="%{localeDE}" >German</s:a>
 
	</body>
</html>

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">
 
 
 	<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>test.jsp</welcome-file>
	</welcome-file-list>
 
</web-app>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
	<constant name="struts.custom.i18n.resources" value="global" />
 
	<package name="user" namespace="/user" extends="struts-default">
 
	  <action name="login" class="com.w3spoint.action.Login">
		<result name="success">/welcome.jsp</result>
		<result name="input">/login.jsp</result>
	  </action>
 
	</package>
 
	<package name="default" namespace="/" extends="struts-default">
 
	  <action name="locale" class="com.w3spoint.action.Locale">
		<result name="success">/login.jsp</result>
	  </action>
 
	</package>
 
</struts>

Login.java

import com.opensymphony.xwork2.ActionSupport;
 
/**
 * This class is used as an action class for login process.
 * @author w3spoint
 */
public class Login extends ActionSupport {
	//data members
	private String userName;
	private String password;
 
	public String execute(){
		return SUCCESS;
	}
 
	public void validate(){
	  if(userName.trim().equals("")){
	     addFieldError("userName", getText("userName.required"));
	  }
	  if(password.trim().equals("")){
	    addFieldError("password", getText("password.required"));
	  }
	}
 
	public String getUserName() {
		return userName;
	}
 
	public void setUserName(String userName) {
		this.userName = userName;
	}
 
	public String getPassword() {
		return password;
	}
 
	public void setPassword(String password) {
		this.password = password;
	}	
}

Locale.java

import com.opensymphony.xwork2.ActionSupport;
 
/**
 * This class is used as an action class for locale.
 * @author w3spoint
 */
public class Locale extends ActionSupport{
	public String execute(){
		return SUCCESS;
	}
}

global.properties

label.username = UserName
label.password = Password
label.submit = Submit

global_de.properties

label.username = Benutzername
label.password = Kennwort
label.submit = Einreichen

login.properties

userName.required = Username is required.
password.required = Password is required.

login_de.properties

userName.required = Benutzername ist erforderlich.
password.required = Passwort ist erforderlich.

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="ISO-8859-1" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
 
<html>
	<head>
		<title>Struts 2 i18n localization example.</title>
	</head>
	<body>
		<h3>Hello <s:property value="userName"/></h3>
	</body>
</html>

Output:

struts 23 first   Click on German link. struts 23 second   Download this example.   Next Topic: Struts 2 Zero Configuration by convention approach with example. Previous Topic: Struts 2 url validator with example.

 

Please follow and like us:
Content Protection by DMCA.com