JSP 國際化

在開始前,需要解釋幾個重要的概念:

  • 國際化(i18n):表明一個頁面根據(jù)訪問者的語言或國家來呈現(xiàn)不同的翻譯版本。
  • 本地化(l10n):向網(wǎng)站添加資源,以使它適應(yīng)不同的地區(qū)和文化。比如網(wǎng)站的印度語版本。
  • 區(qū)域:這是一個特定的區(qū)域或文化,通常認(rèn)為是一個語言標(biāo)志和國家標(biāo)志通過下劃線連接起來。比如"en_US"代表美國英語地區(qū)。

如果想要建立一個全球化的網(wǎng)站,就需要關(guān)心一系列項目。本章將會詳細(xì)告訴您如何處理國際化問題,并給出了一些例子來加深理解。

JSP容器能夠根據(jù)request的locale屬性來提供正確地頁面版本。接下來給出了如何通過request對象來獲得Locale對象的語法:

java.util.Locale request.getLocale() 

檢測Locale

下表列舉出了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>

語言設(shè)置

JSP 可以使用西歐語言來輸出一個頁面,比如英語,西班牙語,德語,法語,意大利語等等。由此可見,設(shè)置 Content-Language 信息頭來正確顯示所有字符是很重要的。

第二點就是,需要使用 HTML 字符實體來顯示特殊字符,比如 "&#241;" 代表的是 ñ,"&#161;"代表的是 ¡ :

<%@ 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()來格式化日期和時間。接下來的這個例子顯示了如何根據(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>

區(qū)域特定貨幣

可以使用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>

區(qū)域特定百分比

可以使用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>
丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清