Implementing Internationalization in JAVA


Internationalization

Introduction: With the advent of globalization, the importance of internationalizing web applications has increased. This is important because web applications are accessed by users from various regions and countries. The Internet has no boundaries and neither should your web application. People all over the world access the Net to browse web pages that are written in different languages. For example, you can check your web-based email from virtually anywhere. A user in America can access the web and check her Facebook!. How does Facebook do it? 

In this world of Internationalization and Localization. What are they and why do we need them? This chapter attempts to answer these questions and shows you how to internationalize and localize your J2EE web applications. Each of these terms is explained in detail in the subsequent sections of this chapter. At the end of this chapter you will be able to see how to make your web application user friendly in different countries using different languages. You will also see the various ways to do this.

JAVA and Internationalization: Java Standard Edition, provides a rich set of API's to support Internationalization and Localization of an application. The internationalization API's provided with Java(SE) can easily adapt different messages, number, date, and currency formats according to different country and region conventions. The internationalization and Localization API's specified under Java Se platform is based on the Unicode 3.0 character encoding. Java provides two common classes to implement internationalization, Locale and ResourceBundle.

Internationalization and Localization:
When you read about internationalizing applications, you will come across terms such as localization, character encoding, locales, resource bundles and so on. This section covers some of the commonly used terms in this chapter.Internationalization or I18n is the process of enabling your application to cater to users from different countries and supporting different languages. With I18n, software is made portable between languages or regions. For example, the Yahoo! Web site supports users from English, Japanese and Korean speaking countries, to name a few.Localization or L10n on the other hand, is the process of customizing your application to support a specific location. When you customize your web application to a specific country say, Germany, you are localizing your application. Localization involves establishing on-line information to support a specific language or region. Though I18n and L10n appear to be at odds with each other, in reality they are closely linked.

Describing the Locale Class: The java.util.Locale class is used while creating internationalization Java's applications. The java.util.Locale class is a non-abstract final class packed into the java.util package. A Locale object encapsulates the language, country, and variant of the specific locale. These Locales affects language choice, collation, calendar usage, date and time formats, number and currency formats, and many other cultural sensitive data representations. All other locale-oriented classes use the locale object to adapt to the specific locale and provide the output accordingly.
The Locale class consists of three constructors:

  • Locale (String Language)
  • Locale (String language, String country)
  • Locale (String language, String country, String variant)

Parameters Of The locale Object:  There are three parameters which are ed in the Locale Objects:

  • Language
  • Country
  • Variant

Describing The ResourceBundle Class: The ResourceBundles class provides the mechanism to seprate user interface(ui) elements and other locale sensitive data from the application logic.ResourceBundle is an abstract base class that represents a container of resources.
 
It has two subclasses:

ListResourceBundle and PropertiesResourceBundle. When you are localizing your application, all the locale specific resources like text messages, icons and labels are stored in subclasses of the ResourceBundle. There will be one instance of the ResourceBundle per locale.The getBundle method in this class retrieves the appropriate ResourceBundle instance for a given locale. The location of the right bundle is implemented using an algorithm which is explained later in this section.

Internationalizing Web Applications: Internationalization is the process of preparing an application to support more than one language and data format. For internalizing java web application, we must first decide how to determine the users language and locale preferences. Let's create an application named i18nWebx1, which demonstrate how to internationalize Web application: It have the following component

The index.jsp:

<html>
   <body bgcolor="cyan">
     <center>
       <b>This is an Example To Shows the internationalization, Clik Here To find the output:</b>
       <pre>
       <a href="testser?language=en">English User</a><br/>
       <a href="testser?language=en&country=US">English(us)User</a><br/>
       <a href="testser?language=it">Italian User</a>
       </pre>
     </center>
   </body>
</html>

The Home.jsp:

<%@page import="java.util.*" %>
<jsp:include page="index.jsp"/>
<h2>OUTPUT</h2></br>
<%
     ResourceBundle rb=(ResourceBundle) request.getAttribute("resource");
%>
<%=rb.getString("welcome.message")%>

The I18NServlet.java:

package my;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class I18NServlet extends HttpServlet
{
  public void service(HttpServletRequest req, HttpServletResponse res)
  throws ServletException, IOException
  {
   String cc=req.getParameter("country");
   String ln=req.getParameter("language");
   Locale l=null;
   if(cc==null)
   {
     l=new Locale(ln);
    else
     l=new Locale(ln,cc);
   }
   ResourceBundle rb=ResourceBundle.getBundle("ApplicationResources",l);
   req.setAttribute("resource", rb);
   RequestDispatcher rd=req.getRequestDispatcher("home.jsp");
   rd.forward(req, res);
  }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url=".">
<context-root>/i18nWebx1</context-root>
<servlet>
<servlet-name>I18N</servlet-name>
<servlet-class>my.I18NServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>I18N</servlet-name>
<url-pattern>/testser</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</glassfish-web-app>

OUTPUT:

Index.jsp:

index1.gif

Home.jsp:

home1.gif