Internationalization also known as I18N is used for displaying content specific to the locale based on their languages, currencies and formatting conventions.Resource bundle is the file that contains the key/value pairs for the default language of your application. The naming format for this file is
bundlename_language_country_variant.properties
For example, if you have a bundle named ApplicationResource for the English language in the Unites States for the Windows platform, then your properties file name would be
ApplicationResource_en_US_WIN.properties.
Lets take a senario in which the user can see the same page in four different languages like English, French, German and Italian. The jsp page gets displayed according to the language selected by the user.Our default resource bundle is ApplicationResource.properties file.
Directory Structure of Internationalization Example in Struts 2.0 Using MyEclipse IDE
index.jsp
<%@taglib uri="/struts-tags" prefix="s"%>
<s:form action="register">
<s:textfield name="name" label="Name"/>
<s:password name="password" label="Password"/>
<s:textfield name="mailId" label="MailId"/>
<s:submit value="Register"/>
</s:form>
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>f1</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>f1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="register" class="mypack.RegisterAction">
<result name="success">/hello.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
RegisterAction.java
package mypack;
import com.opensymphony.xwork2.ActionSupport;
public class RegisterAction extends ActionSupport {
private String name,password,mailId;
@Override
public void validate(){
if(name.length()==0)
this.addFieldError("name",getText("name.required"));
if(password.length()==0)
this.addFieldError("password",getText("password.required"));
if(password.length()<5)
this.addFieldError("password",getText("password.length"));
}
public String execute(){
return "success";
}
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;
}
public String getMailId() {
return mailId;
}
public void setMailId(String mailId) {
this.mailId = mailId;
}
}
RegisterAction_en.properties
name.required=Name is required
password.required=Password is required
password.length=Password must contain at least 5 character
RegisterAction_hi.properties
name.required=Nameam ke bina kam nahi chalaga
password.required=Password ke bina nahi chalaga
password.length=Password kam se kam 5 character hona chahiye
hello.jsp
<%@ taglib uri="/struts-tags" prefix="s"%>
<b>You are successfully registered
Output
