struts2的国际化中英文切换

为了实现程序的国际化,必须先提供程序所需要的资源文件。资源文件的内容基本是key-value对,其中key是程序使用的部分,而value是程序的显示部分。

资源文件的命名可以是如下3种形式:

  1. baseName_language_country.properties
  2. baseName_language.properties
  3. baseName.properties

其中baseName是资源文件的基本名称,用户可自由定义,而language和country是不变的,必须是Java所支持的语言和国家。(用到哪个国家语言资源可查官方文档)

Java不可能支持所有的国家和语言,可以通过Locale类的getAvailableLocale方法获取支持的,该方法返回一个Locale数组,该数组中包含了所有支持的国家和语言。

创建资源文件:

globalMessages_en_US.properties(英文)

1

globalMessages_zh_CN.properties(中文)

2

编写jsp页面,在这个jsp上测试中英文切换效果:

<s:i18n name="globalMessages">
  	
    <form id="form1" action="${pageContext.request.contextPath }/LoginAction_login" method="post">
    <div>
        <table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" valign="middle"><table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><img src="Img/login_1.jpg" width="659" height="104"></td>
        </tr>
      <tr>
        <td width="53%" class="login_p1">
        <div class="login_p2">
        <div class="login_p3">
        <table width="100%" border="0" align="left" cellpadding="0" cellspacing="0">
            <tr>
              <td height="44" align="left" valign="bottom" class="wenzi"><s:text name="username"/></td>
            </tr>
            <tr>
            <s:text name="check"></s:text>:
		  	<a href="LoginAction_login?request_locale=zh_CN"><s:text name="chinese"></s:text></a>  
			<a href="LoginAction_login?request_locale=en_US"><s:text name="english"></s:text></a>  
            </tr>
            <tr>
              <td width="87%" height="58" align="left" valign="bottom">                  
                  <input name="userName" type="text">                
              </td>
            </tr>
            <tr>
              <td height="60" align="left" valign="bottom" class="wenzi"><s:text name="password" /></td>
            </tr>
            <tr>
              <td height="58" align="left" valign="bottom">
                 <input name="password" type="password">
              </td>
            </tr>
            <tr>
              <td height="55" align="left" valign="middle"><input type="checkbox" name="checkbox" id="checkbox">
                <s:text name="remember"/></td>
              </tr>
            <tr>
              <td height="77" align="center" valign="bottom">
<!--                    <input id="btnLogin" type="submit" value="登录"> -->
                   <s:submit value="%{getText('loginBtn')}" id="btnLogin"/>
            </td>
            </tr>
          </table>
        </div>
        </div>
        </td>
        </tr>
      <tr>
        <td height="128" align="center"><span class="login_p5">Copyright&copy;版权所有2016江苏美正生物科技有限公司 All Rights Reserved.</span></td>
      </tr>
    </table></td>
  </tr>
</table>
    </div>
    </form>
</s:i18n>

这里有一个i18n,他就是Internationalization(国际化)的英文缩写,为什么是i18n呢?

Internationalization去掉头尾的i和n刚好还剩下18个字符,涨姿势了,原来还可以这样命名

编写action:

public String login(){
		if(model.getUserName()==null){
			return "login";
		}
		User user= userService.login(model);
		if(user !=null){
			ServletActionContext.getRequest().getSession().setMaxInactiveInterval(-1);
			ServletActionContext.getRequest().getSession().setAttribute("loginUser", user);
			ServletActionContext.getRequest().setAttribute("menuIds", Arrays.asList(getCurrUserMenuIds().split(", ")));
			ServletActionContext.getRequest().setAttribute("loginUserId", user.getId());
			return "home";
		}else {
			this.addActionError(this.getText("loginError"));
			return "login";
		}
		}

在struts2中,提供了一个i18n的拦截器,这个拦截器在执行Action方法之前,自动查找请求中名为request_locale的参数,拦截后转换为Locale对象,放置在用户session的名为“WW_TRANS_I18N_LOCALE”的属性。I18n在程序运行时会被自动加载,我们可以利用这些来设置允许用户自动选择网页的语言。

在struts.xml中配置拦截器和国际化的资源:

struts.xml:

 <constant name="struts.custom.i18n.resources" value="globalMessages"></constant>
    
    <package name="CurvePlatform" extends="struts-default" namespace="/">
      <!-- i18n语言包拦截器 -->  
      <interceptors>
			<interceptor name="myInter" class="com.wade.CurvePlatform.controller.CheckInterceptor"></interceptor>
	  </interceptors>    
      <action name="LoginAction_*" class="loginAction" method="{1}">
        <result name="home">/index.jsp</result>
        <result name="login">/login.jsp</result> 
        <interceptor-ref name="defaultStack"></interceptor-ref>
		<interceptor-ref name="myInter"></interceptor-ref>           
      </action>

CheckInterceptor.java:

import java.util.Locale;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class CheckInterceptor extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation ai) throws Exception {
		ActionContext ac = ai.getInvocationContext();
		Map session = ac.getSession();
		Locale locale = (Locale)session.get("WW_TRANS_I18N_LOCALE");
		if(locale==null){
			locale = new Locale("zh","CN");
			session.put("WW_TRANS_I18N_LOCALE",locale);
		}
		return ai.invoke();
	}

}

猜你喜欢

转载自blog.csdn.net/weixin_42656571/article/details/95326570
今日推荐