struts2国际化,不经过action的jsp页面如何国际化

licip 2011-11-10 12:11:32
struts2中项目中的国际化切换是通过设置session中WW_TRANS_I18N_LOCALE值来设定的,因为根据浏览器的语言不能满足需求,还需要可以手动切换。
现在的问题是:某些的jsp页面是直接调用的,不经过struts2的action,这时的国际化就出问题,是浏览器的语言,而不是用户设置的。

各位有什么好的方法解决这个呢?

...全文
637 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ShineQiu 2012-12-28
  • 打赏
  • 举报
回复
用jstl标签库呗,很好用的
fainfy 2011-11-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 licip 的回复:]

这是一种方式,但不是我想要的那种。看来是必须改原码引用 1 楼 fainfy 的回复:
Java code

@StrutsTag(name="i18n", tldTagClass="org.apache.struts2.views.jsp.I18nTag", description="Get a resource bundle" +
" and place it on the valu……
[/Quote]

为什么要改源码?
你自己扩展一个属于你的I18n标签就行了。
也可以在过滤器里面把ActionContext修改成,Session里面的Locale。这个方式我认识不是一个很好的解决方式,因为可能会引发一些其它不便的问题。
licip 2011-11-15
  • 打赏
  • 举报
回复
这是一种方式,但不是我想要的那种。看来是必须改原码[Quote=引用 1 楼 fainfy 的回复:]
Java code

@StrutsTag(name="i18n", tldTagClass="org.apache.struts2.views.jsp.I18nTag", description="Get a resource bundle" +
" and place it on the value stack")
public class I18n e……
[/Quote]
fainfy 2011-11-10
  • 打赏
  • 举报
回复

@StrutsTag(name="i18n", tldTagClass="org.apache.struts2.views.jsp.I18nTag", description="Get a resource bundle" +
" and place it on the value stack")
public class I18n extends Component {

private static final Logger LOG = LoggerFactory.getLogger(I18n.class);

protected boolean pushed;
protected String name;
protected Container container;
private TextProvider textProvider;

public I18n(ValueStack stack) {
super(stack);
}

@Inject
public void setContainer(Container container) {
this.container = container;
}

public boolean start(Writer writer) {
boolean result = super.start(writer);

try {
String name = this.findString(this.name, "name", "Resource bundle name is required. Example: foo or foo_en");
ResourceBundle bundle = (ResourceBundle) findValue("getTexts('" + name + "')");

if (bundle == null) {
bundle = LocalizedTextUtil.findResourceBundle(name, (Locale) getStack().getContext().get(ActionContext.LOCALE));
}

if (bundle != null) {
final Locale locale = (Locale) getStack().getContext().get(ActionContext.LOCALE);
TextProviderFactory tpf = new TextProviderFactory();
container.inject(tpf);
textProvider = tpf.createInstance(bundle, new LocaleProvider() {
public Locale getLocale() {
return locale;
}
});
getStack().push(textProvider);
pushed = true;
}
} catch (Exception e) {
String msg = "Could not find the bundle " + name;
throw new StrutsException(msg, e);
}

return result;
}

public boolean end(Writer writer, String body) throws StrutsException {
if (pushed) {
Object o = getStack().pop();
if ((o == null) || (!o.equals(textProvider))) {
LOG.error("A closing i18n tag attempted to pop its own TextProvider from the top of the ValueStack but popped an unexpected object ("+(o != null ? o.getClass() : "null")+"). " +
"Refactor the page within the i18n tags to ensure no objects are pushed onto the ValueStack without popping them prior to the closing tag. " +
"If you see this message it's likely that the i18n's TextProvider is still on the stack and will continue to provide message resources after the closing tag.");
throw new StrutsException("A closing i18n tag attempted to pop its TextProvider from the top of the ValueStack but popped an unexpected object ("+(o != null ? o.getClass() : "null")+")");
}
}

return super.end(writer, body);
}

@StrutsTagAttribute(description="Name of resource bundle to use (eg foo/bar/customBundle)", required=true, defaultValue="String")
public void setName(String name) {
this.name = name;
}
}


bundle = LocalizedTextUtil.findResourceBundle(name, (Locale) getStack().getContext().get(ActionContext.LOCALE));

自己重写一个I18n的标签,设置Locale不就行了?

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