Servlet - 國際化



在我們繼續之前,讓我解釋三個重要的術語:

  • 國際化 (i18n) - 這意味著使網站能夠提供不同版本的翻譯內容,以適應訪問者的語言或國籍。

  • 本地化 (l10n) - 這意味著向網站新增資源,以適應特定的地理區域或文化區域。

  • 區域設定 (locale) - 這是一個特定的文化或地理區域。它通常指語言程式碼後跟國家/地區程式碼,兩者之間用下劃線分隔。例如,“en_US”表示美國的英語區域設定。

在構建全球性網站時,需要考慮許多因素。本教程不會提供這方面的完整細節,但它將提供一個很好的示例,說明如何透過區分訪問者的位置(即區域設定)為網際網路社群提供不同語言的網頁。

Servlet可以根據請求者的區域設定選擇合適的網站版本,並根據當地語言、文化和需求提供相應的網站版本。以下是request物件的返回Locale物件的方法。

java.util.Locale request.getLocale() 

檢測區域設定

以下是您可以用來檢測請求者位置、語言和區域設定的重要區域設定方法。以下所有方法都顯示在請求者瀏覽器中設定的國家/地區名稱和語言名稱。

序號 方法及描述
1

String getCountry()

此方法返回此區域設定的國家/地區程式碼(大寫),採用ISO 3166 2字母格式。

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 {
   
      //Get the client's Locale
      Locale locale = request.getLocale();
      String language = locale.getLanguage();
      String country = locale.getCountry();

      // Set response content type
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      String title = "Detecting Locale";
      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可以輸出用西歐語言(如英語、西班牙語、德語、法語、義大利語、荷蘭語等)編寫的頁面。這裡重要的是設定ContentLanguage標頭以正確顯示所有字元。

第二點是使用HTML實體顯示所有特殊字元,例如,“&#241;”表示“ñ”,“&#161;”表示“¡”,如下所示

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 {

      // Set response content type
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      // Set spanish language code.
      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>"
      );
   }
} 

特定區域設定的日期

您可以使用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 {
      
      // Set response content type
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      
      //Get the client's Locale
      Locale locale = request.getLocale( );
      String date = DateFormat.getDateTimeInstance(DateFormat.FULL, 
         DateFormat.SHORT, locale).format(new Date( ));

      String title = "Locale Specific Dates";
      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.txt.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 {

      // Set response content type
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();

      //Get the client's Locale
      Locale locale = request.getLocale( );
      NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
      String formattedCurr = nft.format(1000000);

      String title = "Locale Specific Currency";
      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.txt.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 {
      
      // Set response content type
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      
      //Get the client's Locale
      Locale locale = request.getLocale( );
      NumberFormat nft = NumberFormat.getPercentInstance(locale);
      String formattedPerc = nft.format(0.51);

      String title = "Locale Specific Percentage";
      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>"
      );
   }
} 
廣告