当前位置: 代码迷 >> Web前端 >> 国际标签(2)
  详细解决方案

国际标签(2)

热度:371   发布时间:2012-11-26 11:48:50.0
国际标签(二)

国际标签(二)

一、动态数据的国际化

数值,货币,时间,日期等数据由于可能在程序运行时动态产生,所以无法像文字一样简单地将它们从应用程序中分离出来,而是需要特殊处理。Java 中提供了解决这些问题的 API 类(位于 java.util 包和 java.text 包中)

1、Locale 类

Locale 实例对象代表一个特定的地理,政治、文化区域

一个 Locale 对象本身不会验证它代表的语言和国家地区信息是否正确,只是向本地敏感的类提供国家地区信息,与国际化相关的格式化和解析任务由本地敏感的类去完成。(若JDK中的某个类在运行时需要根据 Locale 对象来调整其功能,这个类就称为本地敏感类)

与前面的Core标签一样,在使用标签前需要先导入标签库,在JSP页面中导入国际化标签库的语法如下:

<%@ taglib uri=" http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

二、几种主要的国际化标签

在JSTL之中的国际化标签主要包括<fmt:setLocale>、<fmt:bundle>、<fmt:setBundle>、<fmt:message>、<fmt:setLocale>:指定Locale环境。

<fmt:bundle>:指定消息资源使用的文件。

<fmt:setBundle>:设置消息资源文件。

<fmt:message>:显示消息资源文件中指定key的消息,支持带参数消息。

1、<fmt:setLocale>

<fmt:setLocale>标签用于在JSP页面中显式地设置用户的本地化信息,并将设置的本地化信息以Locale对象的形式保存在某个Web域中,其在Web域中的属性名称为“javax.servlet.jsp.jstl.fmt.locale”。使用<fmt:setLocale>标签设置本地化信息后,国际化标签库中的其他标签将使用该本地化信息,而忽略客户端浏览器传递过来的本地信息。

<fmt:setLocale value="locale"

[variant="variant"]

[scope="{page|request|session|application}"] /> 

<fmt:setBundle>

<fmt:setBundle>标签用于根据<fmt:setLocale>标签设置的本地化信息创建一个资源包(ResourceBundle)实例对象,并将其绑定到一个Web域的属性上。

标签的语法格式如下:

<fmt:setBundle basename="basename"

[var="varName"]

[scope="{page|request|session|application}"] />

2、<fmt:bundle>标签

<fmt:bundle>标签与<fmt:setBundle>标签的功能类似,但它创建的ResourceBundle实例对象只在其标签体内有效。

<fmt:bundle basename="basename“ >

body content

</fmt:bundle>

3、<fmt:message>标签

    <fmt:message>标签用于从一个资源包中读取信息并进行格式化输出。

 <fmt:message key="messageKey"

[bundle="resourceBundle"]

[var="varName"]   [scope="{page|request|session|application}"] />

实例:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<html>

<head>

<title>My JSP '5.jsp' starting page</title>

</head>

<fmt:setLocale value="en"/>

  <fmt:setBundle basename="com.hbsi.resource.propertities.Myproperties"/>

  <body>

    <marquee behavior="scroll">

     <font size="8" color="red">提交表单</font>

     </marquee>

     <form>

     <table border="1px" align="center">

     <tr>

     <td><fmt:message key="userName"/></td>

     <td><input type="text" name="username"/></td>

     </tr>

     <tr>

     <td><fmt:message key="passWord"/></td>

     <td><input type="password" name="password"/></td>

     </tr>

     <tr>

     <td><fmt:message key="age"/></td>

     <td><input type="password" name="age"/></td>

     </tr>

     <tr align="center">

     <td colspan="2">

     <input type="submit" value="<fmt:message key="submit"/>">

     <input type="reset" value="<fmt:message key="reset"/>">

     </td>

     </tr>

     </table>

     </form>

  </body>

</html>

 

三、国际化标签示例

实现页面的国际化效果需要按以下步骤进行。

创建页面需要的资源文件labels_en.properties、labels_zh.properties 

