[ACCEPTED]-Spring: Setting up locale-internationalization

Accepted answer
Score: 16

localeResolver.setLocale works fine for 9 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 8 (default language)

\src\main\resources\messages\messages_en.properties

my.message=Message 7 in English

\src\main\resources\messages\messages_fr.properties

my.message=Message 6 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 5 "Message in French", if I change "fr" to 4 "en" I get "Message in English", and without 3 setLocale call I get "Message (default language)". Changing 2 StringUtils.parseLocaleString("fr") to new 1 Locale("fr") gives the same results.

Score: 6

I would recommend try to set up default 2 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 1 Configuring locale switching with Spring MVC 3.

Score: 1

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;
    }
}

0

Score: 1
 @Bean
 public LocaleResolver localeResolver() {
     SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
     Locale locale = new Locale("tr", "TR");
     sessionLocaleResolver.setDefaultLocale(locale);
     return sessionLocaleResolver;
 }

0

Score: 0

You can probably take a look at Spring Roo 5 project. There is an internationalization 4 add on from Spring which is being used in 3 Spring Roo which enables quick switching 2 of Locale within a Spring Web application 1 automatically generated from Roo.

Score: 0

How do you determine that the locale has 8 not been set? If you expect, the correct 7 locale to be present in HttpServletRequest, this isn't true 6 — its value is handled by the servlet container 5 and is therefore immutable. Instead, you 4 should rely that Spring will inject a proper 3 value to a method parameter with class Locale in 2 your controller. Another way to obtain the 1 locale is by using RequestContextUtils.getLocale(HttpServletRequest request) directly.

More Related questions