@RequestMapping does not translate anything itself, but it can route requests for different locales, for example /en/greeting and /fr/greeting, to handler methods. Spring's real i18n machinery is a LocaleResolver, a MessageSource and, optionally, a LocaleChangeInterceptor. A good design uses @RequestMapping only to carry the locale in the URL, and lets Spring resolve the Locale and the message bundle.
The examples target Spring Boot 3 on Java 17 or newer.
Internationalization separates text from code so one application can serve many languages. Spring MVC splits this into three parts:
| Component | Job |
|---|---|
LocaleResolver | Decides the current Locale for each request |
MessageSource | Looks up text by key and locale from messages_xx.properties bundles |
LocaleChangeInterceptor | Lets a request parameter switch the locale |
@RequestMapping takes part only in routing. It can match a language prefix in the path, an Accept-Language header or a request parameter, but the actual choice of locale belongs to the resolver.
Create property files on the classpath, one per language.
Spring Boot auto-configures a MessageSource that reads messages.properties by default. Change the base name with spring.messages.basename and set a fallback with spring.messages.fallback-to-system-locale=false if you do not want the server's own locale to influence the result. If a key is missing in a language file, Spring falls back to the default file.
Out of the box, Boot uses AcceptHeaderLocaleResolver, which reads the browser's Accept-Language header. A controller can simply declare a Locale argument.
GET /greeting?name=Ana with Accept-Language: fr returns Bonjour, Ana !. This is usually the best choice for APIs, because the client states its preference and every language shares one URL, which also keeps caching simple if you send Vary: Accept-Language.
For websites, a language prefix in the path is better for SEO, because each language gets its own crawlable URL. Use a path variable with a regular expression so only supported codes match.
/fr/greeting returns the French text and /de/greeting is a 404. This single mapping replaces the one-method-per-language pattern (/greeting/en, /greeting/fr), which duplicates code and grows with every new language.
To let users change language with ?lang=fr and remember the choice, register a CookieLocaleResolver (or SessionLocaleResolver) and a LocaleChangeInterceptor.
Now any mapped URL accepts ?lang=es; the interceptor updates the resolver and later requests use the cookie. The bean must be named localeResolver, otherwise Spring does not pick it up.
Accept-Language and a Locale argument.LocaleResolver.Mixing a path prefix with the interceptor is possible but confusing, so pick one source of truth per application.
No. It only routes requests. Translation comes from MessageSource and the message bundles, with the locale supplied by a LocaleResolver.
Add a java.util.Locale parameter to the handler method, or call LocaleContextHolder.getLocale().
Common causes are a missing messages_xx.properties file, a localeResolver bean with the wrong name, or the system-locale fallback. Check spring.messages.fallback-to-system-locale.
Yes. Bean Validation messages are resolved through the same MessageSource when you define a LocalValidatorFactoryBean backed by it, and Boot wires this up for you in common setups.
@RequestMapping supports i18n only as a router: it can carry a language in the path or match headers. The real work is done by LocaleResolver, MessageSource and LocaleChangeInterceptor. Use Accept-Language for APIs, a validated {lang} path variable for indexable sites, and a cookie or profile for user choice.