Announcement Announcement Module
Collapse
No announcement yet.
Localization Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Localization

    I am having problems setting up the i18n package in my web application.
    I got the part to output messages to jsp using <spring:message code="firstname"/>
    The other part that I'm still stuck on is, I want to create a setting menu where I can define the language for the application. I created two xml files for this purpose, applicationContext.xml has this:
    --------------------------------------------------------------------------------------------------------------------------
    <bean id="messageSource" class="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="alwaysUseMessageFormat" value="true" />
    <property name="basename" value="messages"/>
    </bean>
    --------------------------------------------------------------------------------------------------------------------------
    and another file called applicationContext-web.xml that contains the following:
    --------------------------------------------------------------------------------------------------------------------------
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.Locale ChangeInterceptor">
    <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.Sessio nLocaleResolver" />

    <!-- Map urls to controller -->
    <bean id="beanNameUrlMapping" class="org.springframework.web.servlet.handler.Bea nNameUrlHandlerMapping">
    <property name="interceptors">
    <list>
    <ref bean="localeChangeInterceptor" />
    </list>
    </bean>
    --------------------------------------------------------------------------------------------------------------------------
    I also created bunch of property files and named them messages.properties, messages_de.properties, ...

    I try passing the lang parameter in the URL (?lang=de) hoping for the interceptor to catch it, and automatically change the language, but no success.

    Please tell me what I'm missing .

    Thanks in advance

  • #2
    Are you sure the interceptor is being called? It's not clear from the code you posted whether the url mapper is actually used at all, and you don't show any controllers. Normally we expect to see url mapping in a file called <servletName>-servlet.xml, so I'm just checking that you have a DispatcherServlet somewhere.

    P.S. this is a web post, so putting it on the web forum might be a good idea.

    Comment


    • #3
      David thanks very much for replying.
      I am not using any mapping. My application structure is different from the examples I was seeing. I have bunch of sevlet-dispatcher.xml files. each package has one that contains urlMapping and beans for controllers and submit classes that are under it.

      I was wondering if I can find a way to map the interceptor to all my jsp files.

      Thank you very much
      Last edited by wriachy; Feb 16th, 2007, 01:48 PM.

      Comment


      • #4
        You can define the interceptor in a single place (either in the parent application context, which I take it is what you are doing, or in an import into all of your servlet contexts), but all of your url mappings must have the interceptor as a property.

        Comment


        • #5
          Do I declare this line in every servlet-dispatcher.xml that I have
          <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.Locale ChangeInterceptor"/>

          and then create an interceptor for the urlMapping.

          Thank you very much.

          Comment


          • #6
            Not sure what you mean. This line:

            Originally posted by wriachy View Post
            <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.Locale ChangeInterceptor"/>
            is what *creates* the interceptor. You can do that once if you like (but why does it matter?). Then all your url mappers in all the *-servlet.xml files must refer to the/an interceptor in their interceptors property.

            Comment


            • #7
              Hello David,
              thanks very much for taking time to reply. I finally got this thing working. I did not have to specify any mappings. I used the sessionlocaleresolver and that took care of everything.

              The only thing that was missing was setting it up in the session, and that took care of transforming the language in the whole system to the desired one.

              applicationContext.xml

              Code:
              <bean id="localeResolver" class="org.springframework.web.servlet.i18n.Sessio nLocaleResolver" />
              
              <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.Locale ChangeInterceptor"/>
              <!--
              - Explicit URL handler mapping instead of default BeanNameUrlHandlerMapping.
              -->
              <bean id="urlMapping" class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
              <property name="interceptors">
              <list>
              <ref bean="localeChangeInterceptor"/>
              </list>
              </property>
              </bean>

              That was the missing part:
              request.getSession().setAttribute(SessionLocaleRes olver.LOCALE_SESSION_ATTRIBUTE_NAME, Locale.GERMANY);


              Thanks again.

              Comment

              Working...
              X