在我們開(kāi)始之前,先來(lái)看看三個(gè)重要術(shù)語(yǔ):
國(guó)際化(i18n):這意味著一個(gè)網(wǎng)站提供了不同版本的翻譯成訪問(wèn)者的語(yǔ)言或國(guó)籍的內(nèi)容。
本地化(l10n):這意味著向網(wǎng)站添加資源,以使其適應(yīng)特定的地理或文化區(qū)域,例如網(wǎng)站翻譯成印地文(Hindi)。
區(qū)域設(shè)置(locale):這是一個(gè)特殊的文化或地理區(qū)域。它通常指語(yǔ)言符號(hào)后跟一個(gè)下劃線和一個(gè)國(guó)家符號(hào)。例如 "en_US" 表示針對(duì) US 的英語(yǔ)區(qū)域設(shè)置。
當(dāng)建立一個(gè)全球性的網(wǎng)站時(shí)有一些注意事項(xiàng)。本教程不會(huì)講解這些注意事項(xiàng)的完整細(xì)節(jié),但它會(huì)通過(guò)一個(gè)很好的示例向您演示如何通過(guò)差異化定位(即區(qū)域設(shè)置)來(lái)讓網(wǎng)頁(yè)以不同語(yǔ)言呈現(xiàn)。
Servlet 可以根據(jù)請(qǐng)求者的區(qū)域設(shè)置拾取相應(yīng)版本的網(wǎng)站,并根據(jù)當(dāng)?shù)氐恼Z(yǔ)言、文化和需求提供相應(yīng)的網(wǎng)站版本。以下是 request 對(duì)象中返回 Locale 對(duì)象的方法。
java.util.Locale request.getLocale()
下面列出了重要的區(qū)域設(shè)置方法,您可以使用它們來(lái)檢測(cè)請(qǐng)求者的地理位置、語(yǔ)言和區(qū)域設(shè)置。下面所有的方法都顯示了請(qǐng)求者瀏覽器中設(shè)置的國(guó)家名稱和語(yǔ)言名稱。
序號(hào) | 方法 & 描述 |
---|---|
1 | String getCountry() 該方法以 2 個(gè)大寫字母形式的 ISO 3166 格式返回該區(qū)域設(shè)置的國(guó)家/地區(qū)代碼。 |
2 | String getDisplayCountry() 該方法返回適合向用戶顯示的區(qū)域設(shè)置的國(guó)家的名稱。 |
3 | String getLanguage() 該方法以小寫字母形式的 ISO 639 格式返回該區(qū)域設(shè)置的語(yǔ)言代碼。 |
4 | String getDisplayLanguage() 該方法返回適合向用戶顯示的區(qū)域設(shè)置的語(yǔ)言的名稱。 |
5 | String getISO3Country() 該方法返回該區(qū)域設(shè)置的國(guó)家的三個(gè)字母縮寫。 |
6 | String getISO3Language() 該方法返回該區(qū)域設(shè)置的語(yǔ)言的三個(gè)字母的縮寫。 |
本示例演示了如何顯示某個(gè)請(qǐng)求的語(yǔ)言和相關(guān)的國(guó)家:
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 { // 獲取客戶端的區(qū)域設(shè)置 Locale locale = request.getLocale(); String language = locale.getLanguage(); String country = locale.getCountry(); // 設(shè)置響應(yīng)內(nèi)容類型 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String title = "檢測(cè)區(qū)域設(shè)置"; 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>"); } }
Servlet 可以輸出以西歐語(yǔ)言(如英語(yǔ)、西班牙語(yǔ)、德語(yǔ)、法語(yǔ)、意大利語(yǔ)、荷蘭語(yǔ)等)編寫的頁(yè)面。在這里,為了能正確顯示所有的字符,設(shè)置 Content-Language 頭是非常重要的。
第二點(diǎn)是使用 HTML 實(shí)體顯示所有的特殊字符,例如,"ñ" 表示 "?","¡" 表示 "?",如下所示:
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 { // 設(shè)置響應(yīng)內(nèi)容類型 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); // 設(shè)置西班牙語(yǔ)言代碼 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>"); } }
您可以使用 java.text.DateFormat 類及其靜態(tài)方法 getDateTimeInstance() 來(lái)格式化特定于區(qū)域設(shè)置的日期和時(shí)間。下面的示例演示了如何格式化特定于某個(gè)給定的區(qū)域設(shè)置的日期:
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 { // 設(shè)置響應(yīng)內(nèi)容類型 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); // 獲取客戶端的區(qū)域設(shè)置 Locale locale = request.getLocale( ); String date = DateFormat.getDateTimeInstance( DateFormat.FULL, DateFormat.SHORT, locale).format(new Date( )); String title = "特定于區(qū)域設(shè)置的日期"; 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>"); } }
您可以使用 java.text.NumberFormat 類及其靜態(tài)方法 getCurrencyInstance() 來(lái)格式化數(shù)字(比如 long 類型或 double 類型)為特定于區(qū)域設(shè)置的貨幣。下面的示例演示了如何格式化特定于某個(gè)給定的區(qū)域設(shè)置的貨幣:
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 { // 設(shè)置響應(yīng)內(nèi)容類型 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); // 獲取客戶端的區(qū)域設(shè)置 Locale locale = request.getLocale( ); NumberFormat nft = NumberFormat.getCurrencyInstance(locale); String formattedCurr = nft.format(1000000); String title = "特定于區(qū)域設(shè)置的貨幣"; 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>"); } }
您可以使用 java.text.NumberFormat 類及其靜態(tài)方法 getPercentInstance() 來(lái)格式化特定于區(qū)域設(shè)置的百分比。下面的示例演示了如何格式化特定于某個(gè)給定的區(qū)域設(shè)置的百分比:
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 { // 設(shè)置響應(yīng)內(nèi)容類型 response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); // 獲取客戶端的區(qū)域設(shè)置 Locale locale = request.getLocale( ); NumberFormat nft = NumberFormat.getPercentInstance(locale); String formattedPerc = nft.format(0.51); String title = "特定于區(qū)域設(shè)置的百分比"; 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>"); } }