在我們開始之前,先來看看三個重要術(shù)語:
當(dāng)建立一個全球性的網(wǎng)站時(shí)有一些注意事項(xiàng)。本教程不會講解這些注意事項(xiàng)的完整細(xì)節(jié),但它會通過一個很好的實(shí)例向您演示如何通過差異化定位(即區(qū)域設(shè)置)來讓網(wǎng)頁以不同語言呈現(xiàn)。
Servlet 可以根據(jù)請求者的區(qū)域設(shè)置拾取相應(yīng)版本的網(wǎng)站,并根據(jù)當(dāng)?shù)氐恼Z言、文化和需求提供相應(yīng)的網(wǎng)站版本。以下是 request 對象中返回 Locale 對象的方法。
java.util.Locale request.getLocale()
下面列出了重要的區(qū)域設(shè)置方法,您可以使用它們來檢測請求者的地理位置、語言和區(qū)域設(shè)置。下面所有的方法都顯示了請求者瀏覽器中設(shè)置的國家名稱和語言名稱。
序號 | 方法 & 描述 |
---|---|
1 | String getCountry() 該方法以 2 個大寫字母形式的 ISO 3166 格式返回該區(qū)域設(shè)置的國家/地區(qū)代碼。 |
2 | String getDisplayCountry() 該方法返回適合向用戶顯示的區(qū)域設(shè)置的國家的名稱。 |
3 | String getLanguage() 該方法以小寫字母形式的 ISO 639 格式返回該區(qū)域設(shè)置的語言代碼。 |
4 | String getDisplayLanguage() 該方法返回適合向用戶顯示的區(qū)域設(shè)置的語言的名稱。 |
5 | String getISO3Country() 該方法返回該區(qū)域設(shè)置的國家的三個字母縮寫。 |
6 | String getISO3Language() 該方法返回該區(qū)域設(shè)置的語言的三個字母的縮寫。 |
本實(shí)例演示了如何顯示某個請求的語言和相關(guān)的國家:
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");
PrintWriter out = response.getWriter();
String title = "檢測區(qū)域設(shè)置";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\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 可以輸出以西歐語言(如英語、西班牙語、德語、法語、意大利語、荷蘭語等)編寫的頁面。在這里,為了能正確顯示所有的字符,設(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");
PrintWriter out = response.getWriter();
// 設(shè)置西班牙語言代碼
response.setHeader("Content-Language", "es");
String title = "En Español";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\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() 來格式化特定于區(qū)域設(shè)置的日期和時(shí)間。下面的實(shí)例演示了如何格式化特定于某個給定的區(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");
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 public \"-//w3c//dtd html 4.0 " + "transitional//en\">\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() 來格式化數(shù)字(比如 long 類型或 double 類型)為特定于區(qū)域設(shè)置的貨幣。下面的實(shí)例演示了如何格式化特定于某個給定的區(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");
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 public \"-//w3c//dtd html 4.0 " + "transitional//en\">\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() 來格式化特定于區(qū)域設(shè)置的百分比。下面的實(shí)例演示了如何格式化特定于某個給定的區(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");
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 public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + formattedPerc + "</h1>\n" +
"</body></html>");
}
}
更多建議: