English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Servlet Internationalization

Before we start, let's take a look at three important terms:

  • Internationalization (i18n):This means that a website provides different versions of content translated into the language or nationality of the visitor.

  • Localization (l10n):This means adding resources to the website to make it adapt to specific geographical or cultural regions, such as translating the website into Hindi.

  • Regional Setting (locale):This is a special 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 regional setting for the US.

There are some considerations to keep in mind when building a global website. This tutorial will not go into the complete details of these considerations, but it will demonstrate how to present web pages in different languages through differentiated positioning (i.e., regional settings) with a good example.

Servlets can pick up the corresponding version of the website based on the regional settings of the requestor and provide the appropriate version of the website based on local language, culture, and needs. The following is a method to return the Locale object in the request object.

java.util.Locale request.getLocale()

Detecting Regional Settings

The following lists important regional settings methods, which you can use to detect the geographical location, language, and regional settings of the requestor. All methods below show the country name and language name set in the requestor's browser.

Serial NumberMethod & Description
1String getCountry()
This method is based on 2 A capital letter-formatted ISO 3166 格式返回该区域设置的国家/This format returns the country code of the locale.
2Region code.
String getDisplayCountry()
3This method returns the name of the country of the locale suitable for display to the user.
String getLanguage() 639 This method returns the language code of the locale in lowercase letters.
4String getDisplayLanguage()
This method returns the name of the language of the locale suitable for display to the user.
5String getISO3Country()
This method returns the three-letter abbreviation of the country of the locale.
6String getISO3Language()
This method returns the three-letter abbreviation of the language of the locale.

Online 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 setting
      Locale locale = request.getLocale();
      String language = locale.getLanguage();
      String country = locale.getCountry();
      // Set the response content type
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
      String title = "Detect 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>");
  }
}

Language setting

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

The second point is to display all special characters using HTML entities, for example, "ñ" represents "ñ", "¡" represents "¡" as follows:

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 the 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 = "In Spanish";
    String docType = "<!DOCTYPE html> \n";
     out.println(docType +
     "<html>\n" +
     "<head><title>" + title + "</title></head>\n" +
     "<body bgcolor="#f0f0f0">\n" +
     "<h1>" + "In Spanish:" + "</h1>\n" +
     "<h1>" + "¡Hola Mundo!" + "</h1>\n" +
     "</body></html>");
  }
}

Locale-specific date

You can use the java.text.DateFormat class and its static method getDateTimeInstance() to format date and time specific to a locale. 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 the response content type
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    // Get the client's locale setting
    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>");
  }
}

Locale-specific currency

You can use the java.text.NumberFormat class and its static method getCurrencyInstance() to format numbers (such as long type or double type) as currency specific to the locale. 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 the response content type
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    // Get the client's locale setting
    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>");
  }
}

Locale-specific percentage

You can use the java.text.NumberFormat class and its static method getPercentInstance() to format percentages specific to the locale. 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 the response content type
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    // Get the client's locale setting
    Locale locale = request.getLocale( );
    NumberFormat nft = NumberFormat.getPercentInstance(locale);
    String formattedPerc = nft.format(0.51);
    String title = "Percentage Specific to Locale";
    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>");
  }
}