Internalization in struts

Here I am explaining how to do Internalization in struts .

struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
<form-beans>
<form-bean name="Iform" type="com.Internalization.InternalizeForm"/>
</form-beans>
<action-mappings>
<action name="Iform"
path="/internalize"
type="com.Internalization.InternalizeAction"
scope="session"
input="Internalize.jsp">
<forward name="success" path="/Internalization.jsp" />
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
<message-resources parameter="ApplicationResources_de_DE" />
<message-resources parameter="ApplicationResources_sv_SE" />
<message-resources parameter="ApplicationResources_fr_FR" />

</struts-config>

web.xml

<web-app>
<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>2</load-on-startup>
</servlet>
<welcome-file-list>
<welcome-file>Internalize.jsp</welcome-file>
</welcome-file-list>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

<taglib>
<taglib-uri>/tags/struts-html</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/tags/struts-bean</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
</web-app>

InternalizeForm.java


package com.Internalization;

import org.apache.struts.action.ActionForm;

public class InternalizeForm extends ActionForm{

public String lang;

public String getLang() {
return lang;
}

public void setLang(String lang) {
this.lang = lang;
}

}

InternalizeAction.java

package com.Internalization;

import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class InternalizeAction extends Action {

public ActionForward execute(ActionMapping map,ActionForm form,HttpServletRequest req,HttpServletResponse res)throws Exception
{

String lang = req.getParameter("lang");
if (lang == null) {
lang="English";
}else {
ResourceBundle bundle=null;
Locale locale=null;

if (lang.equals("German")) {
locale=Locale.GERMANY;
} else if (lang.equals("French")) {
locale=Locale.FRANCE;
} else if (lang.equals("Swedish")) {
locale=new Locale("sv","SE");
} else {
locale=Locale.US;
}
HttpSession session=req.getSession();
session.putValue("myLocale",locale);

bundle = ResourceBundle.getBundle("ApplicationResources",locale);

for (Enumeration e = bundle.getKeys();e.hasMoreElements();) {
String key = (String)e.nextElement();
String s = bundle.getString(key);
session.putValue(key,s);

}

}

return map.findForward("success");

}

}

ApplicationResources.properties

hello.name=Name
hello.password=Password
hello.course=Course
hello.college=College
hello.city=City
hello.submit=submit

ApplicationResources_de_DE.properties

hello.name=Name-de
hello.password=Password-de
hello.course=Course-de
hello.college=College-de
hello.city=City-de
hello.submit=submit-de

ApplicationResources_fr_FR.properties

hello.name=Name-fr
hello.password=Password-fr
hello.course=Course-fr
hello.college=College-fr
hello.city=City-fr
hello.submit=submit-fr

ApplicationResources_sv_SE.properties

hello.name=Name-sv
hello.password=Password-sv
hello.course=Course-sv
hello.college=College-sv
hello.city=City-sv
hello.submit=submit-sv

Internalize.jsp

<%@ taglib uri="/tags/struts-html" prefix="html"%>
<html:html >
<body><br><br>
<center>
<h3><u>
Please select a language </u></h3>
<html:form action="/internalize">
English <input type="radio" name="lang" value="English" checked>
German <input type="radio" name="lang" value="German">
French <input type="radio" name="lang" value="French">
Swedish<input type="radio" name="lang" value="Swedish">
<br><br>
<html:submit />
</html:form>
</body>
</html:html>

Internalization.jsp

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ page import="java.util.*" %>
<html:html>

<body>
<%Locale myLocale = (Locale)session.getValue("myLocale"); %>
<br><br>
<center><h3><u>Sample Messages</u></h3>
<form>
<table>
<tr><td><%=session.getValue("hello.name")%></td>
<td><input type="text" /></td></tr>
<tr><td><%=session.getValue("hello.password")%></td>
<td><input type="text" /></td></tr>
<tr><td><%=session.getValue("hello.course") %></td>
<td><input type="text" /></td></tr>
<tr><td><%=session.getValue("hello.college") %></td>
<td><input type="text" /></td></tr>
<tr><td><%=session.getValue("hello.city") %></td>
<td><input type="text" /></td></tr>
<tr><td></td><td colsapn="2"><html:submit> <%=session.getValue("hello.submit") %></html:submit></td></tr>
</table>
</form>

</body>
</html:html>

1 comment:

Anonymous said...

This is my first message. Please don't delete it. Thanks
jsh39shd2 [url=http://hsk39sg3.com]link[/url]