JSTL fmt:消息和资源包 [英] JSTL fmt:message and resource bundle

查看:115
本文介绍了JSTL fmt:消息和资源包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据区域设置从资源包中设置我的表的dir属性。

I want to set the "dir" property of my table from resource bundle based on the locale.

这是片段:

        <fmt:setBundle basename="class.path.to.resource.bundle"/>
        <table align=center class="" dir=<fmt:message key="registration.direction"/>>

当页面呈现时,我得到:

When the page renders I get this:

   <table align=center dir=???registration.direction???>

我有两个英语和阿拉伯语的资源包。

I have two resource bundles for english and arabic.

registration.direction = ltr - >英语

registration.direction = ltr -> English

registration.direction = rtl - >阿拉伯语

registration.direction = rtl -> Arabic

请告诉我我做错了什么? dir应该有ltr或rtl,具体取决于区域设置。

Please tell what I am doing wrong? The dir should have "ltr" or "rtl" depending on the locale.

谢谢

BR
SC

BR SC

推荐答案

两件事

1)我会添加用于存储消息结果的变量

1) I would add a variable to store the message result in

<fmt:message key="registration.direction" var="direction" />

然后

2)我会做以下代码

  <fmt:setBundle basename="class.path.to.resource.bundle"/>
  <fmt:message key="registration.direction" var="direction" />
  <table align=center class="" dir="${direction}">

现在,就您的资源包而言,您的资源包应该具有以下结构

Now as far as your resource bundles, typically You should have the following structure for your resource bundles

/foo/bar/MyResourceBundle.properties
/foo/bar/MyResourceBundle_en.properties
/foo/bar/MyResourceBundle_en_US.properties
/foo/bar/MyResourceBundle_<lang>[_COUNTRY[_VAR]].properties

如果你的包没有以这种方式构建,可能是你的一些问题。

If your bundle is not structured in this fashion that might be some of your problem.

确保所有预期可用的密钥都在具有合理默认值的MyResourceBundle。

Make sure that all keys that are expected to be available are defined in MyResourceBundle with reasonable defaults.

我正在修改这个答案,因为我不确定我的评论是否在隐藏函数中丢失了。

I'm amending this answer as I'm not sure if my comment got lost in a hide function.

由于您使用的是Struts 2,我的印象是您正在使用i18n拦截器。拦截器将当前区域设置存储在名为WW_TRANS_I18N_LOCALE的sesion变量中。因此,您能够访问它并使用以下内容设置JSTL标记的区域设置:

With the fact that you are using Struts 2, I'm under the impression that you're using the i18n interceptor. The interceptor will store the current locale in the sesion variable named WW_TRANS_I18N_LOCALE. As such you should be able to get to it and set the locale for the JSTL tags by using the following:

<fmt:setLocale scope="session" value="${sessionScope.WW_TRANS_I18N_LOCALE}" />

希望对您有用。

这篇关于JSTL fmt:消息和资源包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