Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Servlet internationalization


May 15, 2021 Servlet


Table of contents


Servlet Internationalization

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

  • Internationalization (i18n): This means that a website provides different versions of content translated into the visitor's language or nationality.
  • Localization (l10n): This means adding resources to the site to adapt it to specific geographic or cultural areas, such as the site translated into Hindi.
  • Locale: This is a special cultural or geographic area. I t usually refers to a language symbol followed by an underscore and a national symbol. For example, en_US " represents an English locale for US.

There are some considerations when building a global website. This tutorial won't cover the full details of these considerations, but it will show you a good example of how to make web pages appear in different languages through differentiated targeting, or locale.

Servlet can pick up the appropriate version of the site based on the requester's locale and provide the appropriate version of the site according to the local language, culture, and needs. The following is how to return the Locale object in the request object.

java.util.Locale request.getLocale() 

Detect the locale

Important locale methods are listed below, which you can use to detect the requester's geographic location, language, and locale. All of the following methods show the country and language names set in the requester's browser.

Serial number Method & Description
1 String getCountry()
This method returns country code set in this area in the ISO 3166 format in 2 uppercase letters.
2 String getDisplayCountry()
This method returns the name of the country that is suitable for the area set to the user.
3 String getLanguage()
The method returns the language code set by the area in the ISO 639 format in lowercase letters.
4 String getDisplayLanguage()
This method returns the name of the language suitable for the area set to the user.
5 String getISO3Country()
This method returns three alphanumeric abbreviations in the country set.
6 String getISO3Language()
This method returns an abbreviation for the three letters of the language set by the area.

This example shows how to display the language of a request and the relevant country:

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
  {
      // 获取客户端的区域设置
      Locale locale = request.getLocale();
      String language = locale.getLanguage();
      String country = locale.getCountry();

      // 设置响应内容类型
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      String title = "检测区域设置";
      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>");
  }
} 

Language settings

Servlet outputs pages written in Western European languages such as English, Spanish, German, French, Italian, Dutch, and so on. Here, in order to display all the characters correctly, it is important to set the Content-Language header.

The second point is to display all the special characters using HTML entities, for example, ""

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
  {
    // 设置响应内容类型
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    // 设置西班牙语言代码
    response.setHeader("Content-Language", "es");

    String title = "En Espa&ntilde;ol";
    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>" + "En Espa&ntilde;ol:" + "</h1>\n" +
     "<h1>" + "&iexcl;Hola Mundo!" + "</h1>\n" +
     "</body></html>");
  }
} 

The date specific to the locale

You can use the java.text.DateFormat class and its static method getDateTimeInstance() to format locale-specific dates and times. The following example shows 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
  {
    // 设置响应内容类型
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    // 获取客户端的区域设置
    Locale locale = request.getLocale( );
    String date = DateFormat.getDateTimeInstance(
                                  DateFormat.FULL, 
                                  DateFormat.SHORT, 
                                  locale).format(new Date( ));

    String title = "特定于区域设置的日期";
    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\">" + date + "</h1>\n" +
      "</body></html>");
  }
} 

The 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, for locale-specific currencies. The following example shows how to format a 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
  {
    // 设置响应内容类型
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    // 获取客户端的区域设置
    Locale locale = request.getLocale( );
    NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
    String formattedCurr = nft.format(1000000);

    String title = "特定于区域设置的货币";
    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\">" + formattedCurr + "</h1>\n" +
      "</body></html>");
  }
} 

The percentage that is specific to the locale

You can format a locale-specific percentage using the java.text.NumberFormat class and its static method getPercentInstance(). The following example shows how to format a percentage 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
  {
    // 设置响应内容类型
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    // 获取客户端的区域设置
    Locale locale = request.getLocale( );
    NumberFormat nft = NumberFormat.getPercentInstance(locale);
    String formattedPerc = nft.format(0.51);

    String title = "特定于区域设置的百分比";
    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\">" + formattedPerc + "</h1>\n" +
      "</body></html>");
  }
}