LocaleChangeInterceptor is a Spring MVC HandlerInterceptor that lets a user switch the application's locale at runtime. On each request it reads a parameter (named locale by default), converts the value to a Locale, and passes it to the configured LocaleResolver, which stores it so that later requests, message lookups and formatting use the new language.
It does nothing on its own. It only works together with a LocaleResolver that can store a locale, such as SessionLocaleResolver or CookieLocaleResolver, and a MessageSource that holds the translated texts.
Spring MVC resolves the locale for every request through the LocaleResolver bean. The default resolver in Spring Boot reads the Accept-Language header, which the browser controls and the user cannot change from your page. To let users pick a language from a menu, you need a way to say "use French from now on".
LocaleChangeInterceptor provides that. In its preHandle method it:
?lang=fr.StringUtils.parseLocale, so fr, fr_CA and fr-CA all work.localeResolver.setLocale(request, response, locale).Because the resolver stores the result (in the HTTP session or in a cookie), the user does not need to repeat the parameter on later requests.
You need three pieces: a storing LocaleResolver, the interceptor itself and its registration. The LocaleResolver bean must be named localeResolver, which is the name DispatcherServlet looks up.
Without setParamName("lang") the parameter is locale. Without the addInterceptors registration the bean is created but never runs.
Spring Boot auto-configures a MessageSource that reads messages.properties from the classpath root. Add one file per language:
A controller can then use the resolved locale directly:
Requests behave like this:
In Thymeleaf templates you read the same bundle with #{greeting}, and the current locale is applied automatically.
| Resolver | Where the locale is stored | Works with the interceptor |
|---|---|---|
AcceptHeaderLocaleResolver | Nowhere, read from Accept-Language | No, setLocale is not supported |
SessionLocaleResolver | HTTP session | Yes |
CookieLocaleResolver | Browser cookie | Yes |
FixedLocaleResolver | Fixed value | No, changes are rejected |
If you keep the default AcceptHeaderLocaleResolver, a request with ?lang=fr fails with an UnsupportedOperationException ("Cannot change HTTP Accept-Language header"). That is the most common setup mistake.
For stateless or load-balanced services, a cookie is usually better than a session, because the preference survives restarts and does not need session replication:
IllegalArgumentException. Call setIgnoreInvalidLocale(true) to ignore bad input instead of returning an error.setHttpMethods("GET", "POST") limits the interceptor to the listed methods. Use it if you do not want a locale change triggered by other methods.SameSite, Secure over HTTPS) as for any cookie.lang parameter or the cookie in the cache key, otherwise users see someone else's language.Accept-Language header is usually enough, and you do not need this interceptor at all.It is locale. Call setParamName to use something shorter such as lang.
You are still using AcceptHeaderLocaleResolver, which Spring Boot configures by default. Define a localeResolver bean of type SessionLocaleResolver or CookieLocaleResolver.
No. The resolver stores it, so it applies to later requests in the same session or for as long as the cookie lives.
Yes. It is part of Spring MVC. Register the interceptor in your WebMvcConfigurer and declare the localeResolver and messageSource beans yourself.
LocaleChangeInterceptor turns a request parameter such as ?lang=fr into a stored Locale through the LocaleResolver. Pair it with a session or cookie resolver, register it in addInterceptors, provide messages_xx.properties bundles, and the whole application, including messages, dates and numbers, follows the user's chosen language.