Struts2 的国际化


Struts2 的国际化
===============================
资源文件的级别
===============================
---------
全局的:
---------
命名:baseName + _语言码_国家码.properties
        或 baseName + _语言码.properties
        或 baseName + .properties
message_en_US.properties
            username.invalid = username invalid
message_zh_CN.properties
            username.invalid = 用户名错误
配置:
struts.xml
                                            value:对应于资源文件的baseName
<constant name="struts.coustom.i18n.resources"value="message" />
位置:classes下(/src)
---------
包级别的:
---------
命名:package_语言码_国家码.properties
package_en_US.properties
          username.invalid = usernameinvalid
package_zh_CN.properties
          username.invalid =用户名错误
位置:一个基础包中 

---------
类级别的:
---------
命名:类名+_语言码_国家码.properties
RegisterAction_en_US.properties
          username.invalid = usernameinvalid
RegisterAction_zh_CN.properties
          username.invalid =用户名错误
位置:与Action在同一目录
 
 
 
==================
jsp页面的国际化
==================
1.
src下建立2个文件 
message_en_US.properties
          内容: addUser=Add UserInformation
message_zh_CN.properties
          内容: addUser=添加用户信息
-------------------------------------
2. 
struts.xml
                                            value:对应于资源文件的baseName
<constant name="struts.coustom.i18n.resources"value="message" />
指定全局的国际化文件
-------------------------------------
3.
### jsp页面 文字的国际化 ###
<center>
  name:对应资源文件中的Key,如果资源文件中没有这个Key,将直接显示name的内容
  <s:textname="addUser"></s:text>
</center>
 
 
### 表单的国际化 ###

1.去掉<form> 中的theme=simple
2.<s:form action="login"><!-- 确保theme不为simple -->
  # 通过key的方式找到全局资源文件中对应的值
      <s:textfield name="username"key="page.index.text.username"></s:textfield>
      <s:password name="password"key="page.index.text.password"></s:password>
  # 通过label指定ognl表达式,找到所对应的资源文件中的值
      <s:password name="repassword"label="%{getText('page.index.text.repassword')}"/>
  </s:form>
3.提交了以后,将和action发生关联,优先找类级别的资源文件。
 


### 使用<s:i18n>标签指定临时的资源文件###

    1. i18n 标签从临时资源文件中获取信息
    <s:i18nname="temp">   -----baseName对应名称的资源文件名
      <s:textname="info">   -----info为资源文件中的key
      <s:param>张三</s:param>  ---- 指定第一个参数   {0}
      <s:param>23</s:param>    ---- 指定第二个参数  {1}
      </s:text>
    </s:i18n>
     
    <br/><br/>
     
    2.指定一个类,对应到那个类的资源文件
    <s:i18nname="org.scorpio.jh.struts2.i18n.web.action.LoginAction">
      <s:textname="page.index.text.username"></s:text>
      <s:textname="page.index.text.password"></s:text>
    </s:i18n>

在src下(classes下)建立:
命名:baseName + _语言码_国家码.properties
temp_en_US.properties
        info = User Name: {0} Age: {1}
        page.index.text.username = User Name
        page.index.text.password = Password
temp_zh_CN.properties
        info= 用户名:{0} 年龄:{1}        
        page.index.text.username = 用户名
        page.index.text.password = 密码
-------------------------------------
*** 更改浏览器的请求头:Internet选项---》语言---》可以添加一种语言,然后上移
-------------------------------------
 
================
action的国际化
================

1.
src下建立2个文件 
message_en_US.properties
        内容: username.invalid = username invalid

message_zh_CN.properties
        内容: username.invalid = 用户名填写不正确
-------------------------------------
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 )
    这个key可以代参数的字符串,比如:{0},{1}... 这个参数由List或String[]来提供
# 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 } ) );

 
 
=========================
  xml验证文件中国际化
=========================
<validators>
  <fieldname="username">
  <field-validatortype="requiredstring" short-circuit="true">

    <messagekey="page.index.error.username.empty"></message>

  </field-validator>
  <field-validatortype="stringlength">
    <paramname="minLength">4</param>
    <paramname="maxLength">20</param>

    <messagekey="page.index.error.username.length.out.of.bounds"></message>

  </field-validator>
  </field>
  <fieldname="age">
  <field-validatortype="int">
    <paramname="min">1</param>
    <paramname="max">100</param>

    <messagekey="page.index.error.age.size.out.of.bounds"></message>

  </field-validator>
  </field>
</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 ) {
  System.out.println(locals.getDisplayCountry() + " : " + locals.getCountry() );
}
=======================================
命名:
  baseName + _语言代码_国家代码.properties
英文
\src\hellofile_en_US.properties
        内容: hello=hello world
中文
\src\hellofile_zh_CN.properties
        内容: hello=你好
---------------------------------------
public class Test {
  public static void main(String[] args){
  Locale locale = Locale.getDefault();  //获得本地默认的国家
  ResourceBundle bundle =ResourceBundle.getBundle("hellofile",locale);
  String value = bundle.getString("hello");----hello 对应 属性文件中的key,得到的是这个键所对
应的值
  System.out.println(value);
  }
}

---------------------------------------
资源文件中的动态部分
如:
hello=你好:{0}
{0} --- 可以从程序中传入
必须从0开始,然后依次增加,如果没有找到对应的参数就将 {数字} 直接输出。
java.text.MessageFormat类
    static Stringformat(String pattern, Object... arguments)   
    -----Object...为可变参数,要放到参数的最后一个,可以成一个数组
  用这个数组的值填充前面字符串中的{0}..{1}...等参数
public static void main(String[] args) {
  Locale locale = Locale.getDefault();
  ResourceBundle bundle =ResourceBundle.getBundle("hellofile",locale);
  String value =bundle.getString("hello");
  String result = MessageFormat.format( value,new Object[]{"北京"} );
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值