Struts2国际化

1.查看jdk文档 国际化主要依赖util下面的两个包ResourceBundle(资源包)  Locale  和 java.text.*包下的MessageFormat
2.在src目录下建立basename_language_country.properties
  如:hellofile_en_US.properties中的内容是 hello=helloword : {0}
   hellofile_zh_CN.properties中的内容是 hello=/u4f60/u597d : {0}
==================
public class Test1
{
 public static void main(String[] args)
 { 
 --用getAvailableLocales()获得当前jdk中已经安装好的local实例
  Locale[] locales = Locale.getAvailableLocales();
   -- 遍历所有locals
  for(Locale locale : locales)
  {
--getDisplayCountry()标识是-美国 : getCountry()用code码标识是-US
  System.out.println(locale.getDisplayCountry() + " : " + locale.getCountry());
--getDisplayLanguage()标识是-中文 : getLanguage()标识是-zh
  System.out.println(locale.getDisplayLanguage() + " : " + locale.getLanguage());
  }
 }

}

==========如:hellofile_en_US.properties中的内容是 hello=helloword
         hellofile_zh_CN.properties中的内容是 hello=/u4f60/u597d
public class Test2
{
 public static void main(String[] args)
 { 
  --获得系统默认的local
  Locale locale = Locale.getDefault();
  Locale locale = Locale.US;

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

==============如:hellofile_en_US.properties中的内容是 hello=helloword : {0}
         hellofile_zh_CN.properties中的内容是 hello=/u4f60/u597d : {0}
public class Test3
{
 public static void main(String[] args)
 {
  Locale locale = Locale.US;
  ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);
  String value = bundle.getString("hello");

  --用数组里面的北京(new Object[]{"北京"})来填充前面的partten(value){0}
  String result = MessageFormat.format(value,new Object[]{"北京"});
  
  
  System.out.println(result);
 }
}


--英文国际化message_en_US.properties中的内容是
  addUser = Add User Information
  username.invalid = username "{0}" invalid
  username.xml.invalid = username xml invalid
--中文国际化message_zh_CN.properties中的内容是 
  addUser = /u589e/u52a0/u7528/u6237/u4fe1/u606f
  username.invalid = /u7528/u6237/u540d "{0}" /u586b/u5199/u4e0d/u6b63/u786e
  username.xml.invalid = /u9a8c/u8bc1/u6846/u67b6/u63d0/u793a/u7528/u6237/u540d/u4e0d/u6b63/u786e
  struts.messages.invalid.token = /u8bf7/u4e0d/u8981/u91cd/u590d/u63d0/u4ea4/u8868/u5355
====================JSP页面的国际化
<constant name="struts.custom.i18n.resources" value="message"></constant>全局国际化文件message.properties
小提示:请记得修改浏览器中的语言选项 什么语言对应什么国际化文件
       类级别国际化>包级别国际化>全局(程序级)国际化 当三种同时存在只显示类级别的

--temp_en_US.properties
  addUser = world,{0}
--temp_zh_CN.properties
  addUser = /u4f60/u597d,{0}

<center>
 --从根目录下那个国际化文件中去找根据bsename name="temp"
 <s:i18n name="temp">
  --name="addUser"对应**.properties的addUser
  <s:text name="addUser">
  --langsi就是传给{0}这个占位符
   <s:param>langsin</s:param>
  </s:text>
 </s:i18n>
</center>

===============action的国际化
get.text(key)中的key相当message_en_US.properties中的username.invalid
if (null == username || username.length() < 6 || username.length() > 10)
{
 this.addActionError(this.getText("username.invalid");

 List list = new ArrayList();
 list.add(username);
--数组和list是一样的建议用数组
 this.addActionError(this.getText("username.invalid",new String[]{username}));
 this.addActionError(this.getText("username.invalid",list);
}

===============校验框架xml的国际化key="username.xml.invalid"
<field name="username">
  <field-validator type="requiredstring">
  <param name="trim">true</param>
  <message key="username.xml.invalid"></message>
</field-validator>
当校验出现错误时就会找<message key="username.xml.invalid"/>
key中的username.xml.invalid对应message_zh_CN.properties username.xml.invalid


========包级别的
--package_en_US.properties
  username.xml.invalid = package information
--package_zh_CN.properties
  username.xml.invalid = /u5305/u7ea7/u522b/u63d0/u793a/u4fe1/u606f

=======类级别的
--RegisterAction_en_US.properties
  username.xml.invalid = RegisterAction information
--RegisterAction_zh_CN.properties
  username.xml.invalid = /u7c7b/u7ea7/u522b/u63d0/u793a/u4fe1/u606f

=========表单中属性的国际化key="username.xml.invalid"
<s:textfield name="username" key="username.xml.invalid" id="usernameId"></s:textfield>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值