如何从 Spring 获取当前用户语言环境而不将其作为参数传递给函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10792551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:08:21  来源:igfitidea点击:

How to obtain a current user locale from Spring without passing it as a parameter to functions?

springspring-mvc

提问by ams

I am using Spring 3.1 and want to find the locale active for the current user is there a way to grab the locale directly without being forced to pass it from the controller to the service layer ... etc.

我正在使用 Spring 3.1 并希望找到当前用户的活动区域设置,有没有办法直接获取区域设置,而不必被迫将其从控制器传递到服务层……等等。

does spring store the locale in a thread local storage where it can be grabbed by calling some static method?

spring 是否将语言环境存储在可以通过调用一些静态方法获取的线程本地存储中?

回答by Sam Routledge

I was also looking for how to access the locale without passing the Localearound, but I'm still pretty new to Spring and found most solutions to be confusing. It turns out, though, that Spring MVC does store the locale in thread local storage. It can be accessed by:

我也在寻找如何在不绕过的情况下访问语言环境Locale,但我对 Spring 仍然很陌生,发现大多数解决方案都令人困惑。但事实证明,Spring MVC 确实将语言环境存储在线程本地存储中。可以通过以下方式访问:

Locale locale = LocaleContextHolder.getLocale();

The last approach [...] is based on a thread local in order to provide the current locale in any entity of your architecture. [...] You must be aware that the content of the LocaleContextHolder corresponds by default to the locale specified within the Web request.

最后一种方法 [...] 基于线程本地,以便在架构的任何实体中提供当前语言环境。[...] 您必须知道 LocaleContextHolder 的内容默认对应于 Web 请求中指定的区域设置。

From: Configuring locale switching with Spring MVC 3. (That post also offers alternative configurations/methods for getting the locale, which might be useful for anyone looking to do this).

来自:使用 Spring MVC 3 配置语言环境切换。(该帖子还提供了用于获取语言环境的替代配置/方法,这对于希望执行此操作的任何人都可能有用)。

You can also view the LocaleContextHolderdocs from Spring here.

您还可以在此处查看LocaleContextHolderSpring的文档。

回答by Japan Trivedi

It depends on where you have configured to store the locale, in session or in cookie?

这取决于您配置存储区域设置的位置,在会话中还是在 cookie 中?

In my application I have configured to store the user locale in its session with below mentioned configuration.

在我的应用程序中,我已配置为使用以下提到的配置将用户语言环境存储在其会话中。

    <mvc:interceptors>
        <ref bean="localeChangeInterceptor"/>
    </mvc:interceptors>

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang"/>
    </bean>

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

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
        <list>
            <value>/WEB-INF/i18n/labels</value>
            <value>/WEB-INF/i18n/messages</value>
            <value>/WEB-INF/i18n/include</value>
        </list>
        </property>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>

If you have done somthing like this then you can easily retrieve the locale parameter from the session.

如果你做了这样的事情,那么你可以轻松地从会话中检索语言环境参数。

Hope this helps you.

希望这对你有帮助。

Cheers.

干杯。

回答by Erikas Zuzevicius

We had the same need, so came up with putting Localeinto ThreadLocalcontext. We already had an object (some user information) stored in ThreadLocalcontext, so just appended it (looks like a hack, but the quickest solution we found).

我们有同样的需要,所以有把想出了LocaleThreadLocal上下文。我们已经在ThreadLocal上下文中存储了一个对象(一些用户信息),所以只需附加它(看起来像一个黑客,但我们找到的最快的解决方案)。