Struts2 provides us different ways for handling message resources. Struts2 supports internationalization for different locales and we can provide message resources for different locales. Struts2 will use resource file naming convention through which it identifies that which message resource file to be used within the particular session.
See the following configuration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUB
"-//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="global" />
<package name="default" namespace="/" extends="struts-default">
<action name="myaction" class="MyAction">
<result name="sucess">success.jsp</result>
<result name="input">loginjsp.jsp</result>
</action>
<action name="loginpage">
<result>loginjsp.jsp</result>
</action>
</package>
</struts>
In the above struts.xml file,
<constant name="struts.custom.i18n.resources" value="global" />
This line of code specifies that struts2 will use the global.properties file as message resource file. Struts2 will upload all the values of this property file to the value stack and will use them wherever is required.
Now if you want to create different resource file for different language support, you have to just append the suffix "_" and symbol of the language. For example if we want to create the another resource file contain the messages in Indian language (Hindi) we can give the file name as global_in.properties.
To tell the struts that which resource file to be used throughout the user's session, we have to send the query string with any request request_locale=in, then struts2 automatically loads the messages from global_in.properties file.
Now to add the Hindi Unicode characters to the resource file, you have to change the file encoding to utf-8. To do this, used the jdk(in bin folder of the jdk) utility as following.
Here you will get the file global_in1.properties having the encoding utf-8. Then, I deleted the previous ile global_in.properties file and renamed the global1_in.properties file as global_in.properties to use.
Then you write the following Hindi Unicode messages to the file global_in.properties file to be used.
#Global hindi messages
username =\u092F\u0942\u091C\u0930 \u0928\u093E\u092E
emailid =\u092A\u093E\u0938\u0935\u0930\u094D\u0921
loginpage.title=\u092F\u0942\u091C\u0930 \u0932\u094B\u0917\u0940\u0928
loginpage.heading=\u0915\u0943\u092A\u092F\u093E \u0905\u092A\u0928\u0947 \u0916\u093E\u0924\u0947 \u092E\u0947\u0902 \u0932\u0949\u0917 \u0911\u0928 \u0915\u0930\u0928\u0947 \u0915\u0947 \u0932\u093F\u090F \u0928\u0940\u091A\u0947 \u0915\u0947 \u092B\u093E\u0930\u094D\u092E \u0915\u093E \u0909\u092A\u092F\u094B\u0917 \u0915\u0930\u0947\u0902
Now I am using the above messages in the following jsp page(loginjsp.jsp) to define the labels. In this file, change the charset=utf-8 as specified in the first line.
<%@ page contentType="text/html; charset=utf-8" language="java" %>
<%@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>
<title>
<s:text name="loginpage.title" >
Title Message should be here.
</s:text>
</title>
</head>
<body>
<h2><s:text name="loginpage.heading">Header</s:text></h2>
<s:form action="myaction" method="post">
<s:textfield key="username" name="username"/>
<s:textfield key="emailid"/>
<s:submit value="submit"/>
</s:form>
</body>
</html>
To access the different resource file, we have to call them as follows.
Download complete source code.
See the following configuration.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUB
"-//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="global" />
<package name="default" namespace="/" extends="struts-default">
<action name="myaction" class="MyAction">
<result name="sucess">success.jsp</result>
<result name="input">loginjsp.jsp</result>
</action>
<action name="loginpage">
<result>loginjsp.jsp</result>
</action>
</package>
</struts>
In the above struts.xml file,
<constant name="struts.custom.i18n.resources" value="global" />
This line of code specifies that struts2 will use the global.properties file as message resource file. Struts2 will upload all the values of this property file to the value stack and will use them wherever is required.
Now if you want to create different resource file for different language support, you have to just append the suffix "_" and symbol of the language. For example if we want to create the another resource file contain the messages in Indian language (Hindi) we can give the file name as global_in.properties.
To tell the struts that which resource file to be used throughout the user's session, we have to send the query string with any request request_locale=in, then struts2 automatically loads the messages from global_in.properties file.
Now to add the Hindi Unicode characters to the resource file, you have to change the file encoding to utf-8. To do this, used the jdk(in bin folder of the jdk) utility as following.
Here you will get the file global_in1.properties having the encoding utf-8. Then, I deleted the previous ile global_in.properties file and renamed the global1_in.properties file as global_in.properties to use.
Then you write the following Hindi Unicode messages to the file global_in.properties file to be used.
#Global hindi messages
username =\u092F\u0942\u091C\u0930 \u0928\u093E\u092E
emailid =\u092A\u093E\u0938\u0935\u0930\u094D\u0921
loginpage.title=\u092F\u0942\u091C\u0930 \u0932\u094B\u0917\u0940\u0928
loginpage.heading=\u0915\u0943\u092A\u092F\u093E \u0905\u092A\u0928\u0947 \u0916\u093E\u0924\u0947 \u092E\u0947\u0902 \u0932\u0949\u0917 \u0911\u0928 \u0915\u0930\u0928\u0947 \u0915\u0947 \u0932\u093F\u090F \u0928\u0940\u091A\u0947 \u0915\u0947 \u092B\u093E\u0930\u094D\u092E \u0915\u093E \u0909\u092A\u092F\u094B\u0917 \u0915\u0930\u0947\u0902
Now I am using the above messages in the following jsp page(loginjsp.jsp) to define the labels. In this file, change the charset=utf-8 as specified in the first line.
<%@ page contentType="text/html; charset=utf-8" language="java" %>
<%@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>
<title>
<s:text name="loginpage.title" >
Title Message should be here.
</s:text>
</title>
</head>
<body>
<h2><s:text name="loginpage.heading">Header</s:text></h2>
<s:form action="myaction" method="post">
<s:textfield key="username" name="username"/>
<s:textfield key="emailid"/>
<s:submit value="submit"/>
</s:form>
</body>
</html>
To access the different resource file, we have to call them as follows.
- English : http://localhost:8080/ResourceHandling/loginpage
- Hindi : http://localhost:8080/ResourceHandling/loginpage?request_locale=in
Download complete source code.
Comments
Great blog! Is there an email address I can contact you in private?