3

So I've been following a mix of the following tutorials ....

http://viralpatel.net/blogs/spring-3-mvc-internationalization-i18n-localization-tutorial-example/

http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/

THE IDEA!

Make it so our webpage can display multiple languages by clicking on a specific language and having the webpage update accordingly

THE PLAN!

Using spring's LocaleChangeInterceptor, LocaleResolver, HandlerMapping, and MessageSource, and some messages_xx.properties files, we are dynamically calling messages from different languages depending on the specified "lang" parameter. We call the messages using For example, if "lang"=en, we will go to the messages_en.properties file and get the message for label.message.

THE SETUP TO PROBLEM!

We have set up all of the spring "stuff" (note I am an amateur at this stuff) in the applicationContext.xml file and web.xml file. We have also added the in one of our .jsp files.

THE PROBLEM!

When we run the application (using a TomCat server) it successfully displays the message in the messages_en.properties file (In this case "hello"), but, when we attempt to change the language, the same message appears.

REFERENCE CODE

applicationContext.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:ctx="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd ">


<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> 

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

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

Example: messages_en.properties

 label.message = Hello

jsp snippets for displaying message / changing language

<%@ include file="/include.jsp"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
..............
<a href="?lang=en">English</a>|<a href="?lang=ar">Arabic</a>
<h3>
<spring:message code="label.message" text="default text" />
</h3>
..............

NOTES

  • I threw in a line ${pageContext.response.locale} into the code and the output on the webpage was "en_US". Even after changing the parameter (where in the url it would display .....lang=ar for example)

  • It does find the messages_en.properties file and gets the message from that file and prints it to the webpage, but only accesses that file and only displays the corresponding message.

  • We are using spring v3.0.3 (edit: based off of the library files but it says 2.5 in the xml files so I dont know), should we use a different version or library?

WHAT DO I THINK IT IS?

  • I have very very little experience but I have a feeling that the locale variable is not actually being changed, so how would I effectively change that?

Thank you everyone! I really appreciate any and all help!

EDIT

  • I have since surrounded the "localeChangeInterceptor" with <mvc:interceptors>, no luck

  • If you need any more files or code, please ask!

4

2 回答 2

0

尝试这个..

    <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver" p:basename="messages" />

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

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" />
于 2014-03-17T07:04:09.083 回答
0

在这种情况下,可重新加载的资源无济于事。添加一个 mvc 拦截器并通过扩展 HandlerInterceptorAdapter 进行语言更改拦截器

于 2017-09-11T05:20:17.207 回答