Spring: language setting

I installed Spring as indicated in the following guide: http://www.springbyexample.org/examples/basic-webapp-internationalization-spring-config.html

If I were to add? locale = fr, for example, at the end of the URL, the language will change to French.

However, in my case, I want to set the locale when the user logs in, since this information is related to their profile. I tried using localeResolver.setLocale (request, response, new Locale ("fr")) (where localeResolver is an instance of SessionLocaleResolver) to indicate the locale, however this has no effect.

Any idea what I'm doing wrong? Am I getting this problem right?

+6
source share
5 answers

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 \ home \ resources \ messages \ messages.properties

my.message = Message (default language)

\ SRC \ main \ resources \ messages \ messages_en.properties

my.message = Post in English

\ SRC \ main \ resources \ messages \ messages_fr.properties

my.message = Post 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";
    }
}

" -", "fr" "en", " ", setLocale " ( )". StringUtils.parseLocaleString( "fr" ) ( "fr" ) .

+13

:

<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
     <property name="defaultLocale" value="fr_FR" />
 </bean>

Spring MVC 3.

+5

:

@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;
    }
}
+1

, Spring Roo. โ€‹โ€‹ Spring, Spring Roo, Locale - Spring, Roo.

0

, ? , HttpServletRequest, - . , Spring Locale . - RequestContextUtils.getLocale(HttpServletRequest request).

0

All Articles