Results 1 to 4 of 4

Thread: Controller, i18n and Locale

Threaded View

  1. #1

    Default Controller, i18n and Locale [SOLVED]

    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 at 11:08 AM.

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
  •