Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Servlet Internationalization

Servlet Internationalization

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

There are considerations when building a global website. This tutorial will not cover the complete details of these considerations, but it will show you, through a good example, how to present web pages in different languages through differentiation (i.e. locales).

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



java.util.Locale request.getLocale() 

Detecting the Locale

The following lists important locale methods you can use to detect the requester's geographic location, language, and locale. All of the following methods display the country name and language name set in the requester's browser.

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

Example

This example demonstrates how to display the language and related 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 the client's locale settings

      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 Settings";

      String docType = " \n";

      out.println(docType +

        "\n" +

        "" + title + "\n" +

        "\n" +

        "

" + language + "

\n" + "

" + country + "

\n" + ""); } }

Language Settings

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

The second point is to use HTML entities to display all special characters, for example, "ñ" for "ñ" and "¡" for "¡", 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ñol";

    String docType = " \n";

     out.println(docType +

     "\n" +

     "" + title + "\n" +

     "\n" +

     "

" + "En Español:" + "

\n" + "

" + "¡Hola Mundo!" + "

\n" + ""); } }

Locale-Specific Dates

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 specific to 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 the client's locale settings

    Locale locale = request.getLocale( );

    String date = DateFormat.getDateTimeInstance(

                                  DateFormat.FULL, 

                                  DateFormat.SHORT, 

                                  locale).format(new Date( ));



    String title = "Locale-Specific Dates";

    String docType = " \n";

      out.println(docType +

      "\n" +

      "" + title + "\n" +

      "\n" +

      "

" + date + "

\n" + ""); } }

Locale-Specific Currency

You can use the java.text.NumberFormat class and its static method getCurrencyInstance() to format numbers (such as long or double types) as locale-specific currency. The following example demonstrates how to format currency specific to 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 the client's locale settings

    Locale locale = request.getLocale( );

    NumberFormat nft = NumberFormat.getCurrencyInstance(locale);

    String formattedCurr = nft.format(1000000);



    String title = "Locale-Specific Currency";

    String docType = " \n";

      out.println(docType +

      "\n" +

      "" + title + "\n" +

      "\n" +

      "

" + formattedCurr + "

\n" + ""); } }

Locale-Specific Percentages

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 percentages specific to 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 the client's locale settings

    Locale locale = request.getLocale( );

    NumberFormat nft = NumberFormat.getPercentInstance(locale);

    String formattedPerc = nft.format(0.51);



    String title = "Locale-Specific Percentages";

    String docType = " \n";

      out.println(docType +

      "\n" +

      "" + title + "\n" +

      "\n" +

      "

" + formattedPerc + "

\n" + ""); } }