Announcement Announcement Module
Collapse
No announcement yet.
[PROBLEM] Spring Internationalization Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • [PROBLEM] Spring Internationalization

    Hi
    I'm making a simple aplication to do some login to administrative pages and add some internationalization.
    I'm reading from several tutorials how to do that (internationalization) but i'm unable to load the messages from file.

    In attach is my project without libs. I'm using most recent spring framework and spring security libs and jstl 1.2.

    Can anyone help me on internationalization?

  • #2
    Using property files and interceptor

    Here's how we're doing localization:

    in the xml config:
    Code:
    	<!-- declare the interceptor -->
    	<bean id="localeChangeInterceptor"
    		class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    
    	<!-- declare the resolver -->
    	<bean id="localeResolver"
    		class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
    
    <bean id="urlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    		<property name="interceptors">
    			<list>
    				<!-- adding locale interceptor bean -->
    				<ref local="localeChangeInterceptor" />
    			</list>
    		</property>
    	</bean>
    Then we created a custom tag lib for our JSPs which extend org.springframework.web.servlet.tags.RequestContex tAwareTag and have a key to a property file as the parameter.

    We then pull property files from our res source folder (getting the local from the request and selecting the proper package structure from that) in that class and replace the tags with the appropriate text (english or spanish) using the key.

    We've set up our tag library so that we can set variables with the value in the property file and then use that variable as ${variableName} in fields like title, for the tooltip to be localized.

    There may be an easier way, but this approach has been very dynamic and we've yet to run into a problem we can't fix.

    Comment


    • #3
      Actually, there's an even easier solution.

      Set up the LocaleResolver and LocaleChangeInterceptor as you described above, but also define a Spring MessageSource like so:

      Code:
      <bean id="messageSource" 
               class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
          <property name="basenames">
              <list>
                  <value>messages/errors</value>
                  <value>messages/global</value>
                  <!-- etc -->
              </list>
          </property>
      </bean>
      and the files with the appropriate suffixes (messages/errors_es.properties, messages/errors_fr.properties) will be loaded and referenced without any other effort on your part. This integrates seamlessly with the JSTL fmt:message tag as well, so you can use the "standard" approach to access your internationalized strings.

      Code:
      <fmt:message key="error.panic.now"/>
      Hope this helps
      - Don

      Comment

      Working...
      X