The LocaleResolver interface in Spring MVC determines the Locale for each web request, so the application can pick the right language, date format and number format. DispatcherServlet looks up a bean named localeResolver, calls it for every request and exposes the result through RequestContext, LocaleContextHolder and controller method arguments.
If you define no resolver, Spring uses AcceptHeaderLocaleResolver, which reads the browser's Accept-Language header.
LocaleResolver doesLocaleResolver doesThe interface has two methods:
resolveLocale is called on every request. setLocale is called when something changes the locale, usually LocaleChangeInterceptor. Not every implementation supports changing: AcceptHeaderLocaleResolver and FixedLocaleResolver throw UnsupportedOperationException from setLocale.
A richer sub-interface, LocaleContextResolver, also carries a time zone through LocaleContext. SessionLocaleResolver and CookieLocaleResolver implement it.
| Implementation | Where the locale comes from | Can the user change it? |
|---|---|---|
AcceptHeaderLocaleResolver | The Accept-Language request header (default). | No. |
SessionLocaleResolver | The HTTP session, with a default fallback. | Yes, stored per session. |
CookieLocaleResolver | A cookie on the browser. | Yes, survives the session. |
FixedLocaleResolver | A single locale for the whole application. | No. |
Use the cookie resolver when users should keep their language across visits, the session resolver for per-session choices, and the header resolver when you simply follow the browser.
This Java configuration stores the locale in a cookie, lets users switch with ?lang=fr and loads messages from messages*.properties:
The CookieLocaleResolver(String cookieName) constructor and Duration based setCookieMaxAge are the current API in Spring Framework 6. The older setters for name and numeric max age are deprecated.
Create src/main/resources/messages.properties and messages_fr.properties:
Spring MVC injects the resolved locale directly:
Requesting /greeting?lang=fr stores fr in the cookie and returns Bonjour. Later requests without the parameter keep using French. Anywhere outside a controller, call LocaleContextHolder.getLocale().
Spring Boot registers a MessageSource automatically from spring.messages.basename (default messages), and a locale resolver from two properties:
spring.web.locale-resolver accepts accept-header (default) or fixed. For cookie or session resolvers, declare your own localeResolver bean as above; Boot backs off.
AcceptHeaderLocaleResolver can limit the result to languages you actually translate:
Unsupported requests fall back to the default locale instead of producing missing-message lookups.
The bean must be named localeResolver. A bean named myLocaleResolver is ignored by DispatcherServlet.
?lang=fr throw UnsupportedOperationException?The active resolver is AcceptHeaderLocaleResolver or FixedLocaleResolver, which cannot change the locale. Use a session or cookie resolver.
Choose the cookie resolver if the choice should persist after the session ends and you run stateless servers. Choose the session resolver when you already rely on sessions.
LocaleResolver work in WebFlux?No. WebFlux uses LocaleContextResolver from the org.springframework.web.server.i18n package, with an Accept-Language based default.
LocaleResolver tells Spring MVC which locale each request uses. Pick a built-in implementation (header, session, cookie or fixed), name the bean localeResolver, add LocaleChangeInterceptor for user switching and pair it with a MessageSource for translated text.