YouTip LogoYouTip

Servlet Internationalization

Servlet Internationalization

Before we begin, let's look at three important terms:

  • Internationalization (i18n): This means a website provides different versions of content translated into the visitor's language or nationality.
  • Localization (l10n): This means adding resources to a website to make it suitable for a specific geographical or cultural region, such as translating the website into Hindi.
  • Locale: This is a specific cultural or geographical region. It usually refers to a language symbol followed by an underscore and a country symbol. For example, "en_US" represents the English locale for the US.

There are some considerations when building a global website. This tutorial will not cover all the details of these considerations, but it will demonstrate how to present web pages in different languages through differentiated targeting (i.e., locale) using a good example.

A Servlet can pick up the appropriate version of the website based on the requester's locale and provide the corresponding version of the website according to the local language, culture, and needs. The following is the method in the request object that returns a Locale object.

java.util.Locale request.getLocale()

The important locale methods listed below can be used to detect the requester's geographical location, language, and locale. All the following methods display the country name and language name set in the requester's browser.

Serial Number Method & Description
1 String getCountry() This method returns the country/region code for this locale in ISO 3166 format as two uppercase letters.
2 String getDisplayCountry() This method returns the name of the country for this locale suitable for display to the user.
3 String getLanguage() This method returns the language code for this locale in ISO 639 format as lowercase letters.
4 String getDisplayLanguage() This method returns the name of the language for this locale suitable for display to the user.
5 String getISO3Country() This method returns the three-letter abbreviation for the country of this locale.
6 String getISO3Language() This method returns the three-letter abbreviation for the language of this locale.

Example

This example demonstrates how to display the language and associated country of a request:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;

public class GetLocale extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Get client's locale
        Locale locale = request.getLocale();
        String language = locale.getLanguage();
        String country = locale.getCountry();

        // Set response content type
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        String title = "Detecting Locale";
        String docType = "<!DOCTYPE html> 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>");
    }
}

A Servlet can output pages written in Western European languages (such as English, Spanish, German, French, Italian, Dutch, etc.). Here, it is very important to set the Content-Language header to display all characters correctly.

The second point is to use HTML entities to display all special characters, for example, "ñ" represents "Γ±", "¡" represents "Β‘", as shown below:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;

public class DisplaySpanish extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        // Set Spanish language code
        response.setHeader("Content-Language", "es");

        String title = "En Espa&ntilde;ol";
        String docType = "<!DOCTYPE html> n";
        out.println(docType +
                "<html>n" +
                "<head><title>" + title + "</title></head>n" +
                "<body bgcolor="#f0f0f0">n" +
                "<h1>" + "En Espa&ntilde;ol:" + "</h1>n" +
                "<h1>" + "&iexcl;Hola Mundo!" + "</h1>n" +
                "</body></html>");
    }
}

You can use the java.text.DateFormat class and its static method getDateTimeInstance() to format locale-specific dates and times. The following example demonstrates how to format a date for a given locale:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.DateFormat;
import java.util.Date;

public class DateLocale extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        // Get client's locale
        Locale locale = request.getLocale();

        String date = DateFormat.getDateTimeInstance(
                DateFormat.FULL, DateFormat.SHORT, locale).format(new Date());

        String title = "Locale-Specific Date";
        String docType = "<!DOCTYPE html> n";
        out.println(docType +
                "<html>n" +
                "<head><title>" + title + "</title></head>n" +
                "<body bgcolor="#f0f0f0">n" +
                "<h1 align="center">" + date + "</h1>n" +
                "</body></html>");
    }
}

You can use the java.text.NumberFormat class and its static method getCurrencyInstance() to format numbers (such as long or double types) into locale-specific currencies. The following example demonstrates how to format currency for a given locale:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Date;

public class CurrencyLocale extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        // Get client's locale
        Locale locale = request.getLocale();

        NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
        String formattedCurr = nft.format(1000000);

        String title = "Locale-Specific Currency";
        String docType = "<!DOCTYPE html> n";
        out.println(docType +
                "<html>n" +
                "<head><title>" + title + "</title></head>n" +
                "<body bgcolor="#f0f0f0">n" +
                "<h1 align="center">" + formattedCurr + "</h1>n" +
                "</body></html>");
    }
}

You can use the java.text.NumberFormat class and its static method getPercentInstance() to format locale-specific percentages. The following example demonstrates how to format a percentage for a given locale:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Locale;
import java.text.NumberFormat;
import java.util.Date;

public class PercentageLocale extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Set response content type
        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();
        // Get client's locale
        Locale locale = request.getLocale();

        NumberFormat nft = NumberFormat.getPercentInstance(locale);
        String formattedPerc = nft.format(0.51);

        String title = "Locale-Specific Percentage";
        String docType = "<!DOCTYPE html> n";
        out.println(docType +
                "<html>n" +
                "<head><title>" + title + "</title></head>n" +
                "<body bgcolor="#f0f0f0">n" +
                "<h1 align="center">" + formattedPerc + "</h1>n" +
                "</body></html>");
    }
}
← Servlet Useful ResourcesServlet Debugging β†’