How can I change Spring locale cookie name?

The cookie for spring MVC locale is named by default as org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE.

How can I set this name to another one?

3 Answers

The Java config way:

@Bean public LocaleResolver localeResolver() { final CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver(); cookieLocaleResolver.setDefaultLocale(Locale.ENGLISH); cookieLocaleResolver.setCookieName("YOUR_LOCALE_COOKIE_NAME"); return cookieLocaleResolver; } 
1

The cookie name can be set in the configuration of the localeResolver, used to store locale changes in session cookies:

<bean> <property name="defaultLocale" value="en"></property> <property name="cookieName" value="YOUR_LOCALE_COOKIE_NAME"/> </bean> 

Thanks to dimitrisli for the link to documentation.

Using Java config:

@Bean public HandlerInterceptor localChangeInterceptor(){ LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor(); localeInterceptor.setParamName("lang"); return localeInterceptor; } 

Using XML config:

 <mvc:interceptors> <bean> <property name="paramName" value="lang" /> </bean> </mvc:interceptors> 

Documentation

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like