瘋狂

          STANDING ON THE SHOULDERS OF GIANTS
          posts - 481, comments - 486, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

             struts利用在session中放一個(gè)local對(duì)象來達(dá)到設(shè)置當(dāng)前語言您的目的,默認(rèn)的情況下,struts根據(jù)網(wǎng)頁向后臺(tái)提交時(shí)所包含的編碼信息來提供缺省的local對(duì)象。這就是我們?yōu)槭裁纯梢酝ㄟ^更改網(wǎng)頁顯示語言設(shè)置,就能顯示不同語言文字的原因。struts在session中存放的這個(gè)local對(duì)象取名為Globals.LOCAL_KEY的值,因此利用這個(gè)原理我們就可以用編程的方式來手工切換整個(gè)應(yīng)用系統(tǒng)的語言。
          struts國際化問題的一個(gè)小例子:(根據(jù)下拉列表的選擇顯示不同語言的網(wǎng)站)

          1,首先針對(duì)不同的語言,寫不同的MessageResources.properties文件,比如說簡體中文是
          MessageResources_zh_CN.properties,英文就是MessageResources_en_US.properties,然后對(duì)這些配置信
          息文件同樣用native2ascii工具處理一次(打開cmd輸入native2ascii然后把要顯示的中文輸入回車就ok了),把非ASCII碼統(tǒng)統(tǒng)轉(zhuǎn)為Unicode編碼。 放在classes文件夾下你指定的地方,這里是放在/web-inf/classes/com/下,
          我寫的兩個(gè)文件是;1,MessageResources_en_US.properties:內(nèi)容如下:
          version=hello,you have choose the english version
          MessageResources_zh_CN.properties內(nèi)容如下:
          version=\u4f60\u597d,\u4f60\u5df2\u7ecf\u9009\u62e9\u4e86\u4e2d\u6587\u7248\u672c
          (要注意的是:文件名中的zh和en代表語言的不能寫錯(cuò),而CN和US代表國家可以不寫)
          2,在struts-config.xml加入 <message-resources parameter="com.MessageResources" />注意com為包名。
          3,測試頁面:index.jsp
          <form name="form1" method="post" action="chooselocal.do">
            <select name="il8n_info">
                <option value="zh">中文版</option>
              <option value="en">英文版</option>
            </select>
            <input type="submit" value="submit"><p>
            </form>
          4.相應(yīng)的actionform:

          package com.il8n;

          import org.apache.struts.action.ActionForm;

          public class il8nActionForm extends ActionForm {
           private String il8n_info;
           
          public String getIl8n_info() {
           return il8n_info;
          }

          public void setIl8n_info(String il8n_info) {
           this.il8n_info = il8n_info;
          }

          }

          5:相應(yīng)的acrion:

          package com.il8n;

          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 org.apache.struts.action.ActionMessage;
          import org.apache.struts.action.ActionMessages;

          public class il8nAction extends Action {

           @Override
           public ActionForward execute(ActionMapping mapping, ActionForm form,
             HttpServletRequest request, HttpServletResponse response)
             throws Exception {
            il8nActionForm il8naf = (il8nActionForm)form;
            ActionMessages messages = new ActionMessages();
           
            Locale locale = null;
            if("zh".equals(il8naf.getIl8n_info())){
             locale = new Locale("zh","CN");
            }else if("en".equals(il8naf.getIl8n_info())){
             locale = new Locale("en","Us");
            }
            this.setLocale(request, locale);//將local對(duì)象放到session里也可以用下面的方法:
            //request.getSession().setAttribute(Globals.LOCALE_KEY, locale);//英文struts是把local對(duì)象放在了session里面
            return mapping.findForward("success");
           }
           
          }

          6:struts-config.xml中的配置:
          <form-beans>
          <form-bean name="il8nActionForm" type="com.il8n.il8nActionForm"></form-bean>
          </form-beans>
          <action-mappings>
          <action path="/chooselocal"
                  type="com.il8n.il8nAction"
                  name="il8nActionForm"
                  scope="request"
          >
          <forward name="success" path="/index.jsp"></forward>
          </action>
          </action-mappings>
          <message-resources parameter="com.MessageResources" />
          然后在index.jsp中使用<been:message key="version"/>來調(diào)用version的值顯示不同的語言。(當(dāng)然要引入相關(guān)的tag)
          也就是當(dāng)你選擇中文版然后提交就會(huì)顯示
          你好,你選擇了中文版
          當(dāng)你選擇英文版的時(shí)候就會(huì)顯示:
          hello,you have choose the english version

          試一下吧!


          評(píng)論

          # re: struts(il8n)實(shí)現(xiàn)國際化的一個(gè)例子  回復(fù)  更多評(píng)論   

          2007-10-22 23:01 by 求學(xué)者
          \u4f60\u597d,\u4f60\u5df2\u7ecf\u9009\u62e9\u4e86\u4e2d\u6587\u7248\u672c

          問下你這個(gè)如何實(shí)現(xiàn)的。

          我想把 這句話 (這個(gè)指令他從頭到尾只有出現(xiàn)在這一長串里面)

          轉(zhuǎn)為上邊的形式 如何實(shí)現(xiàn)? 謝謝了

          # re: struts(il8n)實(shí)現(xiàn)國際化的一個(gè)例子  回復(fù)  更多評(píng)論   

          2007-10-23 19:08 by freeman1984
          開始->運(yùn)行->cmd->輸入:native2ascii 然后回車。
          輸入你要轉(zhuǎn)的中文回車就可以了;
          比如輸入你那句話:這個(gè)指令他從頭到尾只有出現(xiàn)在這一長串里面。會(huì)顯示:
          \u8fd9\u4e2a\u6307\u4ee4\u4ed6\u4ece\u5934\u5230\u5c3e\u53ea\u6709\u51fa\u73b0\u
          5728\u8fd9\u4e00\u957f\u4e32\u91cc\u9762

          ok了!?。?!

          # re: struts(il8n)實(shí)現(xiàn)國際化的一個(gè)例子  回復(fù)  更多評(píng)論   

          2008-06-16 22:07 by fy_iceworld
          進(jìn)入頁面時(shí)鼠標(biāo)上有一個(gè)動(dòng)畫特效,不便于瀏覽。

          # re: struts(il8n)實(shí)現(xiàn)國際化的一個(gè)例子  回復(fù)  更多評(píng)論   

          2008-08-27 13:50 by chua
          同樣的問題,可不可以把鼠標(biāo)上的動(dòng)畫特效去掉,確實(shí)不便于瀏覽。

          # re: struts(il8n)實(shí)現(xiàn)國際化的一個(gè)例子  回復(fù)  更多評(píng)論   

          2009-05-21 16:47 by yueyue
          非常好!剛好我要用這個(gè),好久沒搞了,看了你的一下子就搞出來了。多謝了!

          # re: struts(il8n)實(shí)現(xiàn)國際化的一個(gè)例子  回復(fù)  更多評(píng)論   

          2014-12-03 16:27 by 花戰(zhàn)魂
          你的配置有穿越了

          # re: struts(il8n)實(shí)現(xiàn)國際化的一個(gè)例子  回復(fù)  更多評(píng)論   

          2016-04-26 22:26 by 正常
          在啥地方

          只有注冊用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 根河市| 神农架林区| 七台河市| 淳安县| 循化| 江华| 綦江县| 玉环县| 永川市| 奉新县| 平定县| 大石桥市| 忻城县| 陆良县| 井陉县| 义乌市| 郧西县| 灵宝市| 荔浦县| 烟台市| 黄浦区| 峨边| 东丽区| 拉萨市| 高要市| 抚远县| 富锦市| 绥江县| 玉林市| 永济市| 运城市| 株洲县| 新河县| 绥江县| 阿荣旗| 石台县| 新化县| 保山市| 金山区| 肇州县| 黎川县|