转自https://blog.csdn.net/qq_43560838/article/details/83747604

一:简单理解

    国际化简称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=\u767B\u5F55\u7528\u6237\u540D
login.password=\u767B\u5F55\u5BC6\u7801

 

在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:

   

复制代码
 1  <!-- 局部定义使用哪一种国际化语音 -->
 2    <s:i18n name="messages_zh_CN">  
 3    <form action="<%=basePath%>login.action" method="post">
 4              <table>
 5                <tr>
 6                  <td><s:text name="login.username"/></td>
 7                  <td><input type="text" name="user.userName"/></td>
 8                </tr>
 9                <tr>
10                  <td><s:text name="login.password"/></td>
11                  <td><input type="text" name="user.password"/></td>
12                </tr>
13                <tr>
14                 <td colspan="2"><input type="submit" value="<s:text name="login"/>"/></td>
15               </tr>              
16              </table>                             
17     </form>
18    </s:i18n> 
复制代码

 

 浏览器显示为:

 

复制代码
 1  <!-- 局部定义使用哪一种国际化语音 -->
 2    <s:i18n name="messages_en_US">  
 3    <form action="<%=basePath%>login.action" method="post">
 4              <table>
 5                <tr>
 6                  <td><s:text name="login.username"/></td>
 7                  <td><input type="text" name="user.userName"/></td>
 8                </tr>
 9                <tr>
10                  <td><s:text name="login.password"/></td>
11                  <td><input type="text" name="user.password"/></td>
12                </tr>
13                <tr>
14                 <td colspan="2"><input type="submit" value="<s:text name="login"/>"/></td>
15               </tr>              
16              </table>                             
17     </form> 
18    </s:i18n> 
复制代码

浏览器显示为

posted on 2017-11-12 02:23  Sharpest  阅读(1756)  评论(0编辑  收藏  举报