在Struts框架中进行应用程序的 国际化 (进行中英文转化)
两种语言的属性文件:ApplicationResources_en.propertiesApplicationResources_zh.properties都建好了,内容没有问题,放在SRC文件夹下。
我想通过一个按钮选择语言进入对应的中英文页面。
参考了部分代码如下:
<html:form action="/changeLanguage.do">
<html:select property="language">
  <html:option value="en">English</html:option>
  <html:option value="zh">Chinese</html:option>
</html:select>
<html:submit>Change</html:submit>
</html:form>public class LanguageForm extends ActionForm {
  private String language; // getter & setter
}
然后对应的Action里面:
Locale l = new Locale(languageForm.getLanguage());
setLocale(request, l);我把俩个属性文件都配置在struts-config.xml文件里了。但怎么也出不来效果啊。 
请问如何解决啊?
代码越详细越好。
请大师们不吝赐教啊!在此先谢了啊。

解决方案 »

  1.   

    看看我的代码
    采用struts2实现的。当我们选择下来列表上时,自动中英文转换
    helloi18n.jsp
    <%@ page language="java" contentType="text/html; charset=gb2312"
        pageEncoding="gb2312"%>
        <%@taglib prefix="s" uri="/struts-tags" %>
    <!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=gb2312">
    <title>Insert title here</title>
    </head>
    <body>
    <s:include value="/chooseLanguage.jsp"/>
    <s:text name="HelloWorld"></s:text>
    <s:property value="%{getText('HelloWorld')}"/></body>
    </html>
    chooseLanguage.jsp<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
    <%@taglib prefix="s" uri="/struts-tags"%>
    <!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=gb2312">
    <title>根据语言显示内容</title>
    <script type="text/javascript">
    function langSelecter_onChanged() {
            document.langForm.submit();
        }

    </script>
    </head>
    <body>
    <s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']" />
    <s:bean id="locales" name="tutorial.Locales" />
    <form action="<s:url includeParams="get" encode="true"/>"
    name="langForm"
    style="background-color: powderblue; padding-top: 4px; padding-bottom: 4px;">
    Language:
    <s:select label="Language" list="#locales.locales" listKey="value"
    listValue="key"
    value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"
    name="request_locale" id="langSelecter"
    onchange="langSelecter_onChanged()" theme="simple" />
    </form>
    </body>
    </html>
    package tutorial;import java.util.Hashtable;
    import java.util.Locale;
    import java.util.Map;public class Locales { public Map<String,Locale> getLocales(){
    Map<String,Locale> locales=new Hashtable<String,Locale>(2);
    locales.put("American English", Locale.US);
    locales.put("Simplified Chinese",Locale.CHINA);
    return locales;
    }
    }<action name="HelloI18n">
    <result>/helloi18n.jsp</result>
    </action>
    中英文资源文件globalMessages_en_US.propertiesHelloWorld=Hello World!globalMessages_zh_CN.propertiesHelloWorld=你好,世界!最后直接访问
    http://localhost:8080/项目名称/HelloI18n.action
    就行啦。
      

  2.   

    如果是用struts2的话,很简单:request_locale作为参数传递(别拼错了),en_US是英文,zh_CN是中文。比如我想显示英文版,我就让链接连着:lang.action?request_locale=en_US.其中lang这个Action什么都不用做。在jsp页面这样写:<s:property value="getText('username')"/>就能获取相应的request_locale对应的语言。相应的struts.xml里面和properties文件自己查文档吧
      

  3.   

    谢谢各位啊,struts 2没有接触过啊,struts 1.2怎么实现啊?
      

  4.   


    我有一个不是提交到action中的例子,楼主看一下吧:
    <%@ page language="java" pageEncoding="UTF-8"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><html>
    <head>
      <title>struts的国际化实例</title>
    </head><body>
    <center>
    <c:if test="${not empty param.lan}">
     <c:if test="${param.lan=='zh'}">
     <fmt:setLocale value="zh_CN"/>
     </c:if>
     <c:if test="${param.lan=='en'}">
     <fmt:setLocale value="en_US"/>
     </c:if>
    </c:if><fmt:setBundle basename="com.yourcompany.struts.ApplicationResources"/>
    <fmt:message>lab</fmt:message>
    <hr>
    <form name="myform" action="" method="get">
    <input type="radio" name="lan" value="zh" >中文
    <input type="radio" name="lan" value="en" >英文<br>
    <hr>
    <input type="submit" value="submit">
    </form></center>
    </body>
    </html>其中这两句改一下
    <fmt:setBundle basename="com.yourcompany.struts.ApplicationResources"/>
    <fmt:message>lab</fmt:message>
      

  5.   

    <form name="myform" action="" method="get">
    action为空,是不是提交过又返回原页面啊?<fmt:setBundle basename="com.yourcompany.struts.ApplicationResources"/>
    <fmt:message>lab</fmt:message>
    资源文件有俩个,basename后面怎么写啊?
      

  6.   

    两个资源文件名:
    ApplicationResources_en_US.properties  ApplicationResources_zh_CN.properties放在包com.yourcompany.struts下
      

  7.   

    http://topic.csdn.net/u/20100316/18/e5a61c83-4801-469d-a365-d8862ee5c48f.html?seed=1478034688&r=63964429#r_63964429这是我一前发布的.没有使用框架实现的国际化.不知道对你有没有帮助你可以使用struts的框架.
      

  8.   

    struts.apache.org
    上有现成的例子,参考代码:
    <s:url action="registerInput" var="registerInputLink" />
    <p><a href="${registerInputLink}">Please register</a> for our prize drawing.</p>
    <h3>Registro español</h3>
    <s:url action="registerInput" var="registerInputLinkES">
        <s:param name="request_locale">es</s:param>
    </s:url>
    <p><a href="${registerInputLinkES}">Por favor, regístrese</a> para nuestro sorteo</p>具体地址:
    http://struts.apache.org/2.1.8.1/docs/message-resource-files.html
      

  9.   

    是不是应该改变session里面的那个值啊,
    类似这样
    session.setAttribute("org.apache.struts.action.LOCALE", new Locale("en"));具体忘了,lz试试吧good luck