gpt4 book ai didi

java - Spring 国际化 : How to dynamically set locale value

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:59 26 4
gpt4 key购买 nike

我正在尝试通过spring实现国际化,以下是我做过的配置

`<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="messages">
</property>
</bean>

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />`

现在我有三个属性文件 - message_en.properties、message_fr.properties、message_sp.properties。并在带有 JSTL 标记的 jsp 中使用它。我的问题是如何传递语言环境值以便它可以获取正确的属性文件?一种方法是传入请求 url,但我的应用程序太大,无法在每个 url 中包含此请求参数。还有其他方法可以设置语言环境值吗?我将语言环境值存储在数据库中,我必须从中获取和设置。我怎样才能用最好的方法实现这一目标?

可以使用下面的方法更改语言环境 RequestContextUtils.getLocaleResolver(request).setLocale(request, response, Locale.FRANCE);

但这将需要编写一个过滤器来执行每个请求并在数据库中触发查询以获取值。有更好的方法吗?

最佳答案

我猜您现在可能已经找到了解决方案,但对于 future 遇到这个问题的人来说,也许您需要摆脱 LocaleResolver?

根据 Mkyong :

If you do not register any “localeResolver”, the default AcceptHeaderLocaleResolver will be used, which resolves the locale by checking the accept-language header in the HTTP request.

允许用户的浏览器自动说出它喜欢的语言非常有意义,并且通过更改您的首选语言(也可能是其他浏览器)很容易在 Chrome 中进行测试。

(顺便说一句,Mkyong 的教程非常方便,值得一读)

关于java - Spring 国际化 : How to dynamically set locale value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16810886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com