~~~用properties文件实现国际化与本地化的一个小例子~~~
类别: JAVA教程
JSP源文件如下:
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.awt.*" %>
<%
String languageDefault = Locale.getDefault().getLanguage();
String countryDefault = Locale.getDefault().getCountry();
Locale defaultLocale = null;
String charset = null;
if (languageDefault == "en" || languageDefault.equals("en")){
defaultLocale = Locale.ENGLISH;
charset = "iso-8859-1";
}else if (countryDefault == "CN" || countryDefault.equals("CN")){
defaultLocale = Locale.SIMPLIFIED_CHINESE;
charset = "gb2312";
}else if (countryDefault == "TW" || countryDefault.equals("TW")){
defaultLocale = Locale.TRADITIONAL_CHINESE;
charset = "big5";
}else{
System.out.println("LanguageDefault not equals en.CountryDefault not equals TW or CN");
}
if (session.isNew()){
session.setAttribute("locale",defaultLocale);
session.setAttribute("charset",charset);
}else{
String language = request.getParameter("language");
if (language == null || language.equals(null)){
session.setAttribute("locale",defaultLocale);
session.setAttribute("charset",charset);
}else{
Locale locale = null;
if (language == "Locale.SIMPLIFIED_CHINESE" || language.equals( "Locale.SIMPLIFIED_CHINESE")){
locale = Locale.SIMPLIFIED_CHINESE;
charset = "gb2312";
}else if (language == "Locale.TRADITIONAL_CHINESE" || language.equals( "Locale.TRADITIONAL_CHINESE")){
locale = Locale.TRADITIONAL_CHINESE;
charset = "big5";
}else if (language == "Locale.ENGLISH" || language.equals( "Locale.ENGLISH")){
locale = Locale.ENGLISH;
charset = "iso-8859-1";
}
session.setAttribute("locale",locale);
session.setAttribute("charset",charset);
}
}
Locale locale = (Locale)session.getAttribute("locale");
ResourceBundle messagesBundle = ResourceBundle.getBundle("FullTextSearch.MessagesBundle",locale);
String localeCountry = locale.getCountry();
String localeLanguage = locale.getLanguage();
if(localeLanguage == "en"){
response.setContentType("text/html; charset=ISO8859-1 ");
}else if (localeCountry == "CN"){
response.setContentType("text/html; charset=gb2312");
}else if(localeCountry == "TW"){
response.setContentType("text/html; charset=big5");
}
%>
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>top</title>
<base target="contents">
</head>
<body topmargin="0" leftmargin="0" background="../images/leftbg.gif">
<table border="0" width="796" style="font-size: 10pt" cellspacing="0" cellpadding="0" height="60">
<tr>
<td width="196" height="68"><img border="0" src="../images/cmlogo.gif" width="196" height="68"></td>
<td width="470" height="64">
<p align="center"><a href="#" onclick="href=\'testmulti.jsp?language=Locale.SIMPLIFIED_CHINESE\'"><%=messagesBundle.getString("simplifiedChinese")%></a> | <a href="#" onclick="href=\'testmulti.jsp?language=Locale.TRADITIONAL_CHINESE\'"><%=messagesBundle.getString("traditionalChinese")%></a> | <a href="#" onclick="href=\'testmulti.jsp?language=Locale.ENGLISH\'"><%=messagesBundle.getString("english")%></a></td>
<td width="130" height="56">
<p align="center">
<%=messagesBundle.getString("username")%>
</td>
</tr>
</table>
</body>
</html>
属性文件如下(英文,简体中文,繁体中文,默认)(放在包FullTextSearch下面)
MessagesBundle_en.properties
username=administrator
simplifiedChinese=SimplifiedChinese
traditionalChinese=TraditionalChinese
english=English
MessagesBundle_zh_CN.properties
username=/u7cfb/u7edf/u7ba1/u7406/u5458
simplifiedChinese=/u7b80/u4f53/u4e2d/u6587
traditionalChinese=/u7e41/u4f53/u4e2d/u6587
english=/u82f1/u6587
MessagesBundle_zh_TW.properties
username=/u7cfb/u7d71/u7ba1/u7406/u54e1
simplifiedChinese=/u7c21/u9ad4/u4e2d/u6587
traditionalChinese=/u7e41/u9ad4/u4e2d/u6587
english=/u82f1/u6587
MessagesBundle.properties
username=administrator
simplifiedChinese=SimplifiedChinese
traditionalChinese=TraditionalChinese
english=English
备注:
汉字转换为ascii码用命令:native2ascii.exe
本程序可以正常运行~~~
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.awt.*" %>
<%
String languageDefault = Locale.getDefault().getLanguage();
String countryDefault = Locale.getDefault().getCountry();
Locale defaultLocale = null;
String charset = null;
if (languageDefault == "en" || languageDefault.equals("en")){
defaultLocale = Locale.ENGLISH;
charset = "iso-8859-1";
}else if (countryDefault == "CN" || countryDefault.equals("CN")){
defaultLocale = Locale.SIMPLIFIED_CHINESE;
charset = "gb2312";
}else if (countryDefault == "TW" || countryDefault.equals("TW")){
defaultLocale = Locale.TRADITIONAL_CHINESE;
charset = "big5";
}else{
System.out.println("LanguageDefault not equals en.CountryDefault not equals TW or CN");
}
if (session.isNew()){
session.setAttribute("locale",defaultLocale);
session.setAttribute("charset",charset);
}else{
String language = request.getParameter("language");
if (language == null || language.equals(null)){
session.setAttribute("locale",defaultLocale);
session.setAttribute("charset",charset);
}else{
Locale locale = null;
if (language == "Locale.SIMPLIFIED_CHINESE" || language.equals( "Locale.SIMPLIFIED_CHINESE")){
locale = Locale.SIMPLIFIED_CHINESE;
charset = "gb2312";
}else if (language == "Locale.TRADITIONAL_CHINESE" || language.equals( "Locale.TRADITIONAL_CHINESE")){
locale = Locale.TRADITIONAL_CHINESE;
charset = "big5";
}else if (language == "Locale.ENGLISH" || language.equals( "Locale.ENGLISH")){
locale = Locale.ENGLISH;
charset = "iso-8859-1";
}
session.setAttribute("locale",locale);
session.setAttribute("charset",charset);
}
}
Locale locale = (Locale)session.getAttribute("locale");
ResourceBundle messagesBundle = ResourceBundle.getBundle("FullTextSearch.MessagesBundle",locale);
String localeCountry = locale.getCountry();
String localeLanguage = locale.getLanguage();
if(localeLanguage == "en"){
response.setContentType("text/html; charset=ISO8859-1 ");
}else if (localeCountry == "CN"){
response.setContentType("text/html; charset=gb2312");
}else if(localeCountry == "TW"){
response.setContentType("text/html; charset=big5");
}
%>
<html>
<head>
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>top</title>
<base target="contents">
</head>
<body topmargin="0" leftmargin="0" background="../images/leftbg.gif">
<table border="0" width="796" style="font-size: 10pt" cellspacing="0" cellpadding="0" height="60">
<tr>
<td width="196" height="68"><img border="0" src="../images/cmlogo.gif" width="196" height="68"></td>
<td width="470" height="64">
<p align="center"><a href="#" onclick="href=\'testmulti.jsp?language=Locale.SIMPLIFIED_CHINESE\'"><%=messagesBundle.getString("simplifiedChinese")%></a> | <a href="#" onclick="href=\'testmulti.jsp?language=Locale.TRADITIONAL_CHINESE\'"><%=messagesBundle.getString("traditionalChinese")%></a> | <a href="#" onclick="href=\'testmulti.jsp?language=Locale.ENGLISH\'"><%=messagesBundle.getString("english")%></a></td>
<td width="130" height="56">
<p align="center">
<%=messagesBundle.getString("username")%>
</td>
</tr>
</table>
</body>
</html>
属性文件如下(英文,简体中文,繁体中文,默认)(放在包FullTextSearch下面)
MessagesBundle_en.properties
username=administrator
simplifiedChinese=SimplifiedChinese
traditionalChinese=TraditionalChinese
english=English
MessagesBundle_zh_CN.properties
username=/u7cfb/u7edf/u7ba1/u7406/u5458
simplifiedChinese=/u7b80/u4f53/u4e2d/u6587
traditionalChinese=/u7e41/u4f53/u4e2d/u6587
english=/u82f1/u6587
MessagesBundle_zh_TW.properties
username=/u7cfb/u7d71/u7ba1/u7406/u54e1
simplifiedChinese=/u7c21/u9ad4/u4e2d/u6587
traditionalChinese=/u7e41/u9ad4/u4e2d/u6587
english=/u82f1/u6587
MessagesBundle.properties
username=administrator
simplifiedChinese=SimplifiedChinese
traditionalChinese=TraditionalChinese
english=English
备注:
汉字转换为ascii码用命令:native2ascii.exe
本程序可以正常运行~~~
-= 资 源 教 程 =-
文 章 搜 索