JSP 国际化

jsp 国际化

在开始前,需要解释几个重要的概念:

  • 国际化(i18n):表明一个页面根据访问者的语言或国家来呈现不同的翻译版本。
  • 本地化(l10n):向网站添加资源,以使它适应不同的地区和文化。比如网站的印度语版本。
  • 区域:这是一个特定的区域或文化,通常认为是一个语言标志和国家标志通过下划线连接起来。比如"en_us"代表美国英语地区。

如果想要建立一个全球化的网站,就需要关心一系列项目。本章将会详细告诉您如何处理国际化问题,并给出了一些例子来加深理解。

jsp容器能够根据request的locale属性来提供正确地页面版本。接下来给出了如何通过request对象来获得locale对象的语法:

java.util.locale request.getlocale() 

检测locale

下表列举出了locale对象中比较重要的方法,用于检测request对象的地区,语言,和区域。所有这些方法都会在浏览器中显示国家名称和语言名称:

序号 方法 & 描述
1 string getcountry()

返回国家/地区码的英文大写,或 iso 3166 2-letter 格式的区域
2 string getdisplaycountry()

返回要显示给用户的国家名称
3 string getlanguage()

返回语言码的英文小写,或iso 639 格式的区域
4 string getdisplaylanguage()

返回要给用户看的语言名称
5 string getiso3country()

返回国家名称的3字母缩写
6 string getiso3language()

返回语言名称的3字母缩写

实例演示

这个例子告诉我们如何在jsp中显示语言和国家:

<%@ page import="java.io.*,java.util.locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%
   //获取客户端本地化信息
   locale locale = request.getlocale();
   string language = locale.getlanguage();
   string country = locale.getcountry();
%>
<html>
<head>
<title>detecting locale</title>
</head>
<body>
<center>
<h1>detecting locale</h1>
</center>
<p align="center">
<% 
   out.println("language : " + language  + "<br />");
   out.println("country  : " + country   + "<br />");
%>
</p>
</body>
</html>

语言设置

jsp 可以使用西欧语言来输出一个页面,比如英语,西班牙语,德语,法语,意大利语等等。由此可见,设置 content-language 信息头来正确显示所有字符是很重要的。

第二点就是,需要使用 html 字符实体来显示特殊字符,比如 "&#241;" 代表的是 ñ,"&#161;"代表的是 ¡ :

<%@ page import="java.io.*,java.util.locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%
    // set response content type
    response.setcontenttype("text/html");
    // set spanish language code.
    response.setheader("content-language", "es");
    string title = "en espa?ol";

%>
<html>
<head>
<title><%  out.print(title); %></title>
</head>
<body>
<center>
<h1><%  out.print(title); %></h1>
</center>
<div align="center">
<p>en espa?ol</p>
<p>?hola mundo!</p>
</div>
</body>
</html>

区域特定日期

可以使用java.text.dateformat类和它的静态方法getdatetimeinstance()来格式化日期和时间。接下来的这个例子显示了如何根据指定的区域来格式化日期和时间:

<%@ page import="java.io.*,java.util.locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%@ page import="java.text.dateformat,java.util.date" %>

<%
    string title = "locale specific dates";
    //get the client's locale
    locale locale = request.getlocale( );
    string date = dateformat.getdatetimeinstance(
                                  dateformat.full, 
                                  dateformat.short, 
                                  locale).format(new date( ));
%>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>local date: <%  out.print(date); %></p>
</div>
</body>
</html>

区域特定货币

可以使用java.text.numberformat类和它的静态方法getcurrencyinstance()来格式化数字。比如在区域特定货币中的long型和double型。接下来的例子显示了如何根据指定的区域来格式化货币:

<%@ page import="java.io.*,java.util.locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%@ page import="java.text.numberformat,java.util.date" %>

<%
    string title = "locale specific currency";
    //get the client's locale
    locale locale = request.getlocale( );
    numberformat nft = numberformat.getcurrencyinstance(locale);
    string formattedcurr = nft.format(1000000);
%>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>formatted currency: <%  out.print(formattedcurr); %></p>
</div>
</body>
</html>

区域特定百分比

可以使用java.text.numberformat类和它的静态方法getpercentinstance()来格式化百分比。接下来的例子告诉我们如何根据指定的区域来格式化百分比:

<%@ page import="java.io.*,java.util.locale" %>
<%@ page import="javax.servlet.*,javax.servlet.http.* "%>
<%@ page import="java.text.numberformat,java.util.date" %>

<%
    string title = "locale specific percentage";
    //get the client's locale
    locale locale = request.getlocale( );
    numberformat nft = numberformat.getpercentinstance(locale);
    string formattedperc = nft.format(0.51);
%>
<html>
<head>
<title><% out.print(title); %></title>
</head>
<body>
<center>
<h1><% out.print(title); %></h1>
</center>
<div align="center">
<p>formatted percentage: <%  out.print(formattedperc); %></p>
</div>
</body>
</html>
相关文章