Sunday, August 19, 2012

Building JAVA Based Localized Web Applications

Posted by Shiva Gouraram | Sunday, August 19, 2012 | Category: |


This tutorial describes how to develop your applications which can be available in different number of local languages like Hindi, Telugu, Tamil or any other language available in this world.
Dont you think your application if available in different languages attract the visitors or users which makes them easy to understand without learning or having trouble in learning and understanding only English???
Yes....to make it possible this is a tutorial which does not make use of any other 3rd party applications like Google Translator or Microsoft Translator in your application.
O.K let us start our tutorial,This tutorial is a simple tutorial which describes the conversion of one message into 2 different languages Telugu and Hindi.
After this tutorial you can develop your java based web applications for different languages.

Before describing the code i will explain few JAVA classes which helps to develop these kind of applications.

1) Locale Object:

Def: A Locale object represents a specific geographical, political, or cultural region. An operation that requires a Locale to perform its task is called locale-sensitive and uses the >Locale to tailor information for the user. For example, displaying a number is a locale-sensitive operation--the number should be formatted according to the customs/conventions of the user's native country, region, or culture.
i.e a Locale object can be used for specific country or region to get the country details like language,currency and percentages.
to know more click on the below link

Locale Object from Oracle website

2) ResourceBundle Object:


Resource bundles contain locale-specific objects. When your program needs a locale-specific resource, a String for example, your program can load it from the resource bundle that is appropriate for the current user's locale. In this way, you can write program code that is largely independent of the user's locale isolating most, if not all, of the locale-specific information in resource bundles.
This allows you to write programs that can:
  • be easily localized, or translated, into different languages
  • handle multiple locales at once
  • be easily modified later to support even more locale
to know more about ResourceBundle Class click on the below link


OK these are the 2 classes by which we are going to develop localized applications.


As i said i am going to develop an application which can be customized for 3 languages we need to have 3 text files generally called as properties file which contains name value pairs by which we can access them in our application.

open notepad or notepad++ and create three types of text file

1) Locale_en_EN.properties
2 )Locale_hi_HI.properties
3) Locale_te_TE.properties

Note:
Please paste these three files in classes folder of WEB-INF directory.

these three files holds the "HTML" equivalent values of Hindi and Telugu language code.
to generate HTML  equivalen  code for specific language we have few tools which generate the code according to given English input.Click on the below links to convert English to other languages which generates HTML code.

Telugu Converter            Hindi Converter 

for instance just paste this code in below files

Locale_hi_HI.properties 



 Locale_te_TE.properties 



Locale_en_EN.properties

OK now we have properties file which consists of equivalent code for the english message.

Now create a Dynamic Web Project in Eclipse and Create a java file "index.jsp" and "Locale.jsp".
index.jsp is the index page which displays the message in three languages and "Locale.jsp" is the page which is used to create HttpSession by which we can know which language is chosen by the user at this point of time.

index.jsp
---------------


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*"%>
<%
String locale = (String) session.getAttribute("locale");
if (locale == null)
locale = "en";
Locale c = new Locale(locale, locale);
ResourceBundle captions = ResourceBundle.getBundle("Locale", c);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Localization</title>
</head>
<body>
<div id="langholder" style="top:15px;">

<a href="Locale.jsp?lang=en">English</a>
<a href="Locale.jsp?lang=te">Telugu</a>
<a href="Locale.jsp?lang=hi">Hindi</a>
</div>
<center>
<%=captions.getString("msg")%>
</center>
</body>
</html>






Locale.jsp
-----------
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String lang = request.getParameter("lang");
if (lang != null) {
session.setAttribute("locale", lang);
}
response.sendRedirect("index.jsp");
%>

Preview:




Download Localization Example
That;s it friends...now you can develop applications for any number of localized language.






Share/Bookmark

Currently have 8 comments:

  1. This comment has been removed by the author.

  2. Thanks buddy
    Nice code
    You must have to give it before TGMC 2011.
    ...........
    .........

    And have you knowledge about how to create web service.........

    If yes then give me some light about that
    my email id is mukesh007thakur@gmail.com

  3. Hi mukesh for web services development i created an article please read this!
    http://www.tgmc2k12.co.cc/2012/08/web-services-development-using-eclipse.html

  4. Hi..
    I have a doubt..
    our site can contain any word..
    so do we need to maintain a kind of dictionary in .properties file..?

    waiting for your reply..

  5. Yeah...see...for any word or sentence you need to save the equivalent html code...and we have to store it in name value pairs in properties file..hindi code in hindi properties file like that!

  6. This comment has been removed by the author.



  7. java.util.MissingResourceException: Can't find bundle for base name Locale, locale en_EN


    what to do for this exception??

  8. In order to smoothly handle Java localization, I use this online strings management tool: https://poeditor.com/
    It's collaborative, really well built and user friendly, and it saves a great deal of time.


Leave a Reply

Subscribe