Announcement Announcement Module
Collapse
No announcement yet.
Controller, i18n and Locale Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Controller, i18n and Locale

    Hello,

    I'm building a web app with Spring MVC which needs i18n.

    So far I have successfully configured Bundle Messages for static view string like this:

    web-servlet.xml
    Code:
    <bean id="messageSource"
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource">	    
    	    <property name="basename" value="classpath:messages" />
    	    <property name="defaultEncoding" value="UTF-8"/> 
    	    <property name="cacheSeconds"><value>60</value></property>
    		<property name="useCodeAsDefaultMessage" value="false"/>
    </bean>
    
    <bean id="localeChangeInterceptor"
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="language" />
        </bean>
    	
    <bean id="localeResolver"
            class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    		<!--        
    		<property name="cookieName" value="language" />
    		<property name="cookieMaxAge" value="3600" /> -->
            <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>
    and using <spring:message/> tags on my JSPs.

    But I also need to translate data on domain tables, to be used in forms. For example, a Countries table, which contains a list of countries for every language supported, is needed in the New User form for its address.

    To query the database, I need the current Locale.

    I have tried this:

    Code:
    @RequestMapping(method = RequestMethod.GET) 
        public String setupForm(Model model,Locale locale, HttpServletRequest request, HttpSession session) { 
            Patient patient = new Patient(); 
            model.addAttribute("patient", patient);
            System.out.println("request:"+request.getLocale().getCountry());
            System.out.println("parameter:"locale.getCountry());
            System.out.println("session:"+((Locale)session.getAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME)).getCountry());
            model.addAttribute("countries", userService.getAllCountries(r.getLocale().getCountry()));
            return "patient.form"; 
        }
    request.getLocale().getCountry() always returns "ES" (I'm in Spain), despite having selected any other language (and seeing static JSP texts in that selected language, not in Spanish).

    locale.getCountry() always returns an empty String.

    session.getAttribute().GetCountry() also returns an empty String.

    I need the 2-letter country code of the language selected by the user to query the database.

    Which is the proper way to do this?

    Thanks,

    Nacho
    Last edited by npintos; Jan 26th, 2011, 12:08 PM.

  • #2
    Hi Nacho,

    The RequestContext class will do what you need. Just instantiate an instance (wrapping the Request) if you're in Java. If you're in a view, the ViewResolvers all have a "requestContextAttribute" property which can insert the RC into a model variable with the name you specify.

    Hope this helps
    - Don

    Comment


    • #3
      Hi Don,

      Thank you very much for your quick reply.

      But I had never heard before about Requestcontext, and I'm new to Spring 3 and annotations.

      I tried:

      Code:
      RequestContext rc = new RequestContext(r);
      (inside the same method I posted above, where r is an HttpServletRequest)

      and rc.getLocale().getCountry() also returns an empty String.

      I also tried:
      Code:
      RequestContextUtils.getLocale(r).getCountry()
      and alas, also an empty String.

      I'm executing this in a class annotated with @Controller. Which is the correct way to instantiate this class and get the actual locale?

      Thanks,

      Nacho

      Comment


      • #4
        Ooops my bad!

        I was using getCountry(), and the correct method is locale.getLanguage():

        Code:
        RequestContextUtils.getLocale(r).getLanguage()
        In case anyone needs this.

        Thanks again, Don!!

        Comment

        Working...
        X