struts 多语言切换

首先在struts.properties文件中加入以下内容:
struts.custom.i18n.resources=messageResource
或在struts.xml中加入
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>

 

资源文件的命名格式: 名称_语言代码_国家代码. Properties
如果创建中文和英语国际化,那么资源文件名称为
messageResource_zh_CN.properties和messageResource_en_US.properties

 

1. jsp页面的国际化 
通过使用标签<s:text name="label.helloWorld"/>输出国际化
label.helloWorld为资源文件中定义的key

 


在messageResource_en_US.properties加入以下内容
label.hello=hello {0}
label.helloWorld=hello,world

在messageResource_zh_CN.properties加入以下内容
label.hello=你好 {0}
label.helloWorld=你好,世界

 

(1). <s:text name="label.helloWorld"/>
<s:property value="%{getText('label.helloWorld')}"/>
上面两个都为输出一个hello word的两种表示

 

<s:textfield name="name" key="label.helloWorld"/>
<s:textfield name="name" label="%{getText('label.helloWorld')}"/>
显示一个文本框,文本框的标题进行国际化

 

(2). 使用<s:i18n>标签指定从某个特定的资源文件中取数据
<s:i18n name="messageResource">
   <s:text name="label.helloWorld"></s:text>
</s:i18n>
指定在从messageResource取资源

 

(3).
<s:text name="label.hello">
   <s:param>callan</s:param>
</s:text>
使用带参数的资源.<s:param>可以替换label.hello=hello {0}中的{0}

 


首先把Struts2的环境搭建起来,

 2.建立一个action.测试i18n的。

 3.下面这个是struts.xml的简单配置,里有2中properties文件的配置,一种是全局的,一种是局部的,

Xml代码
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5.  
  6. <struts>
  7. <constant name="struts.devMode" value="true" />
  8. <!-- 局部的配置 -->
  9. <!--
  10. <constant name="struts.custom.i18n.resources" value="com/test/action/I18n"></constant>
  11. -->
  12. <!-- 全局的配置 -->
  13. <!-- -->
  14. <constant name="struts.custom.i18n.resources" value="test"></constant>
  15. <package name="default" namespace="/" extends="struts-default">
  16. <action name="" class="com.test.action.I18nAction">
  17. <result >/index.jsp</result>
  18. </action>
  19. </package>
  20. </struts>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <constant name="struts.devMode" value="true" />
	<!-- 局部的配置 -->
	<!--
	<constant name="struts.custom.i18n.resources" value="com/test/action/I18n"></constant>     
	-->
	<!-- 全局的配置 -->
	<!-- -->
<constant name="struts.custom.i18n.resources" value="test"></constant> 
    <package name="default" namespace="/" extends="struts-default">
		<action name="" class="com.test.action.I18nAction">
			<result >/index.jsp</result>
		</action>
    </package>
</struts>

 

4.根据struts2的配置,插件一个名字为test_en_US.properties和test_zh_CN.properties的配置文件,

test_en_US.properties里面的内容为:hello=hi,hello

test_zh_CN.properties里面的内容为:hello=\u4F60\u597D (注意了:这个是通过编码编译过来的,也可以试用MyEclipse的properties自动编辑转换实现)。

 

5.下面是jsp的展现页面:本人整理了以下几种实现方法,

 

 

Html代码
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  6. %>
  7.  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  9. <html>
  10. <head>
  11. <base href="<%=basePath%>">
  12.  
  13. <title>My JSP 'index.jsp' starting page</title>
  14. <meta http-equiv="pragma" content="no-cache">
  15. <meta http-equiv="cache-control" content="no-cache">
  16. <meta http-equiv="expires" content="0">
  17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  18. <meta http-equiv="description" content="This is my page">
  19. </head>
  20.  
  21. <body>
  22. <a href="<%=basePath%>?local=zh_CN">中文</a>
  23. <a href="<%=basePath%>?local=en_US">英文</a>
  24. This is my JSP page. <br>
  25. <s:debug></s:debug>
  26. property:<s:property value="getText('hello')"/><br>
  27. text:<s:text name="hello"></s:text><br>
  28. i18n:<s:i18n name="test">
  29. <s:text name="hello"></s:text>
  30. </s:i18n>
  31. </body>
  32. </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
  <a href="<%=basePath%>?local=zh_CN">中文</a>
  <a href="<%=basePath%>?local=en_US">英文</a>
    This is my JSP page. <br>
    <s:debug></s:debug>
    property:<s:property value="getText('hello')"/><br>
    text:<s:text name="hello"></s:text><br>
    i18n:<s:i18n name="test">
    	<s:text name="hello"></s:text>
    </s:i18n>
  </body>
</html>

 

 

6.想要实现中英文切换,还要在action中加入这一一句话

Java代码
  1. Locale locale=new Locale("zh","CN");//(这个能根据你传来的值动态改变)
  2. ServletActionContext.getRequest().getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);
Locale locale=new Locale("zh","CN");//(这个能根据你传来的值动态改变)
ServletActionContext.getRequest().getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);

 

7.基本上可以实现动态链接切换中英文了,不过,还有个小问题,需要解决,那就是,需要点击2下中文才能切换到中文,

英文同样也是,这个问题怎么解决呢?

 

8.想解决那个问题其实很简单,配置一个fitler拦截器就行了,这个拦截器最好配置在struts2的拦截器前面,

拦截器的内容大概是:

Java代码
  1. String local=arg0.getParameter("local");
  2. if(local!=null){
  3. String loc[]=local.split("_");
  4. Locale locale=new Locale(loc[0],loc[1]);
  5. ((HttpServletRequest)arg0).getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);
  6. }
  7. arg2.doFilter(arg0, arg1);
String local=arg0.getParameter("local");
if(local!=null){
	String loc[]=local.split("_");
	Locale locale=new Locale(loc[0],loc[1]);
	((HttpServletRequest)arg0).getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);
}
arg2.doFilter(arg0, arg1);

这样就能实现动态切换中英文了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值