Struts实现多国语言切换

摘要:
通过下拉框里各个语言(中文,日本语,English)的选择,切换jsp页面文字。
tyrone1979 发表于 2005-08-26 13:27:19
作者:tyrone1979     来源:blog.csdn.net/tyrone1979
1 准备资源文件。

资源文件命名格式:filename_language_country.properties.
中文文件名为index_zh_CN.properties。
日文文件名为 index_ja_JP.properties。
英文文件名为 index_en.properties。

英文文件内容:
index.jsp.welcome=Colimas Library Management System
index.jsp.name=Name
index.jsp.userid=User ID
index.jsp.pass=Password


中文文件内容:
index.jsp.welcome=/u4f60/u597d
index.jsp.name=/u59d3/u540d
index.jsp.userid=/u7528/u6237/u540d
index.jsp.pass=/u5bc6/u7801


日文文件内容:
index.jsp.welcome=/u3044/u3089/u3063/u3057/u3083/u3044/u307e/u305b
index.jsp.name=/u59d3/u540d
index.jsp.userid=/u30e6/u30fc/u30b6/u30fcID
index.jsp.pass=/u30d1/u30b9/u30ef/u30fc/u30c9

/uxxxx是中文被转换后的ASCII码。可以使用native2ascii.exe工具转换。

2 struts-config.xml里配置资源文件
    
<message-resources parameter="resources.config.index" />

resources.config.index是classes目录下的resources/config子目录的index__xx_xx.properties文件.
struts根据你的浏览器的语言设置调用不同语言的资源文件。
例如:如果你的IE默认语言为中文则。Struts将使用index_zh_CN.properties。而在struts-config.xml里只需写出“index”即可

ActionMapping
  
<form-beans>
                <!--1 Multi-Lanuage support formbean-->
        <form-bean
            name="SelectLanguageForm"
            type="com.nova.colimas.web.form.SelectLanguageForm"/>
   </form-beans>
<!-- =========================================== Global Forward Definitions -->

    <global-forwards>
        <!-- Default forward to "Welcome" action -->
        <!-- Demonstrates using index.jsp to forward -->
        <forward
            name="index"
            path="/pages/index.jsp"/>  
    </global-forwards>


<!-- =========================================== Action Mapping Definitions -->

    <action-mappings>      
        <!-- 1 select language action -->          
            <action    path="/SelectLanguageAction"
              type="com.nova.colimas.web.action.SelectLanguageAction"
              name="SelectLanguageForm"
              scope="request">
            </action>
                …
    </action-mappings>
      


3 jsp
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/tags/struts-bean" prefix="bean"%>
<%@ taglib uri="/tags/struts-html" prefix="html"%>
<%@ taglib uri="/tags/struts-logic" prefix="logic"%>

<html:html>
<Title><bean:message key="index.jsp.welcome"/></Title>
<body>
<logic:present name="user">
        <H3>Welcome <bean:write name="LoginForm" property="userID" />!</H3>
</logic:present>
<logic:notPresent scope="session" name="user">
        <H3>Welcome Colimas!</H3>
</logic:notPresent>
<html:errors />
<html:form action="/SelectLanguageAction.do">
       <html:select property="language">
                <html:option value="0">中文</html:option>
                <html:option value="1">日本語</html:option>
                <html:option value="2">English</html:option>              
       </html:select>
        <html:submit>Select</html:submit>
</html:form>


<html:form action="/LoginAction.do">
        <p><bean:message key="index.jsp.userid"/><input type="text" name="userID" value="tyrone1979" /><br>
        <bean:message key="index.jsp.pass"/><input type="password" name="password" value="197913"/><br>
        <html:submit><bean:message key="index.jsp.login"/></html:submit>
        </p>
</html:form>

</body>
</html:html>


<bean:message key="index.jsp.welcome"/>引用资源文件的index.jsp.welcome属性
SelectLanguageAction.do调用Action实现语言转换。

4 Action
package com.nova.colimas.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
//import org.apache.struts.upload.FormFile;
import com.nova.colimas.web.form.SelectLanguageForm;
import org.apache.struts.Globals;
import java.util.Locale;

