国际话浅谈
来源:互联网 发布:netbeans php教程 编辑:程序博客网 时间:2024/04/30 06:02
一、Struts2中哪些部分需要国际化?前台页面,Action类,校验配置文件。
二、Struts2的国际化资源文件分为两种:全局的国际化资源文件和局部的国际化资源文件
1、全局的国际化资源文件可以被整个应用程序引用,放在类路径根目录下,命名规则如下:
baseName(可变)_语言代码_国家代码.properties
例如:
message_en_US.properties
message_zh_CN.properties
2、局部的国际化资源文件包括包级别的和类级别的。
(1)、包级别的资源文件仅被当前包中的类引用,命名规则如下:
package(固定不变)_语言代码_国家代码.properties
例如:
package_en_US.properties
package_zh_CN.properties
(2)、类级别的资源文件仅被当前Action类引用,命名规则如下:
ActionName(可变)_语言代码_国家代码.properties
例如:
LoginAction_en_US.properties
LoginAction_zh_CN.properties
多个国际化资源文件中有同名(key相同)的消息,当在Action中使用该消息时,类级别的优先级最高,类级别的会覆盖包级别的,包级别的会覆盖全局的。
三、com.opensymphony.xwork2.ActionSupport类中的方法:
public String getText(String key);
public String getText(String key, String defaultValue, String obj);
public String getText(String key, String[] args);
public String getText(String key, List args);
四、应用示例
以全局的国际化资源文件为例:
首先,编写国际化资源文件:
message_en_US.properties(美国英语资源文件)
message_zh_CN.properties(简体中文资源文件)
message.properties(默认资源文件)
然后,在配置文件struts.xml中添加以下内容:
<constant name="struts.custom.i18n.resources" value="message"/>
或者
在struts.properties文件中添加以下内容:
struts.custom.i18n.resources=message
1. JSP页面上的国际化(使用struts2的<s:i18n ...>标签和<s:text .../>标签)
(1)使用默认资源文件
<!-- 消息中没有参数-->
<s:text name="welcome"/>
<!-- 消息中有参数-->
<s:text name="hello">
<s:param>张三</s:param>
</s:text>
(2)使用<s:i18n>标签的name属性指定从某个特定的资源文件中获取数据
当有多个baseName不同名的国际化资源文件时,可使用<s:i18n>标签绑定特定的资源。
<s:i18n name="message">
<s:text name="hello">
<s:param>张三</s:param>
</s:text>
</s:i18n>
<s:i18n name="com/xawx/web/action/package">
<s:text name="hello">
<s:param>zhangsan</s:param>
</s:text>
</s:i18n>
<s:i18n name="com/xawx/web/action/RegisterAction">
<s:text name="hello">
<s:param>zhangsan</s:param>
</s:text>
</s:i18n>
message.properties文件内容:
welcome=welcome you!
hello=hello world,{0}!
message_en_US.properties文件内容:
welcome=welcome you!
hello=hello world,{0}!
message_zh_CN.properties文件内容:
welcome=\u6b22\u8fce\u4f60!
hello=\u4f60\u597d,{0}!
(3). 表单元素中label属性的国际化
前提:theme属性不能设置为simple
未国际化的写法:
<s:textfield name="username" label="username"/>
<s:textfield name="password" label="password"/>
国际化时的写法:
<s:textfield name="username" key="username"/>
<s:textfield name="password" key="password"/>
或者
<s:textfield name="username" label="%{getText('username')}"/>
<s:textfield name="password" label="%{getText('password')}"/>
message.properties文件内容:
username=username
password=password
message_en_US.properties文件内容:
username=username
password=password
message_zh_CN.properties文件内容:
username=\u7528\u6237\u540d
password=\u5bc6\u7801
2. Action类中的国际化
未国际化的写法:
this.addFieldError("username", "the username error!");
this.addFieldError("password", "the password error!");
国际化时的写法:
this.addFieldError("username", getText("username.error"));
this.addFieldError("password", getText("password.error"));
message.properties文件内容:
username.error=username error!
password.error=password error!
message_en_US.properties文件内容:
username.error=username error!
password.error=password error!
message_zh_CN.properties文件内容:
username.error=\u7528\u6237\u540d\u9519\u8bef!
username.error=\u5bc6\u7801\u9519\u8bef!
<s:fielderror fieldName="fieldName"/>
或者
<s:fielderror>
<!--<s:param>password</s:param>-->
<s:param value="'password'"></s:param>
</s:fielderror>
3. 配置文件中的国际化
以输入校验的LoginAction-validation.xml为例:
未国际化:
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>用户名不能为空!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">20</param>
<message>用户名长度应在${minLength}至${maxLength}之间!</message>
</field-validator>
</field>
国际化后:
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<!-- 第一种用法 -->
<!--<message key="username.required"></message>-->
<!-- 第二种用法 -->
<message>${getText("username.required")}</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">20</param>
<message>${getText("username.size")}</message>
</field-validator>
</field>
message_en_US.properties文件内容:
username.required=the username should be required!
username.size=the size of username shoule be between ${minLength} and ${maxLength}!
message_zh_CN.properties文件内容:
username.required=\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a!
username.size=\u7528\u6237\u540d\u957f\u5ea6\u5728${minLength}\u5230${maxLength}!
- 国际话浅谈
- 浅谈:中国艺术品如何走向国际
- Java的国际话
- 浅谈php国际(I18N)以及config包的使用
- eclipse 国际话插件 安装
- 浅谈程序员数学修养:国际公司为什么用算术题招聘测试
- 关于storyboard的国际话问题
- 国际产业
- 国际支付
- 国际DHL
- 国际信用卡
- 国际8
- 聚集国际
- 国际学院
- iOS8 国际
- 国际跳棋
- 浅谈
- 国际冠码与国际电话区号
- c#与c++区别
- Qt控件的使用
- 消息响应COMMAND 和 UPDATE_COMMAND_UI的区别
- Updatedata的应用:
- 第二道笔试题
- 国际话浅谈
- 二级vf(第一章)
- gdb常用命令
- 慎用 .toString()
- linux 内核移植(基本框架)
- 游戏地图问题
- Python3中使用代理访问网页
- Google Android工程师多图详解Android的系统架构
- Struts2的工作原理