非常教程

JSP 教程教程

JSP 国际化

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>
JSP 国际化
JSP 教程

JSP,即 Java Server Pages,是由 Sun Microsystems 公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户端请求,而动态生成 HTML、XML 或其他格式文档的 Web 网页的技术标准。

JSP 教程目录

1.JSP 简介
2.JSP 教程
3.JSP 隐式对象
4.JSP 指令
5.JSP 发送邮件
6.JSP 自动刷新
7.JSP 页面重定向
8.JSP 日期处理
9.JSP 国际化
10.JSP 调试
11.JSP 自定义标签
12.<c:forEach>, <c:forTokens> 标签
13.<c:import> 标签
14.<c:choose>, <c:when>, <c:otherwise> 标签
15.<c:if> 标签
16.<c:catch> 标签
17.<c:remove> 标签
18.<c:set> 标签
19.<c:out> 标签
20.<fmt:setBundle> 标签
21.<fmt:setLocale> 标签
22.<fmt:bundle> 标签
23.<fmt:parseDate> 标签
24.<fmt:formatDate> 标签
25.<fmt:parseNumber> 标签
26.<fmt:formatNumber> 标签
27.<c:redirect> 标签
28.<c:url> 标签
29.<c:param> 标签
30.<sql:transaction> 标签
31.<sql:dateParam> 标签
32.<sql:param> 标签
33.<sql:update> 标签
34.<sql:query> 标签
35.<sql:setDataSource> 标签
36.<fmt:requestEncoding> 标签
37.<fmt:message> 标签
38.<fmt:setTimeZone> 标签
39.<fmt:timeZone> 标签
40.JSTL fn:containsIgnoreCase()函数
41.JSTL fn:contains()函数
42.<x:param> 标签
43.<x:transform> 标签
44.<x:choose>, <x:when>, <x:otherwise> 标签
45.<x:forEach> 标签
46.<x:if> 标签
47.<x:set> 标签
48.<x:parse> 标签
49.<x:out> 标签
50.JSTL fn:substringAfter()函数
51.JSTL fn:substring()函数
52.JSTL fn:startsWith()函数
53.JSTL fn:split()函数
54.JSTL fn:replace()函数
55.JSTL fn:length()函数
56.JSTL fn:join()函数
57.JSTL fn:indexOf()函数
58.JSTL fn:escapeXml()函数
59.JSTL fn:endsWith()函数
60.JSP 标准标签库(JSTL)
61.JSTL fn:trim()函数
62.JSTL fn:toUpperCase()函数
63.JSTL fn:toLowerCase()函数
64.JSTL fn:substringBefore()函数