将资源文件内容转码。

创建页面文件。

将labels_en.properties、labels_zh_CN.properties两个文件存放在项目的src目录下。新建名为fmt_setbundle.jsp的页面。

实现国际化效果还可以使用<fmt:bundle>标签,通过以下步骤可以完成此效果。

修改页面文件。

设置语言选项。执行“开始”→“程序”中的“Internet Explorer”命令,打开IE浏览器,选择“工具”→“浏览器选项”菜单命令,在弹出的“Internet选项”对话框中单击“语言”按钮,修改语言的首选项。

启动容器。启动Tomcat,输入“http://localhost:8080/jstldemo/fmt_ setbundle.jsp”,如果设置的语言的首选项为英语,则显示英文版的页面,如果首选语言为中文,则显示中文页面。

四、DateFormat类(国际化日期)

DateFormat 类可以将一个日期/时间对象格式化为表示某个国家地区的日期/时间字符串。

DateFormat 类除了可按国家地区格式化输出日期外,它还定义了一些用于描述日期/时间的显示模式的 int 型的常量,包括FULL, LONG, MEDIUM, DEFAULT, SHORT,实例化DateFormat对象时,可以使用这些常量,控制日期/时间的显示长度。

五、DateFormat类

例如,对于日期/时间“2005年9月15日下午4时41分20秒”,如设置了DateFormat的显示模式,则日期/时间显示样式分别为:

SHORT模式完全是数字的,在中文环境下显示为“05-9-15 下午4:41”;在英文环境下为“9/15/05 4:41 PM”。

MEDIUM模式比SHORT模式长些,在中文环境显示为“2005-9-15 16:41:20”;在英文环境下显示为“Sep 15, 2005 4:41:20 PM”。

LONG模式比MEDIUM模式更长一些,在中文环境下显示为“2005年9月15日 下午04时41分20秒”;在英文环境下显示为“September 15, 2005 4:41:20 PM CST”。

FULL模式指定日期/时间的完整格式,在中文环境下显示为“2005年9月15日 星期四 下午04时41分20秒 CST”;在英文环境下,这个日期/时间显示为“Thursday, September 15, 2005 4:41:20 PM CST”。

六、实例化DateFormat类

实例化DateFormat类有九种方式,以下三种为带参形式,下面列出的三种方式也可以分别不带参,或只带显示样式的参数。

getDateInstance(int style, Locale aLocale):以指定的日期显示模式和本地信息来获得DateFormat实例对象,该实例对象不处理时间值部分。

getTimeInstance(int style, Locale aLocale):以指定的时间显示模式和本地信息来获得DateFormat实例对象,该实例对象不处理日期值部分。

getDateTimeInstance(int dateStyle, int timeStyle, Locale aLocale):以单独指定的日期显示模式、时间显示模式和本地信息来获得DateFormat实例对象。

七、DateFormat类

DateFormat 对象的方法:

format: 将date对象格式化为符合某个本地环境习惯的字符串。

parse:将字符串解析为日期/时间对象

注意:parse和format完全相反,一个是把date时间转化为相应地区和国家的显示样式,一个是把相应地区的时间日期转化成date对象,该方法在使用时,解析的时间或日期要符合指定的国家、地区格式,否则会抛异常。

DateFormat 对象通常不是线程安全的,每个线程都应该创建自己的 DateFormat  实例对象

八、NumberFormat类

NumberFormat 可以将一个数值格式化为符合某个国家地区习惯的数值字符串,也可以将符合某个国家地区习惯的数值字符串解析为对应的数值

NumberFormat 类的方法:

format 方法:将一个数值格式化为符合某个国家地区习惯的数值字符串

parse 方法:将符合某个国家地区习惯的数值字符串解析为对应的数值。

实例化NumberFormat类时,可以使用locale对象作为参数,也可以不使用,下面列出的是使用参数的。

getNumberInstance(Locale locale):以参数locale对象所标识的本地信息来获得具有多种用途的NumberFormat实例对象

getIntegerInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理整数的NumberFormat实例对象

getCurrencyInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理货币的NumberFormat实例对象

getPercentInstance(Locale locale):以参数locale对象所标识的本地信息来获得处理百分比数值的NumberFormat实例对象

小练习:

package com.hbsi.demo;

import java.text.DateFormat;

import java.text.NumberFormat;

import java.text.ParseException;

import java.util.Date;

import java.util.Locale;

/*  1.请创建一个date对象,并把date对象中表示日期部分的时间值,以及表示时间部分的时间值,

 *     分别以short、long模式进行格式化输出(国家设置为中国)。 

2. 请将时间值:09-11-28 上午10时25分39秒 CST,反向解析成一个date对象。 

4.动态文本国际化,课上的实例做一遍。

3. 请将整数198,输出为货币形式:$198,并将$198反向解析成整数198。

 */

public class Demo5 {

public static void main(String[] args) throws ParseException {

Demo5 d=new Demo5();

d.test1();

d.test2();

d.test3();

d.test4();

}

public void test1(){

//.请创建一个date对象,并把date对象中表示日期部分的时间值,以及表示时间部分的时间值,分别以short

Date date=new Date();

DateFormat df=DateFormat.getDateInstance(DateFormat.SHORT,Locale.CHINA);

String result=df.format(date);

System.out.println("short模式:"+result);

//请创建一个date对象,并把date对象中表示日期部分的时间值,以及表示时间部分的时间值,long模式进行格式化输出(国家设置为中国)。

Date date1=new Date();

DateFormat df1=DateFormat.getDateInstance(DateFormat.LONG,Locale.CHINA);

String result1=df1.format(date1);

System.out.println("long模式:"+result1);

}

public void test2(){

//请将时间值:09-11-28 上午10时25分39秒 CST,反向解析成一个date对象。 

String s="09-11-28 上午10时25分39秒 CST";

DateFormat df=DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.CHINA);

try {

Date d=df.parse(s);

System.out.println(d);

} catch (ParseException e) {

e.printStackTrace();

}

}

public void test3(){

// 请将整数198,输出为货币形式:$198,

int price=198;

NumberFormat nf=NumberFormat.getCurrencyInstance(Locale.US);

String str=nf.format(price);

System.out.println(str);

}

public void test4() throws ParseException{

//并将$198反向解析成整数198。

String price="$198";

NumberFormat nf=NumberFormat.getCurrencyInstance(Locale.US);

Number num=nf.parse(price);

System.out.println(num.doubleValue());

}

}

九、 <fmt:formatDate>标签

<fmt:formatDate>标签用于对日期和时间按本地化信息进行格式化,或对日期和时间按化为JSP页面作者自定义的格式进行格式化。语法格式如下:

<fmt:formatDate value="date"

[type="{time|date|both}"] [dateStyle="{default|short|medium|long|full}"] [timeStyle="{default|short|medium|long|full}"] [pattern="customPattern"]

[timeZone="timeZone"]

[var="varName"]

[scope="{page|request|session|application}"] />

十、<fmt:parseDate>标签

<fmt:parseDate>标签与<fmt: formatDate >标签的作用正好相反,它用于将一个表示日期和时间的字符串解析成java.util.Date实例对象.

语法1,没有标签体的情况:

    <fmt:parseDate value="dateString"

[type="time|date|both"]

[dateStyle="default|short|medium|long|full"]

[timeStyle="default|short|medium|long|full"]

[pattern="customPattern"]

[timeZone="timeZone"]

[parseLocale="parseLocale"]

[var="varName"]

[scope="{page|request|session|application}"] /> 

十一、<fmt:formatNumber>标签

<fmt:formatNumber>标签用于将数值、货币或百分数按本地化信息进行格式化,或者按JSP页面作者自定义的格式进行格式化。 

没有标签体的情况:

