在 Spring 框架中访问正确的资源包

发布于 2024-10-19 10:03:35 字数 640 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×纯※雪 2024-10-26 10:03:35

达成了问题的解决方案。

继续我的问题,当 Spring Framework 遇到像 "#{resourceBundle['jboss.version.error']}" 这样的 JSTL 表达式时,默认情况下它会在类路径中查找 message.properties 文件,除非资源包是明确定义的。

当尝试获取正确的资源包时,框架会查看它设置的区域设置。由于在我的情况下未设置 Spring 框架的区域设置,因此它没有为我获取预期的资源包。在可用选项中,我选择了 Spring LocaleResolver,

我修改了应用程序中现有的 JSF Custom ViewHandler,其中添加了代码来设置 Spring Framework 的区域设置。

public Locale calculateLocale(FacesContext arg0) {

HttpServletRequest request  = (HttpServletRequest)arg0.getExternalContext().getRequest();
        HttpServletResponse response  = (HttpServletResponse)arg0.getExternalContext().getResponse();

LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);

localeResolver.setLocale(request, response, **setYourLocaleHere**);

}

故事并没有在这里结束,以这种方式在语言环境解析器中设置语言环境会引发错误:

Cannot change HTTP accept header – use a different locale resolution strategy

请参阅 无法更改 HTTP 接受标头错误

要克服这一问题,应该将其包含

<bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

在 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.

public Locale calculateLocale(FacesContext arg0) {

HttpServletRequest request  = (HttpServletRequest)arg0.getExternalContext().getRequest();
        HttpServletResponse response  = (HttpServletResponse)arg0.getExternalContext().getResponse();

LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);

localeResolver.setLocale(request, response, **setYourLocaleHere**);

}

The story just doesn't end here, setting the locale in locale resolver this way would throw the error:

Cannot change HTTP accept header – use a different locale resolution strategy

Refer Cannot change HTTP accept header error

To overcome this, one should include

<bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

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.

岁月染过的梦 2024-10-26 10:03:35

您可以按照概述的多种方式执行此操作 此处在文档中。

You could do this multiple ways as outlined here in the doc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文