Servlet Internationalization
Before we start, 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 a website to adapt it to a specific geographical or cultural region, for example, translating the website into Hindi.
- Locale:This is a specific cultural or geographical region. It is usually represented by 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 the full details of these considerations, but it will demonstrate through a good example how to make web pages render in different languages through differentiated positioning (i.e., locale).
A Servlet can pick 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. Below are the methods on the request object that return Locale objects.
java.util.Locale request.getLocale()
Detecting Locale
Below is a list of important locale methods that you can use to detect the requester's geographical location, language, and locale. All of the following methods show the country name and language name set in the requester's browser.
| Seq. | Method & Description |
|---|---|
| 1 | String getCountry() This method returns the country/region code of the locale in ISO 3166 format with 2 uppercase letters. |
| 2 | String getDisplayCountry() This method returns the name of the country of the locale suitable for display to the user. |
| 3 | String getLanguage() This method returns the language code of the locale in ISO 639 format with lowercase letters. |
| 4 | String getDisplayLanguage() This method returns the name of the language of the locale suitable for display to the user. |
| 5 | String getISO3Country() This method returns the three-letter abbreviation of the country of the locale. |
| 6 | String getISO3Language() This method returns the three-letter abbreviation of the language of the locale. |
Example
This example demonstrates how to display the language and the 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
{
// 获取客户端的区域设置
Locale locale = request.getLocale();
String language = locale.getLanguage();
String country = locale.getCountry();
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String title = "检测区域设置";
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 Settings
A Servlet can output pages written in Western European languages (such as English, Spanish, German, French, Italian, Dutch, etc.). Here, to correctly display all characters, setting the Content-Language header is very important.
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
{
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 设置西班牙语言代码
response.setHeader("Content-Language", "es");
String title = "En Español";
String docType = "<!DOCTYPE html> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#F0F0F0\">\n" +
"<h1>" + "En Español:" + "</h1>\n" +
"<h1>" + "¡Hola Mundo!" + "</h1>\n" +
"</body></html>");
}
}
Locale-Specific Dates
You can use the java.text.DateFormat class and its static method getDateTimeInstance() to format dates and times that are locale-specific. The following example demonstrates how to format dates 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;charset=UTF-8");
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> \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 or double types) into 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
{
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 获取客户端的区域设置
Locale locale = request.getLocale( );
NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
String formattedCurr = nft.format(1000000);
String title = "特定于区域设置的货币";
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 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
{
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
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> \n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#F0F0F0\">\n" +
"<h1 align=\"center\">" + formattedPerc + "</h1>\n" +
"</body></html>");
}
}
Other Extensions