Skip to main content

Struts2 Resource Handling - Using Hindi language in internationalization (i18n)

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.
  • English : http://localhost:8080/ResourceHandling/loginpage
  • Hindi : http://localhost:8080/ResourceHandling/loginpage?request_locale=in
Here is the output screens for the above execution. 






Download complete source code.

Comments

Hi ,

Great blog! Is there an email address I can contact you in private?
Hemraj said…
This comment has been removed by the author.

Popular posts from this blog

Using HyperSQL (HSQLDB)

HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very a small database that supports almost all features of the standard database system. It comes with small jar file that can be found in lib folder. The HSQLDB jar package is located in the /lib directory of the ZIP package and contains several components and programs. Core components of jar file are : HyperSQL RDBMS Engine (HSQLDB), HyperSQL JDBC Driver, Database Manager, and Sql Tool. Installing and Using Download: download latest release of HyperSQL database from http://hsqldb.org website and extract it. You will see following contents. Here "bin" directory contains some batch files those can be used to run a swing based GUI tool. You can use runManagerSwing.bat to connect to database, but database must be on before running it. Directory lib contains File hsqldb.jar . It is the database to be used by you. Running database First ...

In Process Mode of HSQLDB in web application.

If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLDB into your web application and it runs as a part of your web application programm in the same JVM. In this mode, the database does not open any port to connect to the application on the hosing machine and you don't need to configure anything to access it. Database is not expposed to other application and can not be accessed from any dabase tools like dbVisualizer etc. In this mode ,database will be unknown from any other person except you. But in the 1.8.0 version, you can use Server intance for external as well as in process access.  To close the databse, you can issue SHUTDOWN command as an SQL query.   In the in-process mode, database starts from JDBC with the associated databse file provided through  connection URL. for example   DriverManager.getConnection("jdbc:hsqldb:mydatabase","SA","");   Here myd...

How to handle values from dynamically generated elements in web page using struts2

Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values for unknown number of repeating for some information. for example when you are asking to get the projects details from user, you need to put the option to add the more project for the user since you don't known how many projects user have. In the HTML form, you repeat the particular section to get the multiple values for those elements. In Html page , you can put the option to add new row of elements or text fields by writing the java script or using JQuery API. Now, the question is that how to capture the values of dynamically generated text fields on the server. Using the servlet programming you can get the values by using getParameters() method that resultants the array of the parameter having the same name. But this limit you to naming the text fields in the HTML form. To ally this approach, you have to take the same name for t...