Tuesday, July 11, 2006

实践——如何在STRUTS中实现多语言支持

首先,要准备多语言的properties文件,作为application resources。

我的实践是在src目录下创建一个resources目录,JAVA会把它看成是一个package。在这个目录下创建各个properties文件,然后再复制并加上_zh_CN或者_en_US作为各locale所需的resources。注意,需要用PropertiesEditor,一个配合eclipse的插件,来编辑中文文件,还要特别注意在preview后再存,同时覆盖原_zh_CN文件。

在JSP的中加入locale=true,就成了。意思是根据用户的流览器的首选locale来决定页面和session中的locale。在IE的语言选项中可以选择所需的locale以调试。

在struts的struts-conf.xml中定义controller的contentType为text/html; charset="UTF-8"。在每个JSP文件的头加上contentType定义,在每个
html的文件的head中加contenType定义。

%@ page contentType="text/html;charset=UTF-8" %
%@ taglib uri="/WEB-INF/taglib/struts-html.tld" prefix="html" %
%@ taglib uri="/WEB-INF/taglib/struts-bean.tld" prefix="bean" %
%@ taglib uri="/WEB-INF/taglib/struts-tiles.tld" prefix="tiles" %

html:html locale="true"
head
meta http-equiv="Content-Type" content="text/html;charset=UTF-8"
title bean:message bundle="TITLES" key="title.window.default" / /title
html:base/
link rel="stylesheet" type="text/css" href="html:rewrite forward='DefaultStyleSheet'/"
/head

表格中的多语言支持可以用一个Filter来完成,

/*
* Created on 9-Aug-2004
*/
package com.cm.soccer.impl;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
* @author thomas
*
* Filter that sets the character encoding to be used in parsing the
* incoming request, either unconditionally or only if the client did not
* specify a character encoding. Configuration of this filter is based on
* the following initialization parameters:
*
* encoding - The character encoding to be configured
* for this request, either conditionally or unconditionally based on
* the ignore initialization parameter. This parameter
* is required, so there is no default.
* ignore - If set to "true", any character encoding
* specified by the client is ignored, and the value returned by the
* selectEncoding() method is set. If set to "false,
* selectEncoding() is called only if the
* client has not already specified an encoding. By default, this
* parameter is set to "true".
* Although this filter can be used unchanged, it is also easy to
* subclass it and make the selectEncoding() method more
* intelligent about what encoding to choose, based on characteristics of
* the incoming request (such as the values of the Accept-Language
* and User-Agent headers, or a value stashed in the current
* user's session.
*/

public class EncodeFilter implements Filter {

// ----------------- Instance Variables
/**
* The default character encoding to set for requests that pass through
* this filter.
*/
protected String encoding = null;

/**
* The filter configuration object we are associated with. If this value
* is null, this filter instance is not currently configured.
*/
protected FilterConfig filterConfig = null;

/**
* Should a character encoding specified by the client be ignored?
*/
protected boolean ignore = true;

// ----------------- Public Methods
/**
* Place this filter into service.
*
* @param filterConfig The filter configuration object
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}

/**
* Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
* @param request The servlet request we are processing
* @param result The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException {

String encoding = selectEncoding(request);
// One way to ensure response set to be UTF-8 is to wrap the response here.
// But we don't go this way in this implementation.
// We have put contentType setting in every JSPs.
// response =
// new ResponseWrapper((HttpServletResponse) response, encoding);
// Conditionally select and set the character encoding to be used
if (ignore (request.getCharacterEncoding() == null)) {
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}

/**
* Take this filter out of service.
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}

// ----------------- Protected Methods
/**
* Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* null.
* The default implementation unconditionally returns the value configured
* by the encoding initialization parameter for this
* filter.
*
* @param request The servlet request we are processing
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}


在web.xml中定义,

EncodeFilter
com.cm.soccer.impl.EncodeFilter

encoding
UTF-8

ignore
false

EncodeFilter
cmss

No comments: