JSP空间欢迎您!专业JSP虚拟主机提供商!!!
返回首页
当前位置: 主页 > JSP教程 >

~~~用properties文件实现国际化与本地化的一个

时间:2011-01-30 12:05来源:佚名 编辑:www.jspzg.com 点击: 次   字体大小:[ ]

当前阅读得是【JSP教程】之~~~用properties文件实现国际化与本地化的一个!
【关键字信息】:~~,~~,~用,用p,pr,ro
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
本程序可以正常运行~~~
标签:
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
【郑重声明】JSP中国刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何投资或其他建议。本网站有部分文章是由网友自由上传。对于此类文章本站仅提供交流平台,不为其版权负责。如果您发现本网站上有侵犯您的知识产权的文章,请联系我们,我们将及时删除,谢谢!
推荐内容