public class SelectLanguageAction extends Action {
        public ActionForward execute(ActionMapping mapping,
                         ActionForm form,
                         HttpServletRequest request,
                         HttpServletResponse response)
        throws Exception{
                SelectLanguageForm myform=(SelectLanguageForm)form;
                String lan=myform.getLanguage();
                switch((new Integer(lan)).intValue()){
                case 0 :
                        request.getSession().setAttribute(Globals.LOCALE_KEY,Locale.CHINA);
                        break;
                case 1:
                        request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.JAPAN);
                        break;
                case 2:
                        request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ENGLISH);
                        break;
                default:
                        request.getSession().setAttribute(Globals.LOCALE_KEY, Locale.ENGLISH);
                        break;
                }
                        return mapping.findForward("index");
        }
}
Form
/*
* Created on 2005/06/18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.nova.colimas.web.form;

import org.apache.struts.action.ActionForm;

/**
* @author tyrone
**/
public class SelectLanguageForm extends ActionForm {

        private String language;

        public void Reset() {
                this.language="";
                return;
        }

        /**
         * @return Returns the Language.
         */
        public String getLanguage() {
                return language;
        }
        /**
         * @param language The Language to set.
         */
        public void setLanguage(String property1) {
                this.language = property1;
        }
}


结果
1 IE默认语言为中文:
启动画面
image

切换为日语
image

切换为英文
image



本页页面地址:
<script type="text/javascript"> var tempval = document.getElementById("contentLink"); tempval.value = window.location.href; function copyLink() { var tempval = document.getElementById("contentLink"); tempval.value = window.location.href; tempval.select(); var therange = tempval.createTextRange(); therange.execCommand("Copy"); } function copyLinkTitle(title) { var tempval = document.getElementById("contentLink"); tempval.value = window.location.href; tempval.value = " " + title + ""; tempval.select(); var therange = tempval.createTextRange(); therange.execCommand("Copy"); } </script>
<script src="/dwr/interface/ContentFacade.js" type="text/javascript"></script> <script src="/dwr/engine.js" type="text/javascript"></script> <script src="/dwr/util.js" type="text/javascript"></script>
投票评分(记入本贴作者的专家分)
     非常好 还行 一般 扔鸡蛋          投票总得分: / 投票总人次: <script type="text/javascript"> DWRUtil.setValue("voteCount", 5); DWRUtil.setValue("voteNum", 1); </script>
<script type="text/javascript"> function articleVote(voteSelect) { var vote = new Object(); vote.author = "86376"; vote.oid = "43663"; vote.voteSelect = voteSelect; ContentFacade.articleVote(vote, viewVote); } function viewVote(ajaxResult) { if (ajaxResult.messages != null) { var color; if (ajaxResult.message == false) { color = "red"; } DWRUtil.setValue("voteInfo", " " + ajaxResult.messages + ""); } if (null == ajaxResult.bean) { location.hash="avote"; return; } var voteCount = parseInt(DWRUtil.getValue("voteCount")); var voteNum = parseInt(DWRUtil.getValue("voteNum")); voteCount = voteCount + ajaxResult.bean.votePoint; voteNum = voteNum + 1; DWRUtil.setValue("voteCount", voteCount); DWRUtil.setValue("voteNum", voteNum); location.hash="avote"; } function initArticleVote() { DWRUtil.useLoadingMessage(); } callOnLoad(initArticleVote); </script> <script src="/j/base64.js" type="text/javascript"></script> <script src="/j/cookie.js" type="text/javascript"></script> <script src="/dwr/interface/ContentFacade.js" type="text/javascript"></script> <script src="/dwr/engine.js" type="text/javascript"></script> <script src="/dwr/util.js" type="text/javascript"></script>
用户评论列表
#1 评论作者: rivalhw 发表时间: 2005-12-24 10:31 上午

这种方式可以根据客户端情况自动将语言转换为客户浏览器语言版本,但如果客户要根据自己事情需求将语言转换成自己所需语言版本就显得不够灵活了

建议
自己定义Resource文件,根据客户指定的语言来选择不同的资源文件,并显示出来

ResourceBundle bundle = (ResourceBundle)pageContext.getAttribute(name);    

#0000 author: 000 submitTime: 2006-00-00 12:59
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值