zoukankan      html  css  js  c++  java
  • Servlet实现国际化

    以下内容引用自http://wiki.jikexueyuan.com/project/servlet/internationalization.html

    三个重要术语:

    • 国际化(i18n):这意味着网站能够提供翻译成访问者的语言或国籍的不同版本的内容。

    • 本地化(l10n):这意味着向网站添加资源,使其适应特定的地理或文化区域,例如网站翻译成印地语。

    • 区域设置:这是一个特殊的文化或地理区域。它通常指语言符号后跟一个由下划线分隔的国家符号。例如"en_US"表示US的英语区域设置。

    Servlet可以根据请求者的区域设置读出相应版本的网站,并根据当地的语言、文化和需求提供相应的网站版本。以下是Request对象中的方法,它返回了Locale对象。

    java.util.Locale request.getLocale() 

    一、检测区域设置

    下面列出了重要的区域设置方法,可以使用它们来检测请求者的地理位置、语言和区域设置。下面所有的方法都显示了请求者浏览器中设置的国家名称和语言名称。

    方法描述

    String getCountry()

    该方法以2个大写字母形式的ISO3166格式返回该区域设置的国家/地区代码。

    String getDisplayCountry()

    该方法返回适合向用户显示的区域设置的国家的名称。

    String getLanguage()

    该方法以小写字母形式的ISO639格式返回该区域设置的语言代码。

    String getDisplayLanguage()

    该方法返回适合向用户显示的区域设置的语言的名称。

    String getISO3Country()

    该方法返回该区域设置的国家的三个字母缩写。

    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">
    ";
          out.println(docType +
            "<html>
    " +
            "<head><title>" + title + "</title></head>
    " +
            "<body bgcolor="#f0f0f0">
    " +
            "<h1 align="center">" + language + "</h1>
    " +
            "<h2 align="center">" + country + "</h2>
    " +
            "</body></html>");
      }
    } 

    二、语言设置

    Servlet可以输出以西欧语言编写的页面,如英语、西班牙语、德语、法语、意大利语、荷兰语等。在这里,设置Content-Language头信息来正确的显示所有字符是非常重要的。

    第二点是使用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">
    ";
         out.println(docType +
         "<html>
    " +
         "<head><title>" + title + "</title></head>
    " +
         "<body bgcolor="#f0f0f0">
    " +
         "<h1>" + "En Espa&ntilde;ol:" + "</h1>
    " +
         "<h1>" + "&iexcl;Hola Mundo!" + "</h1>
    " +
         "</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">
    ";
          out.println(docType +
          "<html>
    " +
          "<head><title>" + title + "</title></head>
    " +
          "<body bgcolor="#f0f0f0">
    " +
          "<h1 align="center">" + date + "</h1>
    " +
          "</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
      {
        // 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">
    ";
          out.println(docType +
          "<html>
    " +
          "<head><title>" + title + "</title></head>
    " +
          "<body bgcolor="#f0f0f0">
    " +
          "<h1 align="center">" + formattedCurr + "</h1>
    " +
          "</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
      {
        // 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">
    ";
          out.println(docType +
          "<html>
    " +
          "<head><title>" + title + "</title></head>
    " +
          "<body bgcolor="#f0f0f0">
    " +
          "<h1 align="center">" + formattedPerc + "</h1>
    " +
          "</body></html>");
      }
    } 

    测试工程:https://github.com/easonjim/5_java_example/tree/master/servletbasics/test18

  • 相关阅读:
    postman 的基础使用篇(一)
    C# struct
    细说javascript typeof操作符
    javascript-void keyword
    深圳求生记
    博客园开篇--对程序员的一点看法
    京东2018校园招聘 数据开发
    数据结构之哈希、哈希函数、哈希表
    scrapy入门教程
    linux学习笔记1
  • 原文地址:https://www.cnblogs.com/EasonJim/p/6986084.html
Copyright © 2011-2022 走看看