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"/>


2、分模块多个国际化资源文件:

同样需要一个displaytag.properties文件,

改成locale.provider=test.common.extension.I18nTestStrutsAdapter (自定义的一个类)


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"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值