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
and using <spring:message/> tags on my JSPs.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>
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:
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).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"; }
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


Reply With Quote
