从制造到创造
软件工程师成长之路
posts - 249,  comments - 96,  trackbacks - 0
效果:


文件清单:
序号 文件名
1 ChangeLocaleAction.java
2 ChangeLocaleForm.java
3 struts-config.xml
4 index.jsp
5 application_en_US.properties
6 application_zh_CN.properties
7 application_zh_TW.properties

清单1:
package com.coderdream.struts.action;

import java.util.Locale;

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

import org.apache.struts.Globals;
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 com.coderdream.struts.form.ChangeLocaleForm;

public class ChangeLocaleAction extends Action {

    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        ChangeLocaleForm clForm 
= (ChangeLocaleForm)form;
        String language 
= clForm.getLanguage();
        
if (language != null) {
            Locale locale;
            
if (language.equalsIgnoreCase("en")) {
                locale 
= Locale.US;
                request.getSession().setAttribute(
                        Globals.LOCALE_KEY, locale);
            } 
else if (language.equalsIgnoreCase("tw")) {
                locale 
= Locale.TAIWAN;
                request.getSession().setAttribute(
                        Globals.LOCALE_KEY, locale);
            } 
else {
                locale 
= Locale.CHINA;
                request.getSession().setAttribute(
                        Globals.LOCALE_KEY, locale);
            }
        }
        
        
return mapping.findForward("success");
    }
         
}

清单2:
package com.coderdream.struts.form;

import org.apache.struts.action.ActionForm;


/**
 * 
 * description:
 * 
 * 
@author 
 *
 
*/
public class ChangeLocaleForm extends ActionForm{
    
    
public ChangeLocaleForm() {
        
super();
    }

    
private String language;

    
public String getLanguage() {
        
return language;
    }

    
public void setLanguage(String language) {
        
this.language = language;
    }
}

清单3:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
    
<data-sources />
    
<form-beans>
        
<form-bean name="ChangeLocaleForm"
            type
="com.coderdream.struts.form.ChangeLocaleForm" />
    
</form-beans>

    
<action-mappings>

        
<action name="ChangeLocaleForm" path="/ChangeLocale"
            scope
="request"
            type
="com.coderdream.struts.action.ChangeLocaleAction"
            validate
="false">
            
<forward name="success" path="/index.jsp" />
        
</action>
    
</action-mappings>

    
<message-resources key="application"
        parameter
="com.coderdream.struts.resources.application" />
</struts-config>

清单4:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="org.apache.struts.Globals"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>

<html:html>
<head>
    
<title>多语言测试</title>
    
<meta http-equiv="pragma" content="no-cache">
    
<meta http-equiv="cache-control" content="no-cache">
    
<meta http-equiv="expires" content="0">
    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    
<meta http-equiv="description" content="This is my page">
</head>
<script language="javascript"> 
    function onLanguage(){
        var language 
= document.getElementsByName("language")[0].value;
        
if(language!=null&&language!=""){         
          document.forms[
0].submit();
            }
          }   
    
</script>

<body>
    
<html:form action="ChangeLocale.do">
        
<html:select property="language" styleId="height:18"
            onchange
="onLanguage();">
            
<option value="" title="">
                
--
                
<bean:message bundle="application" key="global.select.language" />
                
--
            
</option>
            
<option value="en" title="English">
                English
            
</option>
            
<option value="cn" title="简体中文">
                简体中文
            
</option>
            
<option value="tw" title="繁體中文">
                繁體中文
            
</option>
        
</html:select>
    
</html:form>
    
<br>
    
<br>
    
<br>
    
<bean:message bundle="application" key="global.select.test" />
    
<%=request.getSession().getAttribute(Globals.LOCALE_KEY)%>

</body>
</html:html>

清单5:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)

global.select.language 
= Language
global.select.test     
= Test

清单6:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)
#BizException

global.select.language = \u8BED\u8A00
global.select.test     = \u6D4B\u8BD5

清单7:
#Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)

global.select.language = \u8A9E\u8A00
global.select.test     = \u6E2C\u8A66

此功能的关键是在Action里面将页面传过来的language信息得到,然后根据信息设置Locale,然后将新的Locale放到Session中。

源代码:点击下载
posted on 2008-05-14 18:20 CoderDream 阅读(905) 评论(0)  编辑  收藏 所属分类: 经验点滴

只有注册用户登录后才能发表评论。


网站导航:
 

<2008年5月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

常用链接

留言簿(9)

我参与的团队

随笔分类(245)

随笔档案(239)

文章分类(3)

文章档案(3)

收藏夹(576)

友情链接

搜索

  •  

积分与排名

  • 积分 - 429005
  • 排名 - 118

最新评论

阅读排行榜

评论排行榜