2

我有一些不一致的地方,我需要一些专家的建议。我正在使用 Spring MVC 3.2

有这个豆子:

<bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="cacheSeconds" value="10" /> <!-- for easier development -->
</bean>

<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.CookieLocaleResolver">
    <property name="defaultLocale" value="en"/>
</bean>

还有两个不同的消息文件:

messages.properties
messages_es.properties

使用 spring 标记时,语言环境解析为“es”并按预期使用 messages_es(这是我的系统语言)。

但是,如果我尝试以编程方式获取语言环境,我总是会得到“en”。我试过这个选项:

选项1:

@Autowired
private LocaleResolver localeResolver;
....
localeResolver.resolveLocale(request);

选项 2:

LocaleContextHolder.getLocale();

选项 3:

RequestContextUtils.getLocale(request);

所有这些都以“en” lang 产生。

问题是,如何获取 spring:message 标签使用的语言环境?

根据文档,

当一个请求进来时,DispatcherServlet 会寻找一个区域设置解析器,如果它找到一个,它会尝试使用它来设置区域设置。使用 RequestContext.getLocale() 方法,您始终可以检索由语言环境解析器解析的语言环境。

但是我在最终 html 中的所有文本都在“es”中,这个方法返回“en”

4

3 回答 3

1

好吧,我会自己回答。我对其进行了足够深入的调试,以发现问题是 ReloadableResourceBundleMessageSource 定义中缺少一个属性。添加 fallbackToSystemLocale = false 并在 localeResolver 中删除默认语言环境现在语言环境解析为请求中的“es”。

到目前为止,一切都很好。遗憾的是,通过“?lang=en”参数更改语言环境的部分不起作用,但那是另一回事。

解析度:

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="cacheSeconds" value="2" />
    <property name="fallbackToSystemLocale" value="false" />
</bean>

<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.CookieLocaleResolver" />

LocaleContextHolder.getLocale();
于 2013-09-17T23:41:43.750 回答
1

通常,可以使用RequestContext.getLocale()( reference ) 以编程方式访问语言环境。

但是,在控制器的@RequestMapping处理程序方法中,您可以依靠 Spring 来获取 Locale。由于Locale受支持的参数类型,因此只需将类型变量声明Locale为控制器方法的参数即可让您访问当前语言环境。

拦截器
在所示配置中,语言环境拦截器被声明为 bean,但未注册为拦截器。试试这个,来自Spring 文档

<bean id="handlerMapping"
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="localeChangeInterceptor"/>
        </list>
    </property>
</bean>

或者,从 Spring 3.0 开始,您可以使用 mvc:interceptors

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptors>
于 2013-09-16T22:19:26.857 回答
0

消息标签使用RequestContext来检索MessageSource和当前Locale。这可以在 MessageTag 类的 resolveMessage 方法中看到:

return messageSource.getMessage(this.message, getRequestContext().getLocale());

见:https ://github.com/spring-projects/spring-framework/blob/master/spring-webmvc/src/main/java/org/springframework/web/servlet/tags/MessageTag.java

于 2013-09-16T17:20:56.800 回答