Servlet Internationalization

Servlet Internationalization with Examples

In this article, I am going to discuss Servlet Internationalization with Examples. Please read our previous article where we discussed Servlet Debugging.

Servlet Internationalization

Internationalization (i18n) is the task of creating a program flexible enough to run in any locale. Localization(l10n) is the process of arranging for a program to run during a specific locale. In Java, java.util.Locale represents a selected geographical, political, or cultural region. The string representation of a locale consists of the international standard two-character abbreviation for language and country and an optional variant, separated by underscore (_) characters.

Establishing Locale

An internet application either retrieves the locale from the request using the getLocale() method or allows the user to explicitly select the locale to get the right strings for a given user. A Locale object represents a selected geographical, political, or cultural region. An operation that needs a Locale to perform its task is named locale-sensitive and it uses the Locale to tailor information for the user. No validity check is performed once you construct a Locale because a Locale object is simply an identifier for a neighborhood. If you would like to ascertain whether particular resources are available for the Locale you construct, you want to query those resources.

Methods used:
  1. getCountry(): It returns the country/region code in upper case for this locale in ISO3166 2-letter format.
  2. getDisplayCountry(): It returns the name for the locale’s country which is appropriate for display to the user.
  3. getLanguage(): It returns the language code in lower case for this locale in ISO 639 format.
  4. getDisplayLanguage(): It returns the name for the locale’s language that is appropriate for display to the user.
  5. getISO3Country(): It returns a three-letter abbreviation for this locale’s country.
  6. getISO3Language(): It returns a three-letter abbreviation for this locale’s language.
Language Settings
  1. A servlet outputs a page written in a Western European language such as English, Spanish, German, French, Italian, Dutch, Norwegian, Finnish, or Swedish. Notice the utilization of the special characters “ñ” and “¡”. Characters like these, while scarce in English, are prevalent in Western European languages. Servlets have two ways to get these characters: with HTML character entities or Unicode escape sequences.
  2. The ability for specific sequences of characters in an HTML page to be displayed as a single character. The sequences, called character entities, begin with an ampersand (&) and end with a semi-colon (;). Character entities can either be named or numbered.
  3. The Unicode Worldwide Escape Sequences is a character coding system designed to support the interchange, processing, and display of the written texts of the diverse languages of the modern world. In addition, it supports classical and historical texts of the many written languages.
Locale Dates, currency, Percentage, and Numbers
  1. Java programs use the DateFormat.getDateInstance(int, locale) to parse and format dates in a locale-sensitive manner.
  2. Java programs use the NumberFormat.getXXXInstance(locale) method, where XXX can be Currency, Number, or Percent, to parse and format numerical values in a locale-sensitive manner.
Example: Servlet Internationalization

This example shows how to do Language Settings, display a language and associated country for a request; to format date and time specific to locale using java.text.DateFormat class and its static getDateTimeInstance() method; to format a number in locale-specific currency using java.txt.NumberFormat and its static getCurrencyInstance() method; and to get locale-specific percentage using java.txt.NumberFormat class and its specific getPercentInstance() method.

WelcomeServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.DateFormat;
import java.util.Date;
import java.text.NumberFormat;
public class WelcomeServlet extends HttpServlet {
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  // Get the client's Locale
  Locale locale = request.getLocale();
  String language = locale.getLanguage();
  String country = locale.getCountry();
  String date = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.SHORT, locale).format(new Date());
  NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
  String formattedCurr = nft.format(1000000);
  String formattedPerc = nft.format(0.51);

  // Set response content type
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();

  String title = "Detecting Locale";
  String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";

  out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n"
    + "<body bgcolor = \"#f0f0f0\">\n" + "<h1 align = \"center\">" + language + "</h1>\n"
    + "<h2 align = \"center\">" + country + "</h2>\n" + "</body></html>");

  out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n"
    + "<body bgcolor = \"#f0f0f0\">\n" + "<h1 align = \"center\">" + date + "</h1>\n" + "</body></html>");
  out.println(
    docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor = \"#f0f0f0\">\n"
      + "<h1 align = \"center\">" + formattedCurr + "</h1>\n" + "</body></html>");
  out.println(
    docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor = \"#f0f0f0\">\n"
      + "<h1 align = \"center\">" + formattedPerc + "</h1>\n" + "</body></html>");
 }
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <servlet>
  <servlet-name>WelcomeServlet</servlet-name>
  <servlet-class>WelcomeServlet</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>WelcomeServlet</servlet-name>
  <url-pattern>/WelcomeServlet.java</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file>WelcomeServlet.java</welcome-file>
 </welcome-file-list>
</web-app>
Output

Servlet Internationalization with Examples

From the next article onwards, I am going to discuss Real-time Examples using Java Servlet. Here, in this article, I try to the Servlet Internationalization. I hope you enjoy this Servlet Internationalization article.

Leave a Reply

Your email address will not be published. Required fields are marked *