如何使spring-mvc应用程序多语言?

时间:2014-04-13 13:05:04

标签: spring spring-mvc internationalization

我正在使用spring-mvc 4进行Web服务,我必须将其设为multilanguage。像这样:如果来的网址请求包含土耳其语'..?lang=tr'和英语'..?lang=en'。我读了<mvc:annotation-driven />覆盖LocaleChangeInterceptor的stackoverflow。但当我删除它时,应用程序无法正常工作。我正在使用this tutorial。我还没找到解决方案。对于下面的配置,即使我切换语言,消息也总是英文。此外,我需要在java而不是jsp中获取lang消息。但是当我在syso中时,它总是在java类中返回默认消息。我搞砸了。并寻求你的帮助。

@Autowired
private MessageSource messageSource;

Locale locale = LocaleContextHolder.getLocale();
String msg = messageSource.getMessage("deneme", null, "Deault Message!", locale);

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jee="http://www.springframework.org/schema/jee" 
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.1.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
            http://www.springframework.org/schema/jee
            http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">

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

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

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

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

      <!-- Defining which view resolver to use -->
     <bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" > 
          <property name="prefix" value="/WEB-INF/views/" /> 
          <property name="suffix" value=".jsp" /> 
     </bean>
</beans>

1 个答案:

答案 0 :(得分:0)

将它添加到xml中以注册localChangeInterceptor,它将起作用

<mvc:annotation-driven/>
    <context:component-scan
        base-package="controller">
    </context:component-scan> 

    <bean id="handlerMapping"
            class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">

        </bean>