關於本篇文章
瞭解一些 servlet 概念,本編文章只進行一些文章格式修改,文章內使用的圖片均來自 Runoob 網站
致謝
感謝 Runoob 提供的相關資料,官方資料地址Servlet 教程
Servlet 國際化
在我們開始之前,先來看看三個重要術語:
- **國際化(i18n):**這意味着一個網站提供了不同版本的翻譯成訪問者的語言或國籍的內容。
- **本地化(l10n):**這意味着向網站添加資源,以使其適應特定的地理或文化區域,例如網站翻譯成印地文(Hindi)。
- **區域設置(locale):**這是一個特殊的文化或地理區域。它通常指語言符號後跟一個下劃線和一個國家符號。例如 “en_US” 表示針對 US 的英語區域設置。
當建立一個全球性的網站時有一些注意事項。本教程不會講解這些注意事項的完整細節,但它會通過一個很好的實例向您演示如何通過差異化定位(即區域設置)來讓網頁以不同語言呈現。
Servlet 可以根據請求者的區域設置拾取相應版本的網站,並根據當地的語言、文化和需求提供相應的網站版本。以下是 request 對象中返回 Locale 對象的方法。
java.util.Locale request.getLocale()
檢測區域設置
下面列出了重要的區域設置方法,您可以使用它們來檢測請求者的地理位置、語言和區域設置。下面所有的方法都顯示了請求者瀏覽器中設置的國家名稱和語言名稱。
序號 | 方法 & 描述 |
---|---|
1 | String getCountry() 該方法以 2 個大寫字母形式的 ISO 3166 格式返回該區域設置的國家/地區代碼。 |
2 | String getDisplayCountry() 該方法返回適合向用戶顯示的區域設置的國家的名稱。 |
3 | String getLanguage() 該方法以小寫字母形式的 ISO 639 格式返回該區域設置的語言代碼。 |
4 | String getDisplayLanguage() 該方法返回適合向用戶顯示的區域設置的語言的名稱。 |
5 | String getISO3Country() 該方法返回該區域設置的國家的三個字母縮寫。 |
6 | String getISO3Language() 該方法返回該區域設置的語言的三個字母的縮寫。 |
實例
本實例演示瞭如何顯示某個請求的語言和相關的國家:
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
{
// 獲取客戶端的區域設置
Locale locale = request.getLocale();
String language = locale.getLanguage();
String country = locale.getCountry();
// 設置響應內容類型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String title = "檢測區域設置";
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 可以輸出以西歐語言(如英語、西班牙語、德語、法語、意大利語、荷蘭語等)編寫的頁面。在這裏,爲了能正確顯示所有的字符,設置 Content-Language 頭是非常重要的。
第二點是使用 HTML 實體顯示所有的特殊字符,例如,“ñ” 表示 “ñ”,"¡" 表示 “¡”,如下所示:
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
{
// 設置響應內容類型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 設置西班牙語言代碼
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 類及其靜態方法 getDateTimeInstance() 來格式化特定於區域設置的日期和時間。下面的實例演示瞭如何格式化特定於某個給定的區域設置的日期:
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
{
// 設置響應內容類型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 獲取客戶端的區域設置
Locale locale = request.getLocale( );
String date = DateFormat.getDateTimeInstance(
DateFormat.FULL,
DateFormat.SHORT,
locale).format(new Date( ));
String title = "特定於區域設置的日期";
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 類及其靜態方法 getCurrencyInstance() 來格式化數字(比如 long 類型或 double 類型)爲特定於區域設置的貨幣。下面的實例演示瞭如何格式化特定於某個給定的區域設置的貨幣:
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
{
// 設置響應內容類型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 獲取客戶端的區域設置
Locale locale = request.getLocale( );
NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
String formattedCurr = nft.format(1000000);
String title = "特定於區域設置的貨幣";
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 類及其靜態方法 getPercentInstance() 來格式化特定於區域設置的百分比。下面的實例演示瞭如何格式化特定於某個給定的區域設置的百分比:
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
{
// 設置響應內容類型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 獲取客戶端的區域設置
Locale locale = request.getLocale( );
NumberFormat nft = NumberFormat.getPercentInstance(locale);
String formattedPerc = nft.format(0.51);
String title = "特定於區域設置的百分比";
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>");
}
}