zoukankan      html  css  js  c++  java
  • 【转】Struts2国际化

    原文章:http://www.cnblogs.com/hellokitty1/p/5083663.html

    简单理解

        国际化简称i18n,其来源是英文单词 internationalization的首末字符i 和n。18为中间的字符数。

        随着全球经济的一体化,软件开发者应该开发出支持多国语言、国际化的Web应用。对于Web应用来说,同样的页面在不同的语言环境下需要显示不同的效果。

        国际化文件的命名规则:

              1、基本名.properties 如:message.properties

              2、基本名_语言编码_国家编码.properties 如:message_zh_CN.properties, message_en_US.properties 其中语言编码和国家编码是固定的,可以在JDK中Locale类的常量中找到。

        Java中已经实现了国际化功能,struts2中只是对该功能进行了整合,以方便我们的使用。

        Struts2中使用到国际化的地方有: 1、jsp页面的国际化; 2、Action信息国际化; 3、转换错误信息的国际化; 4、校验错误信息的国际化;

        Struts2国际化文件分类: 1、全局范围国际化文件 2、包范围国际化文件 3、Action类范围国际化文件。

     

        全局范围国际化文件:

        编写一个messages_zh_CN.properties和messages_en_US.properties放在src下。

    我在这里配置了两个属性:

    messages_en_US:

    login.username=username
    login.password=password

     

    messages_zh_CN:

    login.username=u767Bu5F55u7528u6237u540D
    login.password=u767Bu5F55u5BC6u7801

     

    在struts.xml中通过struts.custom.i18n.resources常量把资源文件定义为全局资源文件:

    eg:      

    <!-- 配置struts2国际化 value是国际化资源基本名message-->
    <constant name="struts.custom.i18n.resources" value="messages_en_US"/>
    <!--或者<constant name="struts.custom.i18n.resources" value="messages_zh_CN"/>-->

    (java代码中)国际化获取配置文件值使用:getText("键")

            eg:getText("login.username")  ---------》username。

                  或者

               getText("login.username") ------------>登录用户名。

    在jsp页面中使用国际化。这里需要使用标签:<s:i18n>标签

    eg:

    <!-- 局部定义使用哪一种国际化语音 -->
       <s:i18n name="messages_zh_CN">  
       <form action="<%=basePath%>login.action" method="post">
                 <table>
                   <tr>
                     <td><s:text name="login.username"/></td>
                     <td><input type="text" name="user.userName"/></td>
                   </tr>
                   <tr>
                     <td><s:text name="login.password"/></td>
                     <td><input type="text" name="user.password"/></td>
                   </tr>
                   <tr>
                    <td colspan="2"><input type="submit" value="<s:text name="login"/>"/></td>
                  </tr>              
                 </table>                             
        </form>
       </s:i18n>

     浏览器显示为:

     

    <!-- 局部定义使用哪一种国际化语音 -->
       <s:i18n name="messages_en_US">  
       <form action="<%=basePath%>login.action" method="post">
                 <table>
                   <tr>
                     <td><s:text name="login.username"/></td>
                     <td><input type="text" name="user.userName"/></td>
                   </tr>
                   <tr>
                     <td><s:text name="login.password"/></td>
                     <td><input type="text" name="user.password"/></td>
                   </tr>
                   <tr>
                    <td colspan="2"><input type="submit" value="<s:text name="login"/>"/></td>
                  </tr>              
                 </table>                             
        </form> 
       </s:i18n>

    浏览器显示为

  • 相关阅读:
    前端开发经验总结
    开发组件的原则
    jQuery deferred对象API详解
    prototype、constructor那点事儿
    text-overflow:ellipsis的那点事儿
    9 个超实用的 jQuery 代码片段
    BFC
    Console命令详解,让调试js代码变得更简单
    前端css规范
    什么是FOUC?如何避免FOUC?
  • 原文地址:https://www.cnblogs.com/fengmingyue/p/6146307.html
Copyright © 2011-2022 走看看