struts2 和 java 国际化小结

来源:互联网 发布:typescript 调用js库 编辑:程序博客网 时间:2024/05/07 23:05
1.java中使用国际化.

  国际化即internationalization简称i18n.

  java中是通过配置文件名为baseName-language-country.properties内容为key-value形式的属性文件来实现国际化的.

  可以通过Local类来查看java支持那些国家与语言的国际化.

  Local[] ls=Local.getAvailableLocales();

  for(Local local:ls){

  System.out.println(local.getDisplayCountry()+" "+local.getCountry());

  System.out.println(local.getDisplayLanguage()+" "+local.getLanguage());

  System.out.println("----------");

  }

  根据资源文件获取内容.

  //获得本地属性

  Local l=Local.getDefault();

  //根据本地国家语言来获得classes路径下baseName为message的资源文件

  ResourceBundle rb=ResourceBundle.getBundle("message",l);

  //获得key的值

  String text=rb.getString("username");

  //可以传入参数,如果资源文件这样定义的话:username={0}用户名

  String textp=MessageFormat.format(rb.getString("username"),"普通");

  2.struts2国际化用法

  先要配置baseName来指定资源文件,可在struts.xml文件中配置.

  <constant name="struts.custom.i18n.resources" value="message"></constant>

  然后在classes路劲下添加资源文件.

  资源文件内容写法.

  a.不带参数:key=value

  b.带索引参数:key={0}value{1}

  c.带表达式参数:key=${username}value 表示从ValueStack中直接获得username变了值,此种方式不需要像(b)那样手动传递参数

  访问国际化消息.

  jsp页面:或value1

  action中:getText()方法

  3.struts2资源文件的管理

  有4类资源文件:

  a.全局范围,在classes路径下.

  baseName-language-country.properties.如message_zh_CN.properties

  b.包范围,在包根路径下.

  package-language-country.properties.如package_zh_CN.properties

  c.类范围,在该类同一路径下.

  actionName-language-country.properties.如LoginAction_zh_CN.properties

  d.临时资源文件,在jsp页面使用标记来指定classes路径下的资源文件.

  <s:i18n name="message">

  <s:form.... </s:i18n>

原创粉丝点击