国际化之struts

原理:

 //得到Locale 对象数组,java全部所支持 //Locale 对象表示了特定的地理、政治和文化地区
  Locale [] locales = Locale.getAvailableLocales();
  for (Locale locale : locales) {
   //名字及编号
   System.out.println(locale.getDisplayCountry() + "  " + locale.getCountry());
   System.out.println(locale.getDisplayLanguage() + "  " + locale.getLanguage());

  Locale locale = Locale.getDefault();
  Locale locale_en = new Locale("en", "US");//或者Locale locale_en = Locale.US;
  ResourceBundle rb = ResourceBundle.getBundle("test", locale);
  ResourceBundle rb_en = ResourceBundle.getBundle("test", locale_en);
  String str = rb.getString("hello");
  String str_en = rb_en.getString("hello");
  System.out.println(str);
  System.out.println(str_en);

  //Locale 对象表示了特定的地理、政治和文化地区
  Locale locale = Locale.getDefault();
  //test表示文件名前缀(如test_zh_CN.properties)
  ResourceBundle rb = ResourceBundle.getBundle("test", locale);
  //hello表示语言包中的key
  String value = rb.getString("hello");
  //替换字符,{0}表示第0个对象
  String message = MessageFormat.format(value, new Object[]{"message"});
  System.out.println(message);

基于struts:(struts2)

加入strutsjar包

struts标签中加入

<struts>

 <!--test表示语言包文件的前缀名,如test_zh_CN.properties-->

 <constant name="struts.custom.i18n.resources" value="test"></constant>
 
 <constant name="struts.i18n.encoding" value="utf-8"></constant>

</struts>

在web.xml中加入过滤器,在启动之初把struts加入容器,标签替换语言包的内容

 <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
   org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
 </filter>

 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

在网页中

    <p>
     <s:text name="hello"></s:text>
    </p>
   
    <p>
  <s:i18n name="test">
   <s:text name="hello">
    <s:param>xusong</s:param>
   </s:text>
  </s:i18n>
 </p>

猜你喜欢

转载自xiaosong96.iteye.com/blog/1066757