现在的位置: 首页 > 综合 > 正文

Struts2国际化

2018年05月20日 ⁄ 综合 ⁄ 共 4777字 ⁄ 字号 评论关闭

1:java内置的国际化

     java内置的国际化,以java.util.ResourceBundle和java.util.Locale两个类为中心,其中java.util.Locale负责选择合适的语言,而java.util.ResourceBundle负责根据注入的java.util.Locale对象来选择国际化信息的来源,返回国际化信息。

    简单示例:

     准备3份文件:

       * 默认语言文件:message.properties

          labela = labela in zh_CN

          labelb = labelb in zh_CN

        *中文语言文件

          labela = labela in zh_CN

          labelb = labelb in zh_CN

         *英文语言文件

          labela = labela in en_US
          labelb = labelb in en_US

       java类,在引用多语言信息的时候,要做3件事情:

             *引用哪种语言。

             *引用哪组多语言文件

             *引用这种语言文件的这组文件里的哪条信息

         代码:

     

package com.capinfotech.util;

import java.util.Locale;
import java.util.ResourceBundle;

public class Language {

	public static void main(String[] args) {
		
		Locale local1= Locale.SIMPLIFIED_CHINESE;
		ResourceBundle bundle1 = ResourceBundle.getBundle("message", local1);
		System.out.println("bundle=" + bundle1.getString("labela"));
		
		Locale enLocal = Locale.US;
		ResourceBundle bundle2 = ResourceBundle.getBundle("message", enLocal);
		System.out.println("enBundle=" + bundle2.getString("labela"));
		
		Locale franceLocal = Locale.FRANCE;
		ResourceBundle franceBundle = ResourceBundle.getBundle("message", franceLocal);
		System.out.println("franceLocal=" + franceBundle.getString("labela"));
		
	}
}

输出结果:

bundle=labela in zh_CN
enBundle=labela in en_US
franceLocal=labela in zh_CN

2:向国际化信息中传入参数

  labela = your order({0}) is confirmed, can not be modified

java类:

	Locale local1= Locale.SIMPLIFIED_CHINESE;
		ResourceBundle bundle1 = ResourceBundle.getBundle("message", local1);
		String message = bundle1.getString("labela");
		//将参数合成进读出的国际化信息
		String info = MessageFormat.format(message, "20100000000");
		System.out.println(info);

输出结果:

you order(20100000000) is confimed, can not be modified

如果有两个参数,就要需要使用数组了,

字符串为:labela={0}, your order({1})  is confirmed, cannot be modified

java类:

Locale local1= Locale.SIMPLIFIED_CHINESE;
		ResourceBundle bundle1 = ResourceBundle.getBundle("message", local1);
		String message = bundle1.getString("labela");
		String[] params = new String[2];
		params[0] = "Petter";
		params[1] = "20100000000";
		//将参数合成进读出的国际化信息
		String info = MessageFormat.format(message, params);
		System.out.println(info);

输出结果:

your label=Petter you order(20100000000) is confimed, can not be modified

3:Struts2中的国际化入门

    *首先包含国际化资源文件message.properties, message_zh_CN.properties, message_en_US.properties

    *然后在struts.xml中设置一个常量:

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

     *在jsp中访问国际化信息

     <s:text name="labela" />

4:资源文件读取顺序

    *全局资源文件

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

   这里设置的message是一组国际化资源文件名的前缀,如果有多组国际化资源文件,则可以用逗号分隔,如"message, dictionary",就表示还另有以dictionary为前缀名的国际化资源文件,这些文件都放在classpath的根目录上。

    

    *包级资源文件

     包级资源文件,在当前访问的Action的同包下,声明一组package.properties, 可能包括package.properties, package_zh_CN.properties, package_en_US.properties等。

     如果包级资源文件和全局级资源文件同时存在,则包级资源文件覆盖全局级资源文件中的同样的key的值。

     *类级资源文件

     在类的包下建立以该Action类名为前缀的国际化资源文件,可能包括Action类名.properties, Action类名_en_US.properties, Action类名_zh_CN.properties等。

    比如访问的Action是com.capinfotech.I18NAction, 那么要设置的包级文件是 I18NAction_zh_CN.properties等。

    

优先级的顺序为:类级资源文件> 包级资源文件 > 全局级资源文件

 

5:Struts2访问国际化信息的不同方式

     *使用<s:text>访问国际化信息

     Struts2官方文档对该标签的说明如下:

             

The message must be in a resource bundlewith the same name as the action that it is associated with. In practicethis means that you should create a properties file in the same packageas your Java class with the same name as your class, but with .propertiesextension.

If the named message is not found in a property file, then the body of thetag will be used as default message. If no body is used, then the stack willbe searched, and if a value is returned, it will written to the output.If no value is found on the stack,
the key of the message will be written out.

即最好在该Action的包下面建一个以该类名为前缀的properties文件

      比如有:labela = labela in zh_CN

                      labelb = labelb, {0}

     在jsp文件中:

          <s:text name="labelb">

                 <s:param>k</s:param>

         </s:text>  

输出结果为:

 labela in zh_CN
labelb ok

    

<s:text name="labela" />    
    <br>
    <s:text name="labelb" >
        <s:param>ok</s:param>
    </s:text>
    <br>
    <s:i18n name="com.capinfotech.action">
        <s:text name="labela" />
    </s:i18n>
    
    <s:form>
        <s:textfield name="name1" key="labela" />
    </s:form>

*在Action中访问国际化信息

   

		System.out.println(this.getText("labela"));  //访问默认的国际化信息
		System.out.println(this.getText("labelb", null, "ok")); //访问默认的国际化信息并传入参数值为ok

		ResourceBundle bundle = this.getTexts("message"); //访问名为message的全局国际化信息
		System.out.println(bundle.getString("labela"));

输出结果如下:

labela in zh_CN in class properties
labelb ok
labela in zh_CN for global

6:指定语言信息的不同方式

    *使用struts的常量设置

    <constant name="struts.locale" value="zh_CN" />

     

    *用户选择语言:

     Struts2提供了最灵活,也是级别最高的方式,那就是由用户选择语言,用户只要在提交请求时加上request_locale这个参数,并提供对应的值就可以自由选择语言了。

     这得利于Struts2的i18n内建拦截器,defaultStack拦截器栈引用了i18n拦截器,因此,能很方便的由用户来选择语言。i18n拦截器在Action运行之前会检查请求中是否包含了了一个叫做"request_locale"的参数,如果存在该参数,则会用它的值建立一个Locale对象,并用这个Locale对象去覆盖Struts2的常量设置和浏览器的语言设置,除此之外,Strut2还会把这个Locale对象保存到名为WW_TRANS_I18N_LOCALE的Session属性中去,而这个属性也会覆盖 Struts2的常量设置和浏览器的语言设置,因此,只需要传入一次request_locale参数,session就会记住用户的选择,整个网站就会都变成用户选择的语言。

   如果在页面中使用: <s:property value="#session.WW_TRANS_I18N_LOCALE" default="找不到"/>来获得session中的WW_TRANS_I18N_LOCALE属性值

输出结果下图所示:

  

 

    

抱歉!评论已关闭.