displaytag的国际化方法
在做国际化时,由于资源文件分模块使用,所以用displaytag提供的I18nStrutsAdapter类不能实现国家化,需要自己重新编写一个I18nStrutsAdapter类。
1、默认国际化资源文件:
首先需要一个displaytag.properties文件,放在class文件夹下,
在displaytag的类包里中有,org.displaytag.properties.TableTag.properties.displaytag.properties
在此文件中找到locale.provider=org.displaytag.localization.I18nJstlAdapter,
改成locale.provider=org.displaytag.localization.I18nStrutsAdapter,表示为struts的国际化
<display:column property="uname" titleKey="userdata.userInfo.userName"/>
package test.common.extension;import java.util.Locale;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpSession;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.Tag;import org.apache.struts.Globals;import org.apache.struts.taglib.TagUtils;import org.displaytag.localization.I18nResourceProvider;import org.displaytag.localization.LocaleResolver;public class I18nTestStrutsAdapter implements I18nResourceProvider, LocaleResolver { public static final String UNDEFINED_KEY = ???; //$NON-NLS-1$ public Locale resolveLocale(HttpServletRequest request) { Locale userLocale = null; HttpSession session = request.getSession(false); if (session != null) { userLocale = (Locale) session.getAttribute(Globals.LOCALE_KEY); } if (userLocale == null) { userLocale = request.getLocale(); } return userLocale; } public String getResource(String resourceKey, String defaultValue, Tag tag, PageContext pageContext) { String titlkey = (resourceKey != null) ? resourceKey : defaultValue; String[] temp = titlkey.split("#"); String bundle = temp[0]; String key = temp[1]; String title = null; try { //从指定的Resource Bundle(资源文件)中返回一条消息文本,参数locale指定Locale,参数key指定消息key title = TagUtils.getInstance().message(pageContext, bundle, Globals.LOCALE_KEY, key); } catch (JspException e) { e.printStackTrace(); } if (title == null && resourceKey != null) { title = UNDEFINED_KEY + resourceKey + UNDEFINED_KEY; } return title; }}
workshop_zh_CN.properties
<display:column property="uname" titleKey="workshop#userdata.userInfo.userName"/>