简体   繁体   English

spring3国际化无法正常工作

[英]spring3 internationalization not working properly

I am newbie to spring.I used spring internationalization in my project.Below is my configuration 我是spring的新手,我在项目中使用了spring国际化,下面是我的配置

<bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="WEB-INF/properties/messages" />
         <property name="defaultEncoding" value="UTF-8" />
    </bean>

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

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


<bean id="handlerMapping"
    class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <ref bean="localeChangeInterceptor" />
    </property>
</bean>

In the properties folder i have three files. 在属性文件夹中,我有三个文件。 messages.properties messages.properties

label.username=user

messages_en.properties messages_en.properties

label.username=hddhd

messages_es.properties messages_es.properties

label.username=lalal

i display the message using <spring:message code="label.username" /> this tag. 我使用<spring:message code="label.username" />此标记显示消息。

But this one always prints the value from messages.properties. 但是,此总是打印来自messages.properties的值。 It always prints user. 它总是打印用户。

even i give the url like below 甚至我给URL如下

[http://localhost:8080/Project/?lang=en
http://localhost:8080/Project/?lang=es][1]

why values not taken from the files messages_en and messages_es 为什么不从messages_en和messages_es文件中获取值

Any help will be greatly appreciated!!! 任何帮助将不胜感激!!!

When using <mvc:annotation-driven /> configuring the DefaultAnnotationHandlerMapping will not work. 使用<mvc:annotation-driven />配置DefaultAnnotationHandlerMapping将不起作用。 You should use the <mvc:interceptors /> element to register the interceptors. 您应该使用<mvc:interceptors />元素注册拦截器。

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>
</mvc:interceptors>

Next to that it doesn't work in newer versions of Spring (everything after 2.5) doesn't use the DefaultAnnotationHandlerMapping anymore in case of <mvc:annotation-driven /> . DefaultAnnotationHandlerMapping ,在<mvc:annotation-driven />情况下,它在Spring的较新版本(2.5版之后的所有版本)中也不再使用DefaultAnnotationHandlerMapping

不要忘记添加这一行

<%@ page contentType="text/html;charset=UTF-8" %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM