Struts 2.x : [A Framework from ASF] :: Multilingual with Internationalization
Struts 2.x Internationalization |
Struts 2.x Internationalization
Example for Struts 2.x Internationalization |
Project Structure
Employee.java
package com.javaskool.struts;
import com.opensymphony.xwork2.ActionSupport;
public class Employee extends ActionSupport{
private String name;
private int age;
public String execute()
{
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
MyLocale.java
package com.javaskool.struts;
import com.opensymphony.xwork2.ActionSupport;
public class MyLocale extends ActionSupport{
public String execute()
{
return SUCCESS;
}
}
global.properties
global.name = Name
global.age = Age
global.submit = Submit
global.heading = Select Locale
global.success = Successfully authenticated
global_es.properties
#property for es
global.name = Nombre de usuario
global.age = Edad
global.submit = Presentar
global.heading = seleccionar la configuracion regional
global.success = Autenticado correctamente
global_fr.properties
global.name = Nom d'utilisateur
global.age = l'âge
global.submit = Soumettre des
global.heading = Sé lectionnez Local
global.success = Authentifi é avec succès
global_zh_CN.properties
global.name = \u540D
global.age = \u5E74\u9F84
global.submit = \u63D0\u4EA4
global.heading = \u9009\u62E9\u672C\u5730
global.success = \u6210\u529F\u901A\u8FC7\u9A8C\u8BC1
global_ta.properties
global.name = \u0BAA\u0BC6\u0BAF\u0BB0\u0BCD
global.age = \u0BB5\u0BAF\u0BA4\u0BC1
global.submit = \u0B9A\u0BAE\u0BB0\u0BCD\u0BAA\u0BCD\u0BAA\u0BBF
global.heading = \u0B89\u0BB3\u0BCD\u0BB3\u0BC2\u0BB0\u0BCD \u0BA4\u0BC7\u0BB0\u0BCD\u0BA8\u0BCD\u0BA4\u0BC6\u0B9F\u0BC1\u0B95\u0BCD\u0B95\u0BB5\u0BC1\u0BAE\u0BCD
global.success = \u0BB5\u0BC6\u0BB1\u0BCD\u0BB1\u0BBF\u0B95\u0BB0\u0BAE\u0BBE\u0B95 \u0B85\u0B99\u0BCD\u0B95\u0BC0\u0B95\u0BB0\u0BBF\u0B95\u0BCD\u0B95\u0BBF\u0BB1\u0BA4\u0BC1
global_tg.properties
global.name = \u0C2A\u0C47\u0C30\u0C41
global.age = \u0C35\u0C2F\u0C38\u0C41
global.submit = \u0C2E\u0C28\u0C35\u0C3F \u0C1A\u0C47\u0C2F\u0C41
global.heading = \u0C0E\u0C02\u0C1A\u0C41\u0C15\u0C4B\u0C02\u0C21\u0C3F \u0C32\u0C4A\u0C15\u0C47\u0C32\u0C4D
global.success = \u0C35\u0C3F\u0C1C\u0C2F\u0C35\u0C02\u0C24\u0C02\u0C17\u0C3E \u0C05\u0C27\u0C3F\u0C15\u0C3E\u0C30\u0C02
global_hn.properties
global.name = \u0928\u093E\u092E
global.age = \u0909\u092E\u094D\u0930
global.submit = \u092A\u094D\u0930\u0938\u094D\u0924\u0941\u0924 \u0915\u0930\u0928\u093E
global.heading = \u091A\u092F\u0928 \u0935\u093E\u0924\u093E\u0935\u0930\u0923
global.success = \u0938\u092B\u0932\u0924\u093E\u092A\u0942\u0930\u094D\u0935\u0915 \u092A\u094D\u0930\u092E\u093E\u0923\u0940\u0915\u0943\u0924
index.jsp
<%@page import="java.text.SimpleDateFormat"%>
<%@ page contentType="text/html;charset=UTF-8" %>
<!-- ContentType is very important for Unicode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Employee Form with Multilingual Support</title>
</head>
<%@ taglib prefix="s" uri="/struts-tags"%>
<body>
<h1><s:text name="global.heading"/></h1>
<s:url id="indexEN" namespace="/" action="locale">
<s:param name="request_locale" >en</s:param>
</s:url>
<s:url id="indexES" namespace="/" action="locale" >
<s:param name="request_locale" >es</s:param>
</s:url>
<s:url id="indexFR" namespace="/" action="locale" >
<s:param name="request_locale" >fr</s:param>
</s:url>
<s:url id="localezhCN" namespace="/" action="locale" >
<s:param name="request_locale" >zh_CN</s:param>
</s:url>
<s:url id="localeTA" namespace="/" action="locale" >
<s:param name="request_locale" >ta</s:param>
</s:url>
<s:url id="localeTG" namespace="/" action="locale" >
<s:param name="request_locale" >tg</s:param>
</s:url>
<s:url id="localeHN" namespace="/" action="locale" >
<s:param name="request_locale" >hn</s:param>
</s:url>
<s:a href="%{indexEN}" >English</s:a>
<s:a href="%{indexES}" >Spanish</s:a>
<s:a href="%{indexFR}" >French</s:a>
<s:a href="%{localezhCN}" >Chinese</s:a>
<s:a href="%{localeTA}" >Tamil</s:a>
<s:a href="%{localeTG}" >Telugu</s:a>
<s:a href="%{localeHN}" >Hindi</s:a>
<s:form action="empinfo" method="post" namespace="/">
<s:textfield name="name" key="global.name" size="20" />
<s:textfield name="age" key="global.age" size="20" />
<s:submit name="submit" key="global.submit" />
</s:form>
</body>
</html>
success.jsp
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Success</title>
</head>
<%@ taglib prefix="s" uri="/struts-tags"%>
<body>
<s:property value="getText('global.success')" />
</body>
</html>
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.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="global" />
<package name="helloworld" extends="struts-default" namespace="/">
<action name="empinfo" class="com.javaskool.struts.Employee" method="execute">
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
<action name="locale" class="com.javaskool.struts.MyLocale" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
jar files required in lib folder
Note: List of jar files differ as per project
commons-fileupload-1.2.2
commons-io-2.0.1
commons-lang-2.4
commons-lang3-3.1
commons-logging-1.1.1
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
ognl-3.0.5
struts2-core-2.3.4
xwork-core-2.3.4
Click Here to download all jar files required for basic Struts 2.x Application
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>I18nWithStruts2</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>s2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>s2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Output
in English
in Spanish
in French
in Chinese
in Tamil
in Telugu
in Hindi
Click here to download complete code with jar files [3.57 MB]
Recent Comments