在開始前,需要解釋幾個重要的概念:
如果想要建立一個全球化的網(wǎng)站,就需要關(guān)心一系列項目。本章將會詳細(xì)告訴您如何處理國際化問題,并給出了一些例子來加深理解。
JSP容器能夠根據(jù)request的locale屬性來提供正確地頁面版本。接下來給出了如何通過request對象來獲得Locale對象的語法:
java.util.Locale request.getLocale()
下表列舉出了Locale對象中比較重要的方法,用于檢測request對象的地區(qū),語言,和區(qū)域。所有這些方法都會在瀏覽器中顯示國家名稱和語言名稱:
序號 | 方法 & 描述 |
---|---|
1 | String getCountry() 返回國家/地區(qū)碼的英文大寫,或 ISO 3166 2-letter 格式的區(qū)域 |
2 | String getDisplayCountry() 返回要顯示給用戶的國家名稱 |
3 | String getLanguage() 返回語言碼的英文小寫,或ISO 639 格式的區(qū)域 |
4 | String getDisplayLanguage() 返回要給用戶看的語言名稱 |
5 | String getISO3Country() 返回國家名稱的3字母縮寫 |
6 | String getISO3Language() 返回語言名稱的3字母縮寫 |
這個例子告訴我們?nèi)绾卧贘SP中顯示語言和國家:
<%@ 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>
JSP 可以使用西歐語言來輸出一個頁面,比如英語,西班牙語,德語,法語,意大利語等等。由此可見,設(shè)置 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類和它的靜態(tài)方法getDateTimeInstance()來格式化日期和時間。接下來的這個例子顯示了如何根據(jù)指定的區(qū)域來格式化日期和時間:
<%@ 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.text.NumberFormat類和它的靜態(tài)方法getCurrencyInstance()來格式化數(shù)字。比如在區(qū)域特定貨幣中的long型和double型。接下來的例子顯示了如何根據(jù)指定的區(qū)域來格式化貨幣:
<%@ 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.text.NumberFormat類和它的靜態(tài)方法getPercentInstance()來格式化百分比。接下來的例子告訴我們?nèi)绾胃鶕?jù)指定的區(qū)域來格式化百分比:
<%@ 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>