Results 1 to 7 of 7

Thread: LocaleChangeInterceptor configuration problem

  1. #1
    Join Date
    Sep 2010
    Posts
    7

    Question LocaleChangeInterceptor configuration problem

    Hello everyone,

    I'm building an application with the initial configuration based on the booking-faces.

    I managed to configure the resource bundle correctly. The problem is: no matter what I do the LocaleChangeInterceptor doesn't intercept my request, or at least my debugger doesn't show it, and indeed the locale doesn't change.

    This the relevant bit of my configuration:

    ../resources/messages.properties
    ../resources/messages_pt.properties

    webmvc-config.xml:
    Code:
    <bean id="msgSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"	p:basename="classpath:messages"/>
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
    faces-config.xml: (where MySpringBeanFacesELResolver is exactly as ivats.)
    Code:
    <application>
        <el-resolver>pt.m24.portal.utils.MySpringBeanFacesELResolver</el-resolver>
        <locale-config>
    	  <default-locale>en</default-locale>
    	  <supported-locale>pt</supported-locale>
        </locale-config>
        <resource-bundle>
    	  <base-name>messages</base-name>
    	  <var>msg</var>
        </resource-bundle>
    </application>

    main.xhtml:
    (the bit of the file where I want to change locale)

    Code:
    <h:outputLink id="enLocaleUrl" value="/app/welcome" >
         <f:param name="locale" value="" />
         <h:outputLabel value="#{msgSource.locale_english}"/>
    </h:outputLink>
    <h:outputLink id="ptLocaleUrl" value="/app/welcome" >
          <f:param name="locale" value="pt" />
          <h:outputLabel value="#{msgSource.locale_portuguese}"/>
    </h:outputLink>
    Can anyone please point me in the right direction.
    Thank you in advance.

  2. #2
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    Just declaring the LocaleChangeInterceptor class as a Spring bean won't do. You need to tell Spring that this bean IS an interceptor; for this use the mvc:interceptors command contained in the mvc namespace:

    Code:
      <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
      </mvc:interceptors>

  3. #3
    Join Date
    Sep 2010
    Posts
    7

    Default

    Thank you for your reply Enrico, I just realized my post is incomplete, I already tried to use:

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

    Code:
    <mvc:interceptors>
        	<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    </mvc:interceptors>
    without success, my request still isn't intercepted. Yesterday I went back on the booking-faces original configuration and tried to do it from the beginning
    and no luck still.

  4. #4
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    does your locale change request's query string include a "locale" parameter?

    http://myserver:myport/mypage?locale=it&otherparameters...

    If not, then it's normal that the interceptor will do nothing. If you pass the locale parameter with another name, say "lang", you have to configure the property "paramName" in the bean definition:

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

  5. #5
    Join Date
    Sep 2010
    Posts
    7

    Default

    Yes, the query produced by:

    Code:
    <h:outputLink id="enLocaleUrl" value="/app/welcome" >
         <f:param name="locale" value="" />
         <h:outputLabel value="#{msgSource.locale_english}"/>
    </h:outputLink>
    <h:outputLink id="ptLocaleUrl" value="/app/welcome" >
          <f:param name="locale" value="pt" />
          <h:outputLabel value="#{msgSource.locale_portuguese}"/>
    </h:outputLink>
    ends with ?locale=pt or ?locale=en

    I already tried to enforce the paramName with:
    Code:
    <mvc:interceptors>
            <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" >
                <property name="paramName" value="locale" />
            </bean>
    </mvc:interceptors>
    and still no result, I don't understand this behavior.

  6. #6
    Join Date
    Sep 2010
    Posts
    7

    Thumbs up

    I've found the problem, because the booking-faces uses FlowHandlerMapping the interceptor must be explicitly declared in it, something like this:

    Code:
    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
            <property name="flowRegistry" ref="flowRegistry" />
            <property name="defaultHandler">
                <!-- If no flow match, map path to a view to render; e.g. the "/intro" path would map to the view named "intro" -->
                <bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
            </property>
            <property name="interceptors">
                <list>
                  <ref bean="localeChangeInterceptor" />
                </list>
              </property>
    </bean>
    Thank you for your help.

  7. #7
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    ha-ha...you never mentioned you were using Spring Web Flow (though the fact that it was a faces application should have prompted me to think of it, since Spring web mvc and faces don't go very well together unless through Web Flow), or I would have pointed you to the right solution sooner.

    Well, hopefully this thread will also help other people with the same problem.

    Best regards,

    Enrico

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •