---
title: "struts2国际化"
author: "endual"
date: "2012-02-25T08:38:41Z"
tags: []
categories: ["开源软件"]
slug: "struts2"
draft: false
lastmod: "2012-02-25T08:38:41Z"
id: 1423494
url: "https://endual.iteye.com/blog/1423494"
---

```
struts2国际化

在struts2中需要做国际化的有：

jsp页面的国际化，action错误信息的国际化，转换错误信息的国际化，校验错误信息的国际化

在之前的例子中已经做过和国际化相关的例子了，在struts.xml中配置过

 

view plaincopy to clipboardprint?

    <constant name="struts.custom.i18n.resources" value="message"></constant>   

 

 

其中的message就是国际化资源文件的baseNmae。

我们先看看简单的在jsp中进行国际化

在src目录下新建message_en_US.properties，内容为

hello=add user

新建message_zh_CN.properties文件，内容为

hello=\u589e\u52a0\u7528\u6237

然后修改register2.jsp

要想使用国际化显示，可以将信息添加到<s:text></s:text> 标签中，也可以放在<s:i18n></s:i18n> 中，

在这里，先使用标签 <s:text></s:text>

增加以下内容：

 

view plaincopy to clipboardprint?

    <s:text name="hello"></s:text>   

 

 

重启服务器后，浏览该页，默认会显示出“增加用户”，可以在IE中打开Internet选项，在常规中选择语言，增加英语（美国）[en-US],然后设置为第一项，刷新就可以看到输出“add user”。

 

action错误的国际化

在message_en_US.properties中增加以下内容

username.invalid=username invalid...

在message_zh_CN.properties中增加以下内容

username.invalid=\u7528\u6237\u540d\u4e0d\u5408\u6cd5...

修改RegisterAction中的validate方法，将错误加到ActionError中，在这里将使用到ActionSupport中的getText方法获得和国际化资源文件相关的信息。

以username验证为例：

 

view plaincopy to clipboardprint?

    if (null == username || username.length() < 5 || username.length() > 10) {   
      
        this.addActionError(this.getText("username.invalid"));   
      
    }   



这样就从资源文件中读取username.invalid的值，增加到ActionError中。

 

查看该页面不输入任何数据，提交后就可以看到显示效果了。

 

验证框架的国际化（field级别错误）

在message_en_US.properties文件中增加以下内容

username.xml.invalid=validate information

在message_zh_CN.properties文件中增加以下内容

username.xml.invalid=\u9a8c\u8bc1\u6846\u67b6\u4fe1\u606f

然后修改验证框架，需要将在properties文件中的内容增加到框架中。

以username为例 

 

view plaincopy to clipboardprint?

    <field name="username">   
        <field-validator type="requiredstring">   
            <param name="trim">true</param>   
            <message key="username.xml.invalid"></message>   
        </field-validator>   
    </field>   


 

在message标签中增加属性key，值为properties文件中的key

标签中key大多是和国际化相关的

 

国际化资源文件的分类

当应用程序很大时，需要国际化的东西会很多，因此需要将国际化资源文件进行分类。

需要知道的是在src中的properties文件是全局资源文件，另外还可以分为包级别的和类级别的

首先看看包级别的

命名规则为package_language_country.properties

新建package_en_US.properties，内容为

username.xml.invalid=package validate information

新建package_zh_CN.properties，内容为

username.xml.invalid=\u5305\u9a8c\u8bc1\u4fe1\u606f

可以看到输出的信息为“包验证信息”，由此可见包级别的国际化资源文件的优先级高于全局国际化资源文件。

 

类级别

新建RegisterAction_en_US.properties，内容为

username.xml.invalid=class validate information

新建RegisterAction_zh_CN.properties，内容为

username.xml.invalid=\u7c7b\u9a8c\u8bc1\u4fe1\u606f

此时可以看到输出的信息为“类验证信息”。

由此可以得到国际化资源文件的优先级

全局<包级别<类级别

另外要进行表单的国际化时，要去掉theme="simple"

在RegisterAction_en_US.properties中增加

username.name=username

在RegisterAction_zh_CN.properties中增加

username.name=\u7528\u6237\u540d

修改表单标签

 

view plaincopy to clipboardprint?

    <s:textfield name="username" key="username.name"></s:textfield>   

 

 

注意到key一般是和国际化相关的。

另外除了用

 

 

另外除了用<s:text>这个标签外，还可以使用<s:i18n>这个标签

 

view plaincopy to clipboardprint?

    <s:i18n name="temp"></s:i18n>   

 

 

标签中包含name，代表着可以定义资源文件的baseName，如可以定义成temp，那么对应着

temp_en_US.properties和temp_zh_CN.properties这两个资源文件。

 

如定义：

 

 

view plaincopy to clipboardprint?

    <s:i18n name="hello">   
        <s:text name="world">   
            <s:param>struts2</s:param>   
        </s:text>   
    </s:i18n>   

 

 

注意到可以在<s:text>标签中增加<s:i18n> 标签。

在hello_en_US.properties文件中增加

world=hello {0}

hello_zh_CN.properties中增加

world=\u4f60\u597d,struts2

在struts2的默认拦截器栈中已经定义了i18n拦截器，所以struts2已经是一个国际化的框架了。

struts2会查找从客户端提交的request_locale属性，并存到session中的WW_TRANS_I18N_LOCALE字段

中。

这个<s:text> 标签外，还可以使用<s:i18n> 这个标签  

view plaincopy to clipboardprint?

    <s:i18n name="temp"></s:i18n>   

 总结一下显示方法： 

<s:textname="hello"></s:text>

getText("username.invalid") 

<message key="username.xml.invalid"></message>  

<s:textfield name="username" key="username.name"></s:textfield>    

<s:i18n name="temp"></s:i18n>
```