Aubergine
Aubergine

Reputation: 6042

localeChangeInterceptor does not work

I have very standard configuration, I kept editing many times, checked other questions on SO but by my final configuration most people had their issues resolved, however no result for me. Whenever firing requests like:

http://localhost:8080/appName/?lang=es_MX

or

http://localhost:8080/appName?lang=es_MX it does not resolve to correct locale, it does just do nothing.

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/messages" />
    </bean>


    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"></bean>


     <mvc:interceptors>
        <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
            <property name="paramName" value="lang" />
        </bean>
    </mvc:interceptors>

I know that default locale resolver by headers work so is my messageResource and .jsp configuration is correct, since I set up one browser explicitly to have es_MX locale and it resolves with AcceptHeaderLocaleResolver correctly.

Does it have to do with the way my handler mappings are defined?

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home( Model model, Locale locale, HttpServletRequest hr) {

    String header = hr.getHeader("User-Agent");
    model.addAttribute("header", header);
    String contextPath = hr.getContextPath();
    model.addAttribute("contextPath", contextPath);

    return "index";
}

God I spent so much time on this...please help

Upvotes: 0

Views: 4446

Answers (2)

Aubergine
Aubergine

Reputation: 6042

I solved it by placing interceptor into servlet-context.xml instead of my other config. Now trying to figure it out why in servlet-context.xml it works and in my dedicated config it doesn't, your insights would be valuable ! :-) I am not accepting my answer, since it does not explain why. Please explain me why this is the case(and so I could test it - ideally resulting in interceptor being placed in my custom config).

For anyone in trouble try this:

<interceptors>
        <beans:bean
            class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
            p:paramName="lang" />
</interceptors>

inside your servlet-context.xml

needs namespace:

 xmlns:p="http://www.springframework.org/schema/p" 

SessionLocaleResolver seems not to care about location and works in my custom config location too.

Upvotes: 3

Lars Juel Jensen
Lars Juel Jensen

Reputation: 1683

Check the API docs -> http://static.springsource.org/spring/docs/3.2.4.RELEASE/javadoc-api/org/springframework/web/servlet/i18n/SessionLocaleResolver.html

Also the source of the resolveLocale method of SessionLocaleResolver:

public Locale resolveLocale(HttpServletRequest request) {
    Locale locale = (Locale) WebUtils.getSessionAttribute(request, LOCALE_SESSION_ATTRIBUTE_NAME);
    if (locale == null) {
        locale = determineDefaultLocale(request);
    }
    return locale;
}

It will in turn get the Locale from the request:

protected Locale determineDefaultLocale(HttpServletRequest request) {
    Locale defaultLocale = getDefaultLocale();
    if (defaultLocale == null) {
        defaultLocale = request.getLocale();
    }
    return defaultLocale;
}

And in the docs for the ServletRequest.getLocale method:

Returns the preferred Locale that the client will accept content in, based on the Accept-Language header. If the client request doesn't provide an Accept-Language header, this method returns the default locale for the server. Returns: the preferred Locale for the client

It seems you need to change locale by updating the correct session attribute, which is the job of the LocaleChangeInterceptor.

Maybe something is wrong with the interceptor then. Does it set the locale into the correct LocaleResolver? Can you post the whole spring configuration? Is the mvc namespace correct?

There is a complete i18n tutorial here -> http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/

Upvotes: 0

Related Questions