top
Loading...
JSP 國際化

JSP 國際化

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

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

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

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

java.util.Locale request.getLocale() 

檢測Locale

下表列舉出了Locale對象中比較重要的方法,用於檢測request對象的地區,語言,和區域。所有這些方法都會在瀏覽器中顯示國家名稱和語言名稱:

序號 方法 & 描述
1 String getCountry()

返回國家/地區碼的英文大寫,或 ISO 3166 2-letter 格式的區域
2 String getDisplayCountry()

返回要顯示給用戶的國家名稱
3 String getLanguage()

返回語言碼的英文小寫,或ISO 639 格式的區域
4 String getDisplayLanguage()

返回要給用戶看的語言名稱
5 String getISO3Country()

返回國家名稱的3字母縮寫
6 String getISO3Language()

返回語言名稱的3字母縮寫

這個例子告訴我們如何在JSP中顯示語言和國家:

<%@ 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可以使用西歐語言來輸出一個頁面,比如英語,西班牙語,德語,法語,意大利語等等。由此可見,設置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>

區域特定日期

可以使用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.text.NumberFormat類和它的靜態方法getCurrencyInstance()來格式化數字。比如在區域特定貨幣中的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.text.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>
北斗有巢氏 有巢氏北斗