<fmt:formatNumber value="numericValue"

       [type="{number|currency|percent}"]

       [pattern="customPattern"]

       [currencyCode="currencyCode"]

       [currencySymbol="currencySymbol"]

       [groupingUsed="{true|false}"]

       [maxIntegerDigits="maxIntegerDigits"]

       [minIntegerDigits="minIntegerDigits"]

       [maxFractionDigits="maxFractionDigits"]

       [minFractionDigits="minFractionDigits"]

       [var="varName"]

       [scope="{page|request|session|application}"] />

十二、<fmt:parseNumber>标签

<fmt:parseDate>标签与<fmt: formatNumber >标签的作用正好相反,它用于将一个按本地化方式被格式化后的数值、货币或百分数解析为数值 

语法1,没有标签体的情况:

<fmt:parseNumber value="numericValue"

[type="{number|currency|percent}"]

[pattern="customPattern"]

[parseLocale="parseLocale"]

[integerOnly="{true|false}"]

[var="varName"

[scope="{page|request|session|application}"] />

语法2, 有标签体的情况,在标签体中指定要被解析的日期和/或时间值:

<fmt:parseDate [type="time|date|both"] [dateStyle="default|short|medium|long|full"] [timeStyle="default|short|medium|long|full"]

[pattern="customPattern"]

[timeZone="timeZone"]

[parseLocale="parseLocale"]

[var="varName"] [scope="{page|request|session|application}"]>

date value to be parsed

</fmt:parseDate>

十三、MessageFormat(动态文本)

如果一个字符串中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理。

例如:

At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage

以上字符串中包含了时间、数字、货币等多个与国际化相关的数据,对于这种字符串,可以使用MessageFormat类对其国际化相关的数据进行批量处理。

MessageFormat 类如何进行批量处理呢?

1.MessageFormat类允许开发人员用占位符替换掉字符串中的敏感数据(即国际化相关的数据)。

2.MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。

十四、模式字符串与占位符

At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage

模式字符串:

      At {0} on {1},a destroyed {2} houses and caused {3} of damage.

|-----{占位符}

十五、格式化模式字符串

模式字符串:

On {0}, a hurricance destroyed {1} houses and caused {2} of damage.

MessageFormat类

MessageFormat(String pattern)  

实例化MessageFormat对象,并装载相应的模式字符串。

format(object obj[])

格式化输出模式字符串,参数数组中指定占位符相应的替换对象。

format(new Object[ ]{date, new Integer(99), new Double(1E7) })

String pattern = "On {0}, a hurricance destroyed {1} houses and caused "

+ "{2} of damage.";

MessageFormat msgFmt = new MessageFormat(pattern,Locale.US);

//准备参数数组

String datetime = "Jul 3, 1998 12:30 PM";

Date date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 

DateFormat.SHORT, Locale.US).parse(datetime);

Object [] msgArgs = {date, new Integer(99),new Double(1E7)};

//执行格式化操作

String result = msgFmt.format(msgArgs);

System.out.println(result);

十六、模式字符串与占位符

占位符有三种方式书写方式:

{argumentIndex}: 0-9 之间的数字,表示要格式化对象数据在参数数组中的索引号

{argumentIndex,formatType}: 参数的格式化类型

{argumentIndex,formatType,FormatStyle}: 格式化的样式,它的值必须是与格式化类型相匹配的合法模式、或表示合法模式的字符串。

String pattern = "At {0, time, short} on {0, date}, a destroyed'\n'"

+ "{1} houses and caused {2, number, currency} of damage.";

MessageFormat msgFmt = new MessageFormat(pattern,Locale.US);

String datetimeString = "Jul 3, 1998 12:30 PM";

Date date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,

DateFormat.SHORT,Locale.US).parse(datetimeString);

String event = "a hurricance";

Object []msgArgs = {date, event, new Integer(99), new Double(1E7)};

String result = msgFmt.format(msgArgs);

System.out.println(result);

十七、<fmt:param>标签

<fmt:param>标签用于为格式化文本串中的占位符设置参数值,它只能嵌套在<fmt:message>标签内使用。 

语法1,用value属性指定参数值:

<fmt:param value="messageParameter" />

语法2,在标签体中指定参数的值的情况:

<fmt:param>

body content

</fmt:param>

  相关解决方案