JSP - 国际化( Internationalization)
在本章中,我们将讨论JSP中的国际化概念。 在我们开始之前,让我们了解以下三个重要术语 -
Internationalization (i18n) - 这意味着使网站能够提供翻译成访问者语言或国籍的不同版本的内容。
Localization (l10n) - 这意味着向网站添加资源以使其适应特定的地理或文化区域,例如印地语翻译到网站。
locale - 这是一个特定的文化或地理区域。 它通常被称为语言符号,后跟国家符号,由下划线分隔。 例如,“ en_US ”表示美国的英语语言环境。
在建立全球网站时,应该注意许多项目。 本教程不会向您提供有关此内容的完整详细信息,但它将为您提供一个很好的示例,说明如何通过区分其位置(即区域设置)将您的网页以不同语言提供给Internet社区。
JSP可以根据请求者的语言环境选择适当的站点版本,并根据本地语言,文化和要求提供适当的站点版本。 以下是返回Locale对象的请求对象的方法。
java.util.Locale request.getLocale()
检测区域设置
以下是重要的区域设置方法,您可以使用它们来检测requester's location, language和当前locale 。 以下所有方法都显示请求者浏览器中设置的国家名称和语言名称。
S.No. | 方法和描述 |
---|---|
1 | String getCountry() 此方法以ISO 3166 2字母格式为此区域设置返回大写的国家/地区代码。 |
2 | String getDisplayCountry() 此方法返回适合显示给用户的区域设置国家/地区的名称。 |
3 | String getLanguage() 此方法以ISO 639格式为此语言环境返回小写的语言代码。 |
4 | String getDisplayLanguage() 此方法返回适合显示给用户的语言环境语言的名称。 |
5 | String getISO3Country() 此方法返回此区域设置国家/地区的三个字母缩写。 |
6 | String getISO3Language() 此方法返回此语言环境语言的三个字母缩写。 |
例子 (Example)
以下示例显示如何在JSP中显示请求的语言和关联国家/地区 -
<%@ page import = "java.io.*,java.util.Locale" %>
<%@ page import = "javax.servlet.*,javax.servlet.http.* "%>
<%
//Get the client's Locale
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>
语言设置
JSP可以输出用西欧语言编写的页面,如英语,西班牙语,德语,法语,意大利语,荷兰语等。这里设置Content-Language标题以正确显示所有字符非常重要。
另一个重点是使用HTML实体显示所有特殊字符; 例如, "ñ" 代表"ñ"和"¡" 代表"¡"如下 -
<%@ 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>
区域特定日期
您可以使用java.text.DateFormat类及其静态getDateTimeInstance( )方法来格式化特定于语言环境的日期和时间。 以下示例显示了如何格式化特定于给定语言环境的日期 -
<%@ 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>
区域特定货币
您可以使用java.txt.NumberFormat类及其静态getCurrencyInstance( )方法在特定于语言环境的curreny中格式化数字,例如long或double类型。 以下是显示如何格式化给定区域设置的货币的示例 -
<%@ 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>
区域特定百分比
您可以使用java.txt.NumberFormat类及其静态getPercentInstance( )方法来获取特定于语言环境的百分比。 以下示例显示如何格式化特定于给定语言环境的百分比 -
<%@ 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>