문제

I have created custom @Controller that ivokes language change in Spring 3:

 RequestContextUtils.getLocaleResolver( request )
    .setLocale( request , response , [some special-way choosen locale] );

This works fine with select-request and any further request only if I dont set attribute defaultlocale in Spring CookieLocaleResolver resolver:

<bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="cookieName" value="clientlanguage" />
        <property name="cookieMaxAge" value="60000" />
        <property name="cookiePath" value="/" />
</bean>

The problem is that I want to avoid locale auto-detection, so I provide default locale:

        <property name="defaultLocale" value="en_EN" />

Adding this property causes that locale is always en_EN, and i can't change it. Even first request to locale-chage controller is rendered with english bundle.

I looked up to HTTP response in laguage change and I get just:

Set-Cookie: clientlanguage=""; ....

도움이 되었습니까?

해결책 3

The problem was, that I was setting "pl_PL" locale instead of just "pl". With "pl_PL" the cookie was set to empty string.

다른 팁

Well accoring to Spring documentation, when you specify the default locale, if no cooki is found, then the default locale is used Spring Doc So I guess that in tests you did, no cookie is sent; what I would suggest you is to use an interceptor; you can check the request and check for cookies; if no cookie is found, you can use your default locale; did you try to use interceptors?

The various methods of CookieLocaleResolver which use the defaultLocale use it like so

request.setAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME,
        (locale != null ? locale: determineDefaultLocale(request)));

In other words, it only uses the defaultLocale if an actual Locale cannot be resolved from the cookies. This is inline with what the javadoc says

Set a fixed Locale that this resolver will return if no cookie found.

You states

Adding this property causes that locale is always en_EN

It will only be en_EN if you either didn't provide a cookie with the Locale or if your

RequestContextUtils.getLocaleResolver( request )
                   .setLocale( request , response , [some special-way choosen locale] );

invocation was made with a null Locale argument.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top