Struts2 的国际化
===============================
资源文件的级别
===============================
---------
全局的:
---------
命名:baseName + _语言码_国家码.properties
message_en_US.properties
message_zh_CN.properties
配置:
struts.xml
<constant name="struts.coustom.i18n.resources"value="message" />
位置:classes下(/src)
---------
包级别的:
---------
命名:package_语言码_国家码.properties
package_en_US.properties
package_zh_CN.properties
位置:一个基础包中
---------
类级别的:
---------
命名:类名+_语言码_国家码.properties
RegisterAction_en_US.properties
RegisterAction_zh_CN.properties
位置:与Action在同一目录
==================
jsp页面的国际化
==================
1.
src下建立2个文件
message_en_US.properties
message_zh_CN.properties
-------------------------------------
2.
struts.xml
<constant name="struts.coustom.i18n.resources"value="message" />
指定全局的国际化文件
-------------------------------------
3.
### jsp页面 文字的国际化 ###
<center>
</center>
### 表单的国际化 ###
1.去掉<form> 中的theme=simple
2.<s:form action="login"><!-- 确保theme不为simple -->
3.提交了以后,将和action发生关联,优先找类级别的资源文件。
### 使用<s:i18n>标签指定临时的资源文件###
在src下(classes下)建立:
命名:baseName + _语言码_国家码.properties
temp_en_US.properties
temp_zh_CN.properties
-------------------------------------
*** 更改浏览器的请求头:Internet选项---》语言---》可以添加一种语言,然后上移
-------------------------------------
================
action的国际化
================
1.
src下建立2个文件
message_en_US.properties
message_zh_CN.properties
-------------------------------------
2.
action中的validate()
this.addActionError( this.getText("username.invalid") );
------------
# String getText(Stringkey)方法:通过一个资源文件中的key的字符串获得该键所对应的值
# String getText( String key, List args )
# String getText( String key, String[] args )
# getText( String key, String defaultValue )
如果key没有找到,用默认值代替
如:
------------
资源文件中
en
username.invalid = username {0} invalid
zh
username.invalid = 用户名 {0} 填写不正确
------------
action中:
List list = new ArrayList();
list.add(username);
使用List
this.addActionError( this.getText( "username.invalid", list ));
使用数组
this.addActionError( this.getText( "username.invalid", newString[] { username } ) );
=========================
=========================
<validators>
</validators>
------- 资源文件 -------
page.index.error.username.empty = 请填写用户名
page.index.error.username.length.out.of.bounds =请输入${minLength}到${maxLength}个字符
page.index.error.age.size.out.of.bounds =年龄只能在${min}~${max}之间
# 类型转换错误
invalid.fieldvalue.age = 请填写正确的年龄
# 补充 采用Java国际化 #
=======================================
ResourceBundle类(资源包)
Locale类
static Locale[] getAvailableLocales()
国家
String getDisplayCountry()
获得国家:如:中国
String getCountry()
获得国家码,如:CN
语言
String getDisplayCountry()
获得语言,如:中文
String getLanguage()
获得国家码,如:zh
Locale[] locales = Locale.getAvailableLocales();
for( Locale locale : locales ) {
}
=======================================
命名:
英文
\src\hellofile_en_US.properties
中文
\src\hellofile_zh_CN.properties
---------------------------------------
public class Test {
应的值
}
---------------------------------------
资源文件中的动态部分
如:
hello=你好:{0}
{0} --- 可以从程序中传入
必须从0开始,然后依次增加,如果没有找到对应的参数就将 {数字} 直接输出。
java.text.MessageFormat类
public static void main(String[] args) {
}