2014-04-13 65 views
0

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

@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

可能重复ofhttp://stackoverflow.com/问题/ 22490558/internationalization-with-spring-mvc?rq = 1 – GreyBeardedGeek

+0

是的。有点像我的问题。但我正在问如何在java中获取消息。当我根据该帖子更改我的xml时,我得到'没有名为'localeChangeInterceptor'的bean被定义为'exception'。还有这个错误:'Class'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping'被标记为'已弃用' – demlik

回答

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>