@RequestMapping does not manage locales by itself. Locale handling in Spring MVC is done by a LocaleResolver and a LocaleChangeInterceptor. @RequestMapping only routes requests, but you can use it with a locale in the URL path or a query parameter, and you can read the resolved Locale in the handler method.
The best-practice approach is to resolve the locale once per request, load text from message bundles, and keep one handler per page rather than one handler per language. Mapping a separate method for each language with params = "locale=en" works but does not scale.
For each request, DispatcherServlet asks the configured LocaleResolver bean for a Locale. The result is available in handler methods, in LocaleContextHolder and in view templates. The built-in resolvers are:
| Resolver | Source of the locale | Can be changed at runtime |
|---|---|---|
AcceptHeaderLocaleResolver | Accept-Language request header (default) | No |
CookieLocaleResolver | A cookie | Yes |
SessionLocaleResolver | The HTTP session | Yes |
FixedLocaleResolver | A fixed value | No |
In Spring Boot, the default is AcceptHeaderLocaleResolver, with spring.web.locale as the fallback and spring.web.locale-resolver=fixed to force a fixed one.
To let users change language with a parameter such as ?lang=fr, define a resolver and a LocaleChangeInterceptor.
The bean method must be named localeResolver (the bean name Spring looks for). A request such as /greeting?lang=fr now stores fr in a cookie and uses it for later requests.
Put translations in messages_*.properties files under src/main/resources:
Spring Boot auto-configures a MessageSource that reads messages.properties by default. Set spring.messages.basename if you use another name, and spring.messages.encoding is UTF-8 by default.
Inject MessageSource and take the Locale as a method argument. Spring fills it from the active LocaleResolver.
In Thymeleaf you can use #{greeting} in templates, and the same locale is applied automatically.
For SEO and shareable links, many sites put the language in the path, such as /fr/products. Map it with a path variable, or use a servlet filter or interceptor that strips the prefix and sets the locale.
Validate the language against a whitelist before using it, otherwise arbitrary strings reach your resolver. Search engines also expect hreflang links between the translated versions.
You can also write separate methods that match different parameters:
This is valid, but every new language needs another method, and a request without locale returns 404 unless you add a fallback. Use it only when the pages really differ in structure per language.
No. It only matches requests. The locale comes from the LocaleResolver, and you read it from a Locale argument or LocaleContextHolder.getLocale().
Register a LocaleChangeInterceptor and use a CookieLocaleResolver or SessionLocaleResolver. Send ?lang=xx and the choice is remembered.
AcceptHeaderLocaleResolver is read-only and ignores setLocale. Switch to a cookie or session resolver if you want a manual language switch.
In messages_xx.properties bundles for small apps, or a database or translation service for content that editors change often.
Locale handling in Spring MVC is a job for LocaleResolver, LocaleChangeInterceptor and MessageSource. Use @RequestMapping or @GetMapping to route a single handler, read the Locale argument, and avoid per-language handler methods unless the pages truly differ.