JSP Internationalization

Before we start, there are several important concepts to explain:

  • Internationalization (i18n): This means that a page presents different translated versions based on the visitor's language or country.
  • Localization (l10n): Adding resources to a website to adapt it to different regions and cultures. For example, the Hindi version of a website.
  • Locale: This is a specific region or culture, usually considered as a language code and a country code joined by an underscore. For example, "en_US" represents the US English locale.

If you want to build a globalized website, you need to consider a series of items. This chapter will tell you in detail how to handle internationalization issues and provide some examples to deepen your understanding.

The JSP container can provide the correct page version based on the request's locale attribute. The following shows the syntax for obtaining a Locale object through the request object:

java.util.Locale request.getLocale() 

Detect Locale

The following table lists the more important methods of the Locale object, used to detect the region, language, and locale of the request object. All these methods will display the country name and language name in the browser:

Number Method & Description
1 String getCountry()Returns the country/region code in uppercase English, or the region in ISO 3166 2-letter format
2 String getDisplayCountry()Returns the country name to be displayed to the user
3 String getLanguage()Returns the language code in lowercase English, or the region in ISO 639 format
4 String getDisplayLanguage()Returns the language name to be shown to the user
5 String getISO3Country()Returns the 3-letter abbreviation of the country name
6 String getISO3Language()Returns the 3-letter abbreviation of the language name

Example Demonstration

This example shows us how to display language and country in JSP:

<%@ page import="java.io.*,java.util.Locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%
   //获取客户端本地化信息
   Locale locale = request.getLocale();
   String language = locale.getLanguage();
   String country = locale.getCountry();
%>
<html>
<head>
<title>Detecting Locale</title>
</head>
<body>
<center>
<h1>Detecting Locale</h1>
</center>
<p align="center">
<% 
   out.println("Language : " + language  + "<br />");
   out.println("Country  : " + country   + "<br />");
%>
</p>
</body>
</html>

Language Settings

JSP can use Western European languages to output a page, such as English, Spanish, German, French, Italian, etc. From this, it can be seen that it is important to set the Content-Language header to correctly display all characters.

The second point is that you need to use HTML character entities to display special characters, for example, "&#241;" represents ñ, and "&#161;" represents ¡ :

<%@ page import="java.io.*,java.util.Locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%
    // Set response content type
    response.setContentType("text/html");
    // Set spanish language code.
    response.setHeader("Content-Language", "es");
    String title = "En Espa?ol";

%>
<html>
<head>
<title><%  out.print(title); %></title>
</head>
<body>
<center>
<h1><%  out.print(title); %></h1>
</center>
<div align="center">
<p>En Espa?ol</p>
<p>?Hola Mundo!</p>
</div>
</body>
</html>

Locale-Specific Dates

You can use the java.text.DateFormat class and its static method getDateTimeInstance() to format dates and times. The following example shows how to format dates and times according to a specified locale:

<%@ page import="java.io.*,java.util.Locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%@ page import="java.text.DateFormat,java.util.Date" %>

<%
    String title = "Locale Specific Dates";
    //Get the client's Locale
    Locale locale = request.getLocale( );
    String date = DateFormat.getDateTimeInstance(
                                  DateFormat.FULL, 
                                  DateFormat.SHORT, 
                                  locale).format(new Date( ));
%>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>Local Date: <%  out.print(date); %></p>
</div>
</body>
</html>

Locale-Specific Currency

You can use the java.text.NumberFormat class and its static method getCurrencyInstance() to format numbers. For example, long and double types in locale-specific currency. The following example shows how to format currency according to a specified locale:

<%@ page import="java.io.*,java.util.Locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%@ page import="java.text.NumberFormat,java.util.Date" %>

<%
    String title = "Locale Specific Currency";
    //Get the client's Locale
    Locale locale = request.getLocale( );
    NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
    String formattedCurr = nft.format(1000000);
%>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>Formatted Currency: <%  out.print(formattedCurr); %></p>
</div>
</body>
</html>

Locale-Specific Percentages

You can use the java.text.NumberFormat class and its static method getPercentInstance() to format percentages. The following example tells us how to format percentages according to a specified locale:

<%@ page import="java.io.*,java.util.Locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%@ page import="java.text.NumberFormat,java.util.Date" %>

<%
    String title = "Locale Specific Percentage";
    //Get the client's Locale
    Locale locale = request.getLocale( );
    NumberFormat nft = NumberFormat.getPercentInstance(locale);
    String formattedPerc = nft.format(0.51);
%>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>Formatted Percentage: <%  out.print(formattedPerc); %></p>
</div>
</body>
</html>
Other Extensions