Servlet 國際化

2022-07-23 14:28 更新

Servlet 國際化

在我們開始之前,先來看看三個重要術(shù)語:

  • 國際化(i18n):這意味著一個網(wǎng)站提供了不同版本的翻譯成訪問者的語言或國籍的內(nèi)容。
  • 本地化(l10n):這意味著向網(wǎng)站添加資源,以使其適應(yīng)特定的地理或文化區(qū)域,例如網(wǎng)站翻譯成印地文(Hindi)。
  • 區(qū)域設(shè)置(locale):這是一個特殊的文化或地理區(qū)域。它通常指語言符號后跟一個下劃線和一個國家符號。例如 "en_US" 表示針對 US 的英語區(qū)域設(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è)置方法,您可以使用它們來檢測請求者的地理位置、語言和區(qū)域設(shè)置。下面所有的方法都顯示了請求者瀏覽器中設(shè)置的國家名稱和語言名稱。

序號方法 & 描述
1String getCountry()
該方法以 2 個大寫字母形式的 ISO 3166 格式返回該區(qū)域設(shè)置的國家/地區(qū)代碼。
2String getDisplayCountry()
該方法返回適合向用戶顯示的區(qū)域設(shè)置的國家的名稱。
3String getLanguage()
該方法以小寫字母形式的 ISO 639 格式返回該區(qū)域設(shè)置的語言代碼。
4String getDisplayLanguage()
該方法返回適合向用戶顯示的區(qū)域設(shè)置的語言的名稱。
5String getISO3Country()
該方法返回該區(qū)域設(shè)置的國家的三個字母縮寫。
6String getISO3Language()
該方法返回該區(qū)域設(shè)置的語言的三個字母的縮寫。

實(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>");
  }
} 

語言設(shè)置

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&ntilde;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&ntilde;ol:" + "</h1>\n" +
     "<h1>" + "&iexcl;Hola Mundo!" + "</h1>\n" +
     "</body></html>");
  }
} 

特定于區(qū)域設(shè)置的日期

您可以使用 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>");
  }
} 

特定于區(qū)域設(shè)置的貨幣

您可以使用 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>");
  }
} 

特定于區(qū)域設(shè)置的百分比

您可以使用 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>");
  }
} 
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號