struts2国际化 自动切换语言

首先让我们看一下简单的struts2的国际化应用时如何实现的。

测试项目工程的结构图如下:



 
 globalMessage.properties为默认配置资源文件

globalMessage_zh_CN.properties为中文配置资源文件

globalMessage_en_US.properties为英文配置资源文件

首先 为了让资源文件发挥作用,我们需要改变struts2的默认设置

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

 接着 在资源文件中添加我们index.jsp界面中需要国际化效果的键值对信息

globalMessage.properties,globalMessage_zh_CN.properties的内容为:



 
globalMessage_en_US.properties的内容为:



 完成了资源文件的基本配置 就要设计index.jsp struts2中为了获取国际化资源信息同样是采用struts标签

<s:text> 或者 form表单中可通过标签中的key属性来获取

index.jsp

<s:form action="userAction" method="post">
    	<s:textfield key="username" name="name"></s:textfield><s:fielderror fieldName="name"></s:fielderror>
    	<s:password key="password" name="password"></s:password><s:fielderror fieldName="password"></s:fielderror>
    	<s:submit key="submit"></s:submit>
    </s:form>
    
    <s:url action="localeAction" id="url">
    	<s:param name="lan" value="1"></s:param>
    </s:url>
    <s:url action="localeAction" id="url1">
    	<s:param name="lan" value="2"></s:param>
    </s:url>
	    <a href="<s:property value="#url"/>"><s:text name="china"></s:text></a>
	    <a href="<s:property value="#url1"/>"><s:text name="us"></s:text></a>

完成了以上基本步骤 我们看看如何实现自动切换环境语言

首先 我们知道 在action(继承了ActionSupport)中 如果要实现 错误信息的提示国际化得时候 我们这样:

this.addFieldError("", getText("key"));

 这里的key 就为我们上面资源文件配置中的key

从api我们看出 getText方法 是类ActionSupport实现了接口TextProvider

在我们查看 TextProvider 的api中 我们可以看到这么一段话:

You can override LocaleProvider.getLocale() to change the behaviour of how to choose locale for the bundles that are returned. 
Typically you would use the LocaleProvider interface to get the users configured locale. 

 大意也就是 我们如果想改变系统的locale 可以通过重写LocaleProvider的 getLocale()方法 从而来实现

那么我们就可以自定义action让其实现接口LocaleProvider 重写getLocale()方法。让其返回我们设置的locale

本次我们是通过链接 访问我们自定义的action 通过参数 来设置不同环境的locale 具体代码如下:

public class ChangeLocale implements LocaleProvider {

	private String lan;
	
	public String getLan() {
		return lan;
	}
	public void setLan(String lan) {
		this.lan = lan;
	}
	public Locale getLocale() {
		System.out.println("changelocale-------1");
		Locale locale=null;
		if(lan.equals("1")){
			locale=new Locale("zh", "cn");
		}else if(lan.equals("2")){
			locale=new Locale("en", "US");
		}
		return locale;
	}
}

 从以上代码可以看出 当链接过来的参数为1时 我们设置为中文环境 若为2并设置为英文环境

以上我们只是 实现了设置当前系统的语言环境 但是如何让系统采用我们当前设置 的locale呢 我们可以看一下 Struts2默认的i18n拦截器中有这么方法:

/**
     * Save the given locale to the ActionInvocation.
     *
     * @param invocation The ActionInvocation.
     * @param locale     The locale to save.
     */
    protected void saveLocale(ActionInvocation invocation, Locale locale) {
        invocation.getInvocationContext().setLocale(locale);
    }

 也就是通过setLocale()方法为当前action设置locale

那么我们就可以在我们自定义的action中这样实现了

public String execute(){
		System.out.println(lan);
		System.out.println("changelocale-------2");
		ActionContext ac=ActionContext.getContext();
		ac.setLocale(getLocale());
		return "success";
	}

完成了以上 我们这样就可以测试一下了

看看效果吧

点击英文后 效果如下:



 


 

猜你喜欢

转载自hxlzpnyist.iteye.com/blog/1541486