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

  • LOCALE de-mystified...

    After a really unsuccessful attempt to pull reasonable information about LocaleResolvers and how to manage LOCALE changes within a web app I spent the day trying numerous things. First off I found that the "Country" sample provided in the distrbution was not going to be useful for my project.

    I proceeded with the basic steps of creating a controller and and then adding an interceptor for locale changes. this is what my app (myapp) servlet xml file looks like.
    Code:
    <beans>
        <bean id="myappController" class="com.myapp.MyappController"/>
    
        <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
           <property name="paramName"><value>siteLanguage</value></property>
        </bean> 
        
        <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="interceptors">
               <list>
                   <ref local="localeChangeInterceptor"/>
               </list>
            </property> 
            <property name="mappings">
                <props>
                    <prop key="/servlet/MyController">myappController</prop>
                </props>
            </property>
        </bean>
        <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/> 
    </beans>
    This gave me the basis of being able to dynamically manage locale and language files by using the siteLanguage URI parameter. (http://localhost:8080/myapp/servlet/...Language=fr_CA)

    finally within my MyappController.java file I was able to retrieve my proper ResourceBundle by using the RequestContextUtils class to pull the LOCALE as set by the interceptor.
    Code:
    ResourceBundle myBundle = ResourceBundle.getBundle&#40;"myapp", RequestContextUtils.getLocale&#40;request&#41;&#41;;
    Through this method visitors could select their locale on our website for the duration of thier session. I have not tried the "Country" sample method as I found this way to be alot simpler and only required 1 line of code within my controller to load the correct locale properties file.
Working...
X