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}" />
希望对您有用。