在開(kāi)始前,需要解釋幾個(gè)重要的概念:
● 國(guó)際化(i18n):表明一個(gè)頁(yè)面根據(jù)訪問(wèn)者的語(yǔ)言或國(guó)家來(lái)呈現(xiàn)不同的翻譯版本。
● 本地化(l10n):向網(wǎng)站添加資源,以使它適應(yīng)不同的地區(qū)和文化。比如網(wǎng)站的印度語(yǔ)版本。
● 區(qū)域:這是一個(gè)特定的區(qū)域或文化,通常認(rèn)為是一個(gè)語(yǔ)言標(biāo)志和國(guó)家標(biāo)志通過(guò)下劃線連接起來(lái)。比如"en_US"代表美國(guó)英語(yǔ)地區(qū)。
如果想要建立一個(gè)全球化的網(wǎng)站,就需要關(guān)心一系列項(xiàng)目。本章將會(huì)詳細(xì)告訴您如何處理國(guó)際化問(wèn)題,并給出了一些例子來(lái)加深理解。
JSP容器能夠根據(jù)request的locale屬性來(lái)提供正確地頁(yè)面版本。接下來(lái)給出了如何通過(guò)request對(duì)象來(lái)獲得Locale對(duì)象的語(yǔ)法:
java.util.Locale request.getLocale()
檢測(cè)Locale
下表列舉出了Locale對(duì)象中比較重要的方法,用于檢測(cè)request對(duì)象的地區(qū),語(yǔ)言,和區(qū)域。所有這些方法都會(huì)在瀏覽器中顯示國(guó)家名稱和語(yǔ)言名稱:
序號(hào) |
方法 & 描述 |
---|---|
1 |
String getCountry() 返回國(guó)家/地區(qū)碼的英文大寫(xiě),或 ISO 3166 2-letter 格式的區(qū)域 |
2 |
String getDisplayCountry() 返回要顯示給用戶的國(guó)家名稱 |
3 |
String getLanguage() 返回語(yǔ)言碼的英文小寫(xiě),或ISO 639 格式的區(qū)域 |
4 |
String getDisplayLanguage() 返回要給用戶看的語(yǔ)言名稱 |
5 |
String getISO3Country() 返回國(guó)家名稱的3字母縮寫(xiě) |
6 |
String getISO3Language() 返回語(yǔ)言名稱的3字母縮寫(xiě) |
實(shí)例演示
這個(gè)例子告訴我們?nèi)绾卧贘SP中顯示語(yǔ)言和國(guó)家:
<%@ 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>
語(yǔ)言設(shè)置
JSP可以使用西歐語(yǔ)言來(lái)輸出一個(gè)頁(yè)面,比如英語(yǔ),西班牙語(yǔ),德語(yǔ),法語(yǔ),意大利語(yǔ)等等。由此可見(jiàn),設(shè)置Content-Language信息頭來(lái)正確顯示所有字符是很重要的。
第二點(diǎn)就是,需要使用HTML字符實(shí)體來(lái)顯示特殊字符,比如"ñ" 代表的是"?","¡"代表的是 "?" :
<%@ 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>
區(qū)域特定日期
可以使用java.text.DateFormat類和它的靜態(tài)方法getDateTimeInstance()來(lái)格式化日期和時(shí)間。接下來(lái)的這個(gè)例子顯示了如何根據(jù)指定的區(qū)域來(lái)格式化日期和時(shí)間:
<%@ 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>
區(qū)域特定貨幣
可以使用java.text.NumberFormat類和它的靜態(tài)方法getCurrencyInstance()來(lái)格式化數(shù)字。比如在區(qū)域特定貨幣中的long型和double型。接下來(lái)的例子顯示了如何根據(jù)指定的區(qū)域來(lái)格式化貨幣:
<%@ 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>
區(qū)域特定百分比
可以使用java.text.NumberFormat類和它的靜態(tài)方法getPercentInstance()來(lái)格式化百分比。接下來(lái)的例子告訴我們?nèi)绾胃鶕?jù)指定的區(qū)域來(lái)格式化百分比:
<%@ 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>