java:jsp: ResourceBundle国际化多语言

Posted 穆晟铭

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java:jsp: ResourceBundle国际化多语言相关的知识,希望对你有一定的参考价值。

java提供了一个资源类java.util.ResourceBundle来试下多国语言版本。其实ResourceBundle只是一个抽象的类,她有两个子类:ListResourceBundle,和,PropertyResourceBundle.前一个子类需要编写不同的国家语言信息放置在对象类中,下面将实例演示,后一个子类是需要编写不同国家语言的文件后缀为.properties()来存放语言信息,这部分内容读者可以在国际化标签以及Struts时了解到。例如,存放在RES_zh_EN.propertieh和存放英文的RES_en_US.properties资源文件.

 1 package cn.com.resouce;
 2 import java.util.ListResourceBundle;
 3 
 4 public class RES_zh_CN extends ListResourceBundle {
 5 
 6     static final Object[][] contents = new String[][]{
 7         {"version","这是中文版"},
 8         {"title","中文版本"},
 9         {"index","首页"},
10         {"news","新闻"},
11         {"life","生活"},
12         {"sports","体育"},
13         {"entertainment","娱乐"}
14     };
15     
16     
17     @Override
18     protected Object[][] getContents() {
19         // TODO Auto-generated method stub
20         return contents;
21     }
22 
23 }

 

 1 package cn.com.resouce;
 2 import java.util.ListResourceBundle;
 3 
 4 public class RES_en_US extends ListResourceBundle {
 5     
 6     //创建对应的中文文本信息
 7     
 8     static final Object[][] contents = new String[][]{
 9         
10         {"version","this is english"},
11         {"title","english"},
12         {"index","index"},
13         {"news","news"},
14         {"life","life"},
15         {"sports","sports"},
16         {"entertainment","entertainment"}
17     };
18 
19 
20     @Override
21     protected Object[][] getContents() {
22         // TODO Auto-generated method stub
23         return contents;
24     }
25 
26 }

 

select.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf8"%>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 3 <html>
 4 <head>
 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 6 <title>语言选择</title>
 7 </head>
 8 <body>
 9 
10 
11 <form action="multiLanguage.jsp" method="get">
12 <select name="language">
13 <option name="default">默认</option>
14 <option value="chinese">中文</option>
15 <option value="english">English</option>
16 </select>
17 <input type="submit" value="提交">
18 </form>
19 
20 </body>
21 </html>

 

language.jsp

<%@ page language="java" contentType="text/html; charset=utf8"%>
<%@ page import="java.util.ResourceBundle,java.util.Locale" %>
<%
String language = request.getParameter("language");
//默认语言,浏览器会根据请求中所包含的Accept language来辨别默认的何种语言
ResourceBundle res = ResourceBundle.getBundle("cn.com.resouce.RES");
//中文版本的显示
if(language.equals("chinese"))
{
    res = ResourceBundle.getBundle("cn.com.resouce.RES", new Locale("zh","CN"));    
}
if(language.equals("english"))
{
    res = ResourceBundle.getBundle("cn.com.resouce.RES", new Locale("en", "US"));    
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><%=res.getString("title") %></title>
</head>
<body>

<%=res.getString("version") %>
<hr>
<%=res.getString("index") %> | <%=res.getString("news") %> |<%=res.getString("life") %>
| <%=res.getString("sports") %> | <%=res.getString("entertainment") %>

</body>
</html>

 

以上是关于java:jsp: ResourceBundle国际化多语言的主要内容,如果未能解决你的问题,请参考以下文章

java:ResourceBundle rb=ResourceBundle.getBundle("LocalString");

ResourceBundle读取配置文件

ResourceBundle读取properties配置文件

Java获取ResourceBundle的顺序

ResourceBundle类:读取配置文件

ResourceBundle类读取properties文件