Struts 2 I18N (Struts 2 Interationalization)

Struts 2 I18N Example

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.FilterDispatcher</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>

struts.xml

<!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="langproperties" />
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
        <action name="language" >
            <result>index.jsp</result>
        </action>
        <action name="registerForm" >
            <result>registerForm.jsp</result>
        </action>
        <action name="loginForm" >
            <result>loginForm.jsp</result>
        </action>
        <action name="register" class="action.RegisterActoin" method="register" >
            <result name="success">loginForm.jsp</result>
        </action>
        <action name="login" class="action.LoginActoin" method="login" >
            <result name="success">loginSuccess.jsp</result>
        </action>
    </package>
</struts>

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>Struts2 Localization/Internationalization-codesstore.blogspot.com</title>
</head>
<body>
    <s:include value="languageBar.jsp" />
    <s:include value="menuBar.jsp" />

</body>
</html>

languageBar.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
<div>Languages : 
    <s:a href="language.action?request_locale=en">English </s:a> | 
    <s:a href="language.action?request_locale=sin">Sinhalese</s:a>
    <hr>
</div>
       

menuBar.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
<div>
    <s:a href="loginForm.action">Login</s:a> | 
    <s:a href="registerForm.action">Register</s:a>
    <hr>
</div>
      

loginForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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>Struts2 Localization/Internationalization-codesstore.blogspot.com</title>
</head>
<body>
    <s:include value="languageBar.jsp" />
    <s:include value="menuBar.jsp" />
    
    <s:form action="login" method="post">
        <s:textfield key="label.username" name="username"/>
        <s:password key="label.password" name="password"/>
        <s:submit key="label.login"/>
    </s:form>

</body>
</html>

registerForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!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>Struts2 Localization/Internationalization-codesstore.blogspot.com</title>
</head>
<body>
    <s:include value="languageBar.jsp" />
    <s:include value="menuBar.jsp" />
    
    <s:form action="register" method="post">
        <s:textfield key="label.username" name="username"/>
        <s:password key="label.password" name="password"/>
        <s:password key="label.passwordretype" name="passwordretype"/>
        <s:textfield key="label.email" name="email"/>
        <s:submit key="label.register"/>
    </s:form>

</body>
</html>

loginSuccess.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!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>Struts2 Localization/Internationalization-codesstore.blogspot.com</title>
</head>
<body>
    <s:include value="languageBar.jsp" />
    <s:include value="menuBar.jsp" />
    <s:actionmessage/>
</body>
</html>

langproperties.properties

label.username=Username
label.password=Password
label.login=Login
label.passwordretype=Retype Password
label.email=Email
label.register=Register
message.loginSucess=Welcome to system

langproperties_sin.properties

label.username=Nama
label.password=Murapadhaya
label.login=Logwenna
label.passwordretype=Murapadhaya Nawathath
label.email=Email
label.register=Liyapadhinchi
message.loginSucess=Sadaren piliganimu

LoginAction.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginActoin extends ActionSupport {

    public String login() {
        addActionMessage(getText("message.loginSucess"));
        return SUCCESS;
    }
    
}

RegisterAction.java

package action;

import com.opensymphony.xwork2.ActionSupport;

public class RegisterActoin extends ActionSupport {

    public String register() {
        return SUCCESS;
    }
    
}

Project structure in eclipse
Struts 2 Interationalization

2 comments:

  1. Hi Sameera,

    I went through your codes.But this is not converting to sinhala.Are there any additional things to do ? I did this example with adding unicodes to sinhala property file,then perfectly worked.

    http://tlt.its.psu.edu/suggestions/international/bylanguage/sinhalachart.html

    anyway thank you very much for your example.It was great helpful to me.

    Thanks
    Isuru.

    ReplyDelete
  2. Hi Sameera,

    Can you please explain select tag with internationalization with an example.

    Thanks,
    Manoj

    ReplyDelete