JSTL fmt:消息和资源包

Smo*_*nel 6 java jstl struts2

我想根据语言环境从资源包中设置我的表的"dir"属性.

这是片段:

        <fmt:setBundle basename="class.path.to.resource.bundle"/>
        <table align=center class="" dir=<fmt:message key="registration.direction"/>>
Run Code Online (Sandbox Code Playgroud)

当页面呈现时我得到了这个:

   <table align=center dir=???registration.direction???>
Run Code Online (Sandbox Code Playgroud)

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

registration.direction = ltr - >英文

registration.direction = rtl - >阿拉伯语

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

谢谢

BR SC

Dav*_*e G 6

两件事情

1)我会添加一个变量来存储消息结果

<fmt:message key="registration.direction" var="direction" />
Run Code Online (Sandbox Code Playgroud)

然后

2)我会用你的代码执行以下操作

  <fmt:setBundle basename="class.path.to.resource.bundle"/>
  <fmt:message key="registration.direction" var="direction" />
  <table align=center class="" dir="${direction}">
Run Code Online (Sandbox Code Playgroud)

现在,就资源包而言,通常您应该为资源包提供以下结构

/foo/bar/MyResourceBundle.properties
/foo/bar/MyResourceBundle_en.properties
/foo/bar/MyResourceBundle_en_US.properties
/foo/bar/MyResourceBundle_<lang>[_COUNTRY[_VAR]].properties
Run Code Online (Sandbox Code Playgroud)

如果您的捆绑包没有以这种方式构建,那可能是您的一些问题.

确保所有预期可用的密钥都在MyResourceBundle中以合理的默认值定义.

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

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

<fmt:setLocale scope="session" value="${sessionScope.WW_TRANS_I18N_LOCALE}" />
Run Code Online (Sandbox Code Playgroud)

希望对你有用.