JSTL – I18N - 国际化标签库。

来源:互联网 发布:外汇牌价走势图软件 编辑:程序博客网 时间:2024/05/16 19:06
I18N是Internationalization的简称,因为I到N之间有18个字符所以叫i18n。

在java的核心库当中,提供了对i18n的支持,java.util.Locale类是最重要的i18n相关类。

Locale类:

Locale ch = new Locale(“zh”,”CN”);
Locale ch = Locale.CHINA;
或获取默认值:
Locale zh = Locale.getDefault();
然后可以通过以下方法输出语言和国家:
getLanguage
getCountry

I18N-ResourceBundle类:

Java.util.ResourceBundle类,用于管理和Locale相关的资源的功能。
ResourceBundle类提供了两个方法,用于创建ResourceBundle对像的静态工厂方法:
getBundle(String baseName)—通过此方法获取资源文件的名称
getBundle(String baseName,Locale locale);
参数中的baseName是资源文件的名称,资源文件通常以properties为扩展名。
资源文件的命名规则如下:
默认资源文件:resources.properties
英文资源文件:resources_en_US.properties
中文资源文件:resources_zh_CN.properties


msg.properties

welcome=welcome

msg_en_US.properties

welcome=us_welcome
msg_zh_CN.properties

welcome=\u6B22\u8FCE\u60A8

public static void main(String[] args) {//properties文件名格式:msg_en_US.properties、msg_zh_CN.properties;//若文件名格式错误,使用该语言,会使用msg.properties中的语言ResourceBundle rb=ResourceBundle.getBundle("msg");//获取当前系统默认的国际化语言ResourceBundle rb2=ResourceBundle.getBundle("msg",Locale.US);//获取指定的国际化语言ResourceBundle rb3=ResourceBundle.getBundle("msg",Locale.CANADA_FRENCH);//如果不存在,则使用系统默认的编码String str=rb.getString("welcome");System.out.println(str);String str2=rb2.getString("welcome");System.out.println(str2);String str3=rb3.getString("welcome");System.out.println(str3);}
结果:

欢迎您us_welcome欢迎您

JSTL的国际化标签:

<fmt:setLocale/> - 设置Locale关将它保存在某个范围内,默认为page范围。
如:<fmt:setLocale value=“zh_CN” scope=“session”/>它的作用相当于:
Locale locale = new Locale(“zh”,”CN”);
Session.setAttribute(“javax.servlet.jsp.jstl.fmt.locale.session”,locale);
<fmt:setBundle/>
用于设置要使用的资源文件。如:
<fmt:setBundle basename=“message” var=“变量名” scope=“session”/>
<fmt:bundle/>
用于设置主体使用的资源。即setBundle设置的资源。
<fmt:message/>
读取资源文件中key所对应的值。
<fmt:param/>
嵌套在message中,用户动态的传递参数。
<fmt:requestEncoding/>
用于设置请求的编码字符。<fmt:requestEncoding value=“GBK”/>等同于
Request.setCharacterEncoding(“GBK”);
设置支持国际化的网页:

第一步:建立两个资源文件为:
message.properties
message_zh_CN.properties
第二步:使用jstl标签。
<fmt:setLocale value=“${param.locale}” scope=“session”/>
<fmt:setBundle basename=“message” scope=“session”/>
第三步:切换。
<a href="index.jsp?locale=zh_CN">中文</a><br/>

 <a href="index.jsp?locale=en_US">English</a>


msg_en_US.properties

welcome=us_welcome
msg_zh_CN.properties

welcome=\u6B22\u8FCE\u60A8
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>  </head>  <body>    <h2>国际化标签库演示</h2>        <a href="?bd=zh_CN">中文</a>  <a href="?bd=en_US">English</a>    <hr/>        <!-- 1.设置本地信息(java.util.Local,那个国家的语言) -->    <fmt:setLocale value="${param.bd}"/>        <!-- 2.指定资源文件1(java.util.ResourceBundle) -->    <fmt:setBundle basename="msg" scope="session"/>    <!-- 指定资源文件2 -->    <fmt:setBundle basename="cn.resources.a" var="two" scope="session"/>        <!-- 3.读取国际化资源文件中的信息,进行显示-->    <fmt:message key="welcome"></fmt:message><br/>        <!-- 显示第二个资源文件中的信息 -->    <fmt:message key="addr" bundle="${two}"></fmt:message><br/>        <a href='<c:url value="/jsps/i18n/4.jsp"></c:url>'>跳到下一个页面</a>  </body></html>
4.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>  </head>    <body>    <h2>继续使用前一个页面的国际化信息</h2>        <!--    3.读取国际化资源文件中的信息,进行显示    注意要采用别名,否则不行--因为不指定别名就会把前面的设置覆盖,导致前面的设置无效    -->    <fmt:message key="welcome"></fmt:message>        <!-- 显示第二个资源文件中的信息 -->    <fmt:message key="addr" bundle="${two}"></fmt:message>  </body></html>


原创粉丝点击