 
- Servlet 教程
- Servlet - 主页
- Servlet - 概述
- Servlet - 环境设置
- Servlet - 生命周期
- Servlet - 示例
- Servlet - 表单数据
- Servlet - 客户端请求
- Servlet - 服务器响应
- Servlet - Http 代码
- Servlet - 编写过滤器
- Servlet - 例外
- Servlet - Cookie 处理
- Servlet - 会话跟踪
- Servlet - 数据库访问
- Servlet - 文件上传
- Servlet - 处理日期
- Servlet - 页面重定向
- Servlet - 点击计数器
- Servlet - 自动刷新
- Servlet - 发送电子邮件
- Servlet - 包装
- Servlet - 调试
- Servlet - 国际化
- Servlet - 注释
- Servlet 有用资源
- Servlet - 问题与解答
- Servlet - 快速指南
- Servlet - 有用的资源
- Servlet - 讨论
Servlet - 国际化
在我们继续之前,让我解释一下三个重要术语 -
- 国际化(i18n) - 这意味着使网站能够提供翻译成访问者语言或国籍的不同版本的内容 
- 本地化 (l10n) - 这意味着向网站添加资源以适应特定的地理或文化区域。 
- locale - 这是一个特定的文化或地理区域。它通常被称为语言符号,后跟国家/地区符号,并用下划线分隔。例如,“en_US”代表美国的英语语言环境。 
建立全球网站时应注意许多事项。本教程不会为您提供有关此问题的完整详细信息,但它会为您提供一个很好的示例,说明如何通过区分不同的位置(即区域设置)向互联网社区提供不同语言的网页。
Servlet 可以根据请求者的区域设置获取适当的站点版本,并根据当地语言、文化和要求提供适当的站点版本。以下是请求对象的方法,该方法返回 Locale 对象。
java.util.Locale request.getLocale()
检测区域设置
以下是重要的区域设置方法,您可以使用它们来检测请求者的位置、语言,当然还有区域设置。以下所有方法都会显示请求者浏览器中设置的国家/地区名称和语言名称。
| 先生。 | 方法及说明 | 
|---|---|
| 1 | 字符串 getCountry() 此方法以 ISO 3166 2 字母格式返回该区域设置的大写国家/地区代码。 | 
| 2 | 字符串 getDisplayCountry() 此方法返回适合向用户显示的区域设置国家/地区的名称。 | 
| 3 | 字符串 getLanguage() 此方法以 ISO 639 格式返回该区域设置的小写语言代码。 | 
| 4 | 字符串 getDisplayLanguage() 此方法返回适合向用户显示的区域设置语言的名称。 | 
| 5 | 字符串 getISO3Country() 此方法返回该语言环境所在国家/地区的三个字母的缩写。 | 
| 6 | 字符串 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实体显示所有特殊字符,例如“ñ” 代表“ñ”和“¡” 表示“¡”如下:
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ñ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 类及其静态 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>"
      );
   }
} 
