Before we begin, several important concepts need to be explained:
Internationalization (i18n): Indicates that a page will be rendered in different translations based on the visitor's language or country.
Localization (l10n): Adding resources to a website to adapt it to different regions and cultures. Like the Indian version of the website.
Region: This is a specific region or culture, usually considered a linguistic marker and a national marker connected by an underscore. For example, "en_US" represents the English-speaking region of the United States.
If you want to build a global website, you need to care about a series of projects. This chapter will tell you in detail how to deal with internationalization issues and give some examples to deepen your understanding.
The JSP container can provide the correct page version based on the locale attribute of the request. Next, the syntax of how to obtain the Locale object through the request object is given:
java.util.Locale request.getLocale()
Detecting Locale
The following table lists the more important methods in the Locale object, which are used to detect the region, language, and region of the request object. All these methods will display the country name and language name in the browser:
serial number | method & describe |
---|
1 | String getCountry()
Returns the country/region code in English uppercase, 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 English lower case of the language code, or the area in ISO 639 format
|
4 | String getDisplayLanguage()
Return 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 tells 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. It can be seen that it is important to set the Content-Language header to display all characters correctly.
The second point is that you need to use HTML character entities to display special characters, such as "ñ" represents "?", "¡" 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>
Region-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 range:
<%@ 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>
Region-specific currencies
You can use the java.text.NumberFormat class and its static method getCurrencyInstance() to format numbers. For example, long and double in region-specific currencies. 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>
Region-specific percentages
You can use the java.text.NumberFormat class and its static method getPercentInstance() to format percentages. The following example shows us how to format percentages based on a specified range:
<%@ 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>