国际化和语言转换 一些基本代码步骤 我这里就不写了,网上很多
我只说做的时候遇到的一些关键性问题:
1.struts.properties这个资源文件中
struts.custom.i18n.resources=resources.Messages
这条语句的意思是什么? 其实很简单,resources是路径的名字,在项目中的体现是resources包下的message.properties
文件,这个包下不仅是这个文件,还有你的语言包格式为 名称_语言代码_国家代码 如: Messages_zh_CN.properties和Messages_en_US.properties
关于资源文件转码问题推荐一个Eclipse插件叫Arbitrary url为http://propedit.sourceforge.jp/eclipse/updates/
语言切换原理:它的原理为在执行Action方法前,i18n拦截器查找请求中的一个名为"request_locale"的参数。如果其存在,拦截器就将其作为参数实例化Locale对象,并将其设为用户默认的区域(Locale),最后,将此Locale对象保存在session的名为“WW_TRANS_I18N_LOCALE”的属性中 (这里也就是说session中保存的WW_TRANS_I18N_LOCALE这个值决定了这个请求的语言包)
代码演示:
public class SwitchLanguageAction extends BaseAction {
private String referer;
private static Logger logger = Logger.getLogger(SwitchLanguageAction.class);
public String execute() throws Exception {
String request_locale = request.getParameter("request_locale");
if (request_locale.equals("zh_CN"))
request.getSession().setAttribute("WW_TRANS_I18N_LOCALE",
java.util.Locale.CHINA);
if (request_locale.equals("en_US"))
request.getSession().setAttribute("WW_TRANS_I18N_LOCALE",
java.util.Locale.US);
Cookie cookie = new Cookie(LanguageGlobals.USER_SESSION_NAME + "_LANG",
request_locale);
cookie.setMaxAge(31536000);
cookie.setPath("/");
response.addCookie(cookie);
logger.debug(request_locale);
String ref = request.getHeader("referer");
if (null != referer) {
setReferer(ref);
}
return SUCCESS;
}
public String getReferer() {
return referer;
}
public void setReferer(String referer) {
this.referer = referer;
}
}
上面的代码中有个referer属性: 它是记录之前的url 请看我的配置文件
<action name="language" class="com.szc.Action.SwitchLanguageAction">
<result name="success">${referer}</result>
</action>
这里的${referer}用到了动态result,它指定的是action的属性
language.action?request_locale=zh_CN 这是我的页面url 根据之前的经验参数的name 做好起request_locale
为了保持修改语言后 在本台机器上的其他同样浏览器也能起作用 我用到了cookie 作为保存语言的手段 所以我又写了个filter 代码如下:
public class LanguageFilter implements Filter
{
private static Logger logger = Logger.getLogger(LanguageFilter.class);
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response=(HttpServletResponse)arg1;
request.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
String request_locale = request.getParameter("request_locale");
logger.debug("---------request_locale:"+request_locale);
Locale locale = (Locale) session.getAttribute("WW_TRANS_I18N_LOCALE");
if(locale == null)
{
Cookie[] allcookies = request.getCookies();
if(allcookies!=null)
{
for(int i=0;i<allcookies.length;i++)
{
if(allcookies[i].getName().equalsIgnoreCase(LanguageGlobals.USER_SESSION_NAME+"_LANG"))
{
if(allcookies[i].getValue().equalsIgnoreCase("zh_CN"))
locale = Locale.CHINA;
else if(allcookies[i].getValue().equalsIgnoreCase("zh_TW"))
locale = Locale.TAIWAN;
else if(allcookies[i].getValue().equalsIgnoreCase("en_US"))
locale = Locale.US;
}
}
}
}
if(locale==null)
{
locale = request.getLocale();
}
logger.debug(request.getLocale());
request.getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);
logger.debug("-----------language:"+locale);
arg2.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
这段代码 取得local的顺序要注意!! 先session 然后cookie 最后才是request中的本地locale
页面部分:
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/c.tld" prefix="c" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<script src="js/jquery/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
})
</script>
<body>
<a href="language.action?request_locale=zh_CN"><s:text name="index.chinese"/></a> <a href="language.action?request_locale=en_US"><s:text name="index.english"/></a>
<form action="lucene.action" method="post" id="serchSucene" name="serchSucene">
<s:text name="index.keywordAndId"/><input type="text" id="keyword" name="keyword" value="${keyword}"/>
<input type="submit"/>
</form>
<a href="#"> onclik!!</a>
<pg:pager items="${totalRows}" url="lucene.action" index="half-full" maxPageItems="5" maxIndexPages="5" isOffset="<%=false%>" export="pageOffset,currentPageNumber=pageNumber" scope="request">
<pg:param name="pageSize" value="5"/>
<pg:param name="keyword" value="${keyword}"/>
<table align="center">
<tr>
<th>id</th>
<th>CN_N</th>
<th>created_at</th>
</tr>
<c:forEach items="${auctionList}" var="auction">
<tr>
<td >${auction.id }</td>
<td>${auction.cn_n}</td>
<td>${auction.created_at}</td>
</tr>
</c:forEach>
</table>
<pg:index>
<center>
<table border=0 cellpadding=0 width=10% cellspacing=0>
<tr align=center valign=top>
<td valign=bottom>
<pg:prev ifnull="true">
<%if (pageUrl != null) {%>
<td align=right>
<A HREF="<%=pageUrl%>&pageNo=<%=pageNumber%>">
<IMG SRC=http://www.google.com/nav_previous.gif alt="" border=0>
<br>
<b>上一页</b>
</A>
</td>
<%} else {%>
<td>
<IMG SRC=http://www.google.com/nav_first.gif alt="" border=0>
</td>
<%}%>
</pg:prev>
<pg:pages>
<%if (pageNumber == currentPageNumber) {%>
<td>
<IMG SRC=http://www.google.com/nav_current.gif alt="">
<br>
<font color=#A90A08><%= pageNumber %> </font>
</td>
<%} else {%>
<td>
<A HREF="<%=pageUrl%>&pageNo=<%=pageNumber%>">
<IMG SRC=http://www.google.com/nav_page.gif alt="" border=0>
<br><%=pageNumber%></A>
</td>
<%}%>
</pg:pages>
<pg:next ifnull="true">
<%if (pageUrl != null) {%>
<td>
<A HREF="<%=pageUrl%>&pageNo=<%=pageNumber%>">
<IMG SRC=http://www.google.com/nav_next.gif alt="" border=0>
<br>
<b>下一页</b>
</A>
</td>
<%} else {%>
<td>
<IMG SRC=http://www.google.com/nav_last.gif alt="" border=0>
</td>
<%}%>
</pg:next>
</td>
</tr>
</table>
</center>
</pg:index>
</pg:pager>
</body>
</html>
但我项目弄完后 还有一个小问题 就是当cookie中的语言是英文的时候,我重新开个浏览器起访问确是中文的,但我做一个请求处理就变回英文了,我隐约感觉到 它最后还是取的本地locale(也就是request中的locale),如果有知道为什么的,希望告诉我声,有需要我代码的加我qq 365328730