在 Spring 框架中访问正确的资源包
我正在尝试使用 Spring 框架(WebFlow)访问资源包。 messages.properties 文件和相应的 messages_ar_AE.properties 文件保存在 Spring 框架访问资源包的类路径中。
使用 JSTL ResourceBundle 属性从 xhtml 文件调用代码。
<myCustom:includedInSetValidator set="5.0, 5.0.1, 5.1"
validationMessage="#{resourceBundle['jboss.version.error']}" />
但无论语言环境如何,"#{resourceBundle['jboss.version.error']}"
始终获取默认文本,即;来自英语;
正如我从一些论坛了解到的那样,我得到一个提示,我需要使用 LocaleChangeInterceptor 或其他一些预定义的类来处理这个问题。设置 Spring Locale 后,将默认加载正确的资源包,从而解决我的问题。
我需要一种以编程方式更改 Spring Framework 区域设置的方法来设置区域设置。我如何以编程方式实现这一目标?
谢谢。
I am trying to access a resource bundle using Spring framework (WebFlow). A messages.properties file and accordingly messages_ar_AE.properties file are kept in the classpath from where the Spring Framework access the resource bundle.
The code in invoked from a xhtml file using the JSTL resourceBundle attribute.
<myCustom:includedInSetValidator set="5.0, 5.0.1, 5.1"
validationMessage="#{resourceBundle['jboss.version.error']}" />
But irrespective of locale, the "#{resourceBundle['jboss.version.error']}"
always fetches the default text, i.e; from English;
As I learned from some forums I got an hint that I need to handle this using LocaleChangeInterceptor or some other predefined classes. Once the Spring Locale is set, the proper resource bundle will be loaded by default, and hence solving my problem.
I need a way to change the Spring Framework Locale programatically to set the Locale. How do I achieve this programatically ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
达成了问题的解决方案。
继续我的问题,当 Spring Framework 遇到像
"#{resourceBundle['jboss.version.error']}"
这样的 JSTL 表达式时,默认情况下它会在类路径中查找 message.properties 文件,除非资源包是明确定义的。当尝试获取正确的资源包时,框架会查看它设置的区域设置。由于在我的情况下未设置 Spring 框架的区域设置,因此它没有为我获取预期的资源包。在可用选项中,我选择了 Spring LocaleResolver,
我修改了应用程序中现有的 JSF Custom ViewHandler,其中添加了代码来设置 Spring Framework 的区域设置。
故事并没有在这里结束,以这种方式在语言环境解析器中设置语言环境会引发错误:
请参阅 无法更改 HTTP 接受标头错误
要克服这一问题,应该将其包含
在 Spring 配置文件中。
现在 Spring 框架所需的语言环境已设置。
可能有比我所做的更好的解决方案。如果有的话,人们也可以提出他们的解决方案。
谢谢。
Reached the solution for the problem.
Continuing from my question, when Spring Framework encounters a JSTL expression like
"#{resourceBundle['jboss.version.error']}"
by default it looks for message.properties file in the classpath, unless a resource bundle is defined explicitly.When trying to fetch the proper resource bundle, the framework looks at the at the locale it is set to. As the locale of Spring Framework was not set in my case, it was not fetching me the expected resource bundle. Out of available options i chose Spring LocaleResolver
I modified existing JSF Custom ViewHandler in my application, where I added code to set the locale of Spring Framework.
The story just doesn't end here, setting the locale in locale resolver this way would throw the error:
Refer Cannot change HTTP accept header error
To overcome this, one should include
in the Spring configuration file.
And now the desired locale of the Spring Framework is set.
There could possibly a better solution than what I did. One can also suggest their solutions if any.
Thanks.
您可以按照概述的多种方式执行此操作 此处在文档中。
You could do this multiple ways as outlined here in the doc.