java Spring 中的 LocaleResolver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4782133/
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-10-30 08:00:09  来源:igfitidea点击:

LocaleResolver in Spring

javaspringspring-mvcinternationalization

提问by Jothi

I am using session locale resolver for my application. I am showing the languages in dropdown. if the user selects on any of the language then repopulates all values from that language.

我正在为我的应用程序使用会话区域设置解析器。我在下拉列表中显示语言。如果用户选择任何一种语言,则重新填充该语言的所有值。

<property name="interceptors">
    <list>
        <ref bean="localeChangeInterceptor" />
    </list>
</property>

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

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

But its is not reading it from the session. Always it considers the defaultlanguage from browser setting. Please help on this.

但它不是从会话中读取它。它始终考虑浏览器设置中的默认语言。请帮忙解决这个问题。

回答by Ralph

You need to obtain the locale in this way:

您需要通过这种方式获取语言环境:

Locale loc=RequestContextUtils.getLocale(request);

回答by optional

In Spring 4.0 we can Also use LocaleContextResolver.getLocale()method as well.

在 Spring 4.0 中,我们也可以使用LocaleContextResolver.getLocale()method。