I've setup Spring as specified in the following guide: http://www.springbyexample.org/examples/basic-webapp-internationalization-spring-config.html
If I was to append ?locale=fr, for example, to the end of an URL the locale will change to French.
However, in my case, I want to set the locale when the user logs in as this information is associated with their profile. I've tried to use localeResolver.setLocale(request, response, new Locale("fr")) (where localeResolver is an instance of SessionLocaleResolver) to specify the locale however this doesn't have any effect.
开发者_如何学JAVAAny idea what I'm doing wrong? Am I approaching this issue in the correct way?
localeResolver.setLocale works fine for me, try something like this:
applicationContext
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"
p:basename="messages/messages" p:fallbackToSystemLocale="false" />
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" />
my_page.jsp
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<html>
<body>
<p><spring:message code="my.message"/></p>
</body>
</html>
\src\main\resources\messages\messages.properties
my.message=Message (default language)
\src\main\resources\messages\messages_en.properties
my.message=Message in English
\src\main\resources\messages\messages_fr.properties
my.message=Message in French
Controller
@Controller
@RequestMapping("/")
public class SampleController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome(HttpServletRequest request, HttpServletResponse response) {
LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
localeResolver.setLocale(request, response, StringUtils.parseLocaleString("fr"));
return "my_page";
}
}
With this code I get "Message in French", if I change "fr" to "en" I get "Message in English", and without setLocale call I get "Message (default language)". Changing StringUtils.parseLocaleString("fr") to new Locale("fr") gives the same results.
I would recommend try to set up default locale as:
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="fr_FR" />
</bean>
Some helpful info is in blog post Configuring locale switching with Spring MVC 3.
Example:
@Configuration
public class i18nConfiguration extends WebMvcConfigurerAdapter {
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
Locale locale = new Locale("fr", "FR");
sessionLocaleResolver.setDefaultLocale(locale);
return sessionLocaleResolver;
}
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
Locale locale = new Locale("tr", "TR");
sessionLocaleResolver.setDefaultLocale(locale);
return sessionLocaleResolver;
}
You can probably take a look at Spring Roo project. There is an internationalization add on from Spring which is being used in Spring Roo which enables quick switching of Locale within a Spring Web application automatically generated from Roo.
How do you determine that the locale has not been set? If you expect, the correct locale to be present in HttpServletRequest
, this isn't true — its value is handled by the servlet container and is therefore immutable. Instead, you should rely that Spring will inject a proper value to a method parameter with class Locale
in your controller. Another way to obtain the locale is by using RequestContextUtils.getLocale(HttpServletRequest request)
directly.
精彩评论