Home > apache struts2, locale si, localization > Internationalization (i18n) your Struts application

Internationalization (i18n) your Struts application

A web application can be viewed by different languages. This post is about how to convert a web application into sinhala using Struts 2 framework. 
Apache Struts is a MVC framework for J2EE development. 
add the following jars to webcontent/WEB-INF/lib folder. These are the jars from struts and commons loggings.
Create two packages named org.apache.struts.register.action and org.apache.struts.register.model.Person in src folder. Then create Person.java class in the model folder.
package org.apache.struts.register.model;

public class Person {

private String firstName;
 private String lastName;
 private String email;
 private int age;

public String getFirstName() {
 return firstName;

public void setFirstName(String firstName) {
 this.firstName = firstName;

public String getLastName() {
 return lastName;

public void setLastName(String lastName) {
 this.lastName = lastName;

public String getEmail() {
 return email;

public void setEmail(String email) {
 this.email = email;

public int getAge() {
 return age;

public void setAge(int age) {
 this.age = age;

public String toString() {
 return "First Name: " + getFirstName() + " Last Name:" + getLastName()
 + " Email: " + getEmail() + " Age: " + getAge();

Now add Register action into action package.

package org.apache.struts.register.action;

import org.apache.struts.register.model.Person;

import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {

private static final long serialVersionUID = 1L;

private Person personBean;

public String execute() throws Exception {

return SUCCESS;

public Person getPersonBean() {
 return personBean;

public void setPersonBean(Person personBean) {
 this.personBean = personBean;

public void validate() {
 if (personBean.getFirstName().length() == 0) {
 addFieldError("personBean.firstName", "First name is required");
 if (personBean.getEmail().length() == 0) {
 addFieldError("personName.email", "Email is required");
 if (personBean.getAge() < 18) {
 "Age is required and must be 18 or older");

Now create struts.xml in the src folder.

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

 <constant name="struts.devMode" value="true"></constant>
 <package name="basicstruts2" extends="struts-default">
 <action name="index">

 <action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
 <result name="success">/HelloWorld.jsp</result>
 <action name="strutdemo" class="org.apache.struts.helloworld.action.StrutsAction" method="carry">
 <result name="success">/StrutDemo.jsp</result>
 <action name="register" class="org.apache.struts.register.action.Register" method="execute">
 <result name="success">/thankyou.jsp</result>
 <result name="input">/register.jsp</result>
 <action name="registerInput" class="org.apache.struts.register.action.Register" method="input">
 <result name="input">/register.jsp</result>

Then add log4j.xml

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

 <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
 <layout class="org.apache.log4j.PatternLayout">
 <param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n">

 <!-- specify the logging level for loggers from other libraries -->
 <logger name="com.opensymphony">
 <level value="DEBUG"></level>

 <logger name="org.apache.struts2">
 <level value="DEBUG"></level>

 <!-- for all other loggers log only debug and above log messages -->
 <priority value="INFO"></priority>
 <appender-ref ref="STDOUT">


 and add properties files to action packages.
01. Register.properties file. This is the default locale file.

personBean.firstName=First name
personBean.lastName=Last name
thankyou=Thank you for registering %{personBean.firstName}

02. Register_si.properties file – which contains sinhala content.

welcome=u0DBDu0DD2u0DBAu0DCFu0DB4u0DAFu0DD2u0D82u0DA0u0DD2 u0DC0u0DB1u0DCAu0DB1
personBean.firstName=u0DB8u0DD4u0DBDu0DCA u0DB1u0DB8
personBean.email=u0DC0u0DD2u0DAFu0DCAu200Du0DBAu0DD4u0DADu0DCA u0DADu0DD0u0DB4u0DD1u0DBD

This file contains the relevant UNICODE characters for the sinhala content.

Then the index.jsp page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@ taglib prefix="s" uri="/struts-tags" %>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Basic Struts 2 Application - Welcome</title>

<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'></a>">Hello World</a></p>
<p><a href="<s:url action='strutdemo'></a>">Strut Demo</a></p>
<s:url action="strutdemo" var="helloLink">
 <s:param name="userName">Bruce&Phillips</s:param>
<p><a href="${helloLink}">Hello Bruce Phillipse</a></p>

<p>Get your own personal hello by submittng this form</p>
<s:form action="hello">
 <s:textfield name="userName" label="Your Name"></s:textfield>
 <s:submit value="Submit"></s:submit>
<s:url action="registerInput" var="registerInputLinkSI">
 <s:param name="request_locale">si</s:param>

<p><a href="${registerInputLinkSI}">Por favor, regístrese</a> para nuestro sorteo</p>

register.jsp page. In this page content type should be set to UTF-8.

<%@ taglib prefix="s" uri="/struts-tags" %>
 <%@ page language="java" contentType="text/html;charset=UTF-8" %>
 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<h3><s:text name="welcome"></s:text></h3>
 <s:form action="register">

<s:textfield key="personBean.firstName"></s:textfield>
 <s:textfield key="personBean.lastName"></s:textfield>
 <s:textfield key="personBean.email"></s:textfield>
 <s:textfield key="personBean.age"></s:textfield>
and finally web.xml file

<!--?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_3_0.xsd" id="WebApp_ID" version="3.0">


The output is 
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: