---
title: "struts2国际化小结"
author: "sundful"
date: "2011-02-28T17:46:49Z"
tags: ["JDK", "XML", "框架"]
categories: ["企业架构"]
slug: "struts2"
draft: false
lastmod: "2011-02-28T17:46:49Z"
id: 934813
url: "https://sundful.iteye.com/blog/934813"
---

一、底层是如何运作的  
1、如何获得当前JDK系统已经提供的国家语言的支持  
Locale\[] locales = Locale.getAvailableLocales();  
for(Locale locale : locales)  
{  
    //System.out.println(locale.getDisplayCountry() + " " + locale.getCountry());  
    System.out.println(locale.getDisplayLanguage() + " " + locale.getLanguage());  
}

2、属性配置文件  
命名规则：baseName\_language\_country.properties  
如：hellofile\_en\_US.properties  
    hellofile\_zh\_CN.properties  
例子:  
hellofile\_zh\_CN.properties中内容：  
hello=\\u4F60\\u597D  
hellofile\_en\_US.properties中内容:  
hello=world

例：  
Locale locale = Locale.getDefault();    //获得当前默认Locale

ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);        
String value = bundle.getString("hello");  
System.out.println(value);

bundle = ResourceBundle.getBundle("hellofile",Locale.US);  
value = bundle.getString("hello");  
System.out.println(value);

结果：  
你好  
world

3、  
hellofile\_en\_US.properties中内容：  
hello = world : {0}  
hellofile\_zh\_CN.properties中内容：  
hello = \\u4F60\\u597D : {0}

例：  
Locale locale = Locale.getDefault();

ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);        
String value = bundle.getString("hello");  
System.out.println(value);  
String result = MessageFormat.format(value, new Object\[]{"上海"});    //用上海填充{0}  
System.out.println(result);  
    
bundle = ResourceBundle.getBundle("hellofile",Locale.US);  
value = bundle.getString("hello");  
System.out.println(value);  
result = MessageFormat.format(value, new Object\[]{"北京"});    //用北京填充{0}

System.out.println(result);

二、struts2的国际化  
1、页面中的国际化  
（1）  
&lt;s:text name=""&gt;&lt;/s:text&gt;  
name中的值对应配置文件中的key

例：  
message\_zh\_CN.properties中内容：  
addUser = \\u589e\\u52a0\\u7528\\u6237  
页面中使用：  
&lt;s:text name="addUser"&gt;&lt;/s:text&gt;

（2）form表单中内容国际化theme属性不能为simple  
例：  
&lt;s:textfield key=""&gt;&lt;/s:textfield&gt;  
(3)临时国际化  
&lt;center&gt;  
    &lt;s:i18n name="temp"&gt;  
        &lt;s:text name="hello"&gt;&lt;/s:text&gt;  
    &lt;/s:i18n&gt;  
&lt;/center&gt;  
配置文件：temp\_zh\_CN.properties  
hello = \\u4f60\\u597d  
(4)传参  
&lt;s:i18n name="temp"&gt;  
    &lt;s:text name="hello"&gt;  
        &lt;s:param&gt;didi&lt;/s:param&gt;  
    &lt;/s:text&gt;  
&lt;/s:i18n&gt;  
配置文件：temp\_zh\_CN.properties  
hello =\\u4F60\\u597D,{0}

2、validate中的国际化  
通过getText方法获得Key对应的值  
例：  
message\_zh\_CN.properties中内容：  
username.invalid = \\u7528\\u6237\\u540d\\u586b\\u5199\\u4e0d\\u6b63\\u786e  
validate中使用方法：  
this.addActionError(this.getText("username.invalid"));

例：  
message\_zh\_CN.properties中内容：  
username.invalid = \\u7528\\u6237\\u540d "{0}" \\u586b\\u5199\\u4e0d\\u6b63\\u786e  
validate中使用方法：  
List list = new ArrayList();  
list.add(username);  
this.addActionError(this.getText("username.invalid",list));  
或  
this.addActionError(this.getText("username.invalid",new String\[]{username}));

3、校验框架XML中的国际化  
&lt;message key=""&gt;&lt;/message&gt;  
key对应配置文件中的key

例：  
校验XML中  
&lt;message key="username.xml.invalid"&gt;&lt;/message&gt;  
message\_zh\_CN.properties中内容：  
username.xml.invalid = \\u6821\\u9a8c\\u6846\\u67b6\\u63d0\\u793a\\u7528\\u6237\\u540d\\u4e0d\\u6b63\\u786e

4、国际化资源文件的存活范围  
struts2提供的各个级别的文件名命名规则  
包级别：package\_en\_US.properties    package\_zh\_CN.properties  
类级别：class\_en\_US.properties    (class用具体类名)

优先级：类级别&gt;包级别&gt;全局