MessageSource is Spring's abstraction for internationalisation (i18n). It resolves a message key and a Locale into the right translated text, formats placeholders and falls back to a default language when a translation is missing. It sits on top of Java's ResourceBundle and MessageFormat, and adds locale fallback, caching and integration with the rest of Spring.
Without it you would hard-code strings or write your own lookup logic for each language. With it, translators edit properties files and your code only knows message keys.
Internationalisation means separating text and locale-dependent formatting from code, so adding a language does not require a code change. A typical application has to handle:
MessageSource covers the first, second and fourth items. Dates, numbers and plurals are handled by MessageFormat patterns or by formatters tied to the same Locale.
MessageSource.getMessage(code, args, locale) follows this order for a request in fr_CA:
messages_fr_CA.propertiesmessages_fr.propertiesfallbackToSystemLocale is true (the default for ResourceBundleMessageSource)messages.propertiesIf the key is still missing, the call throws NoSuchMessageException, unless you passed a default message, or you set useCodeAsDefaultMessage to true, in which case the code itself is returned.
Set fallbackToSystemLocale to false in server applications. Otherwise a user asking for an unsupported language may get the language of the server's operating system instead of your default.
In Spring Boot you can skip the bean and set spring.messages.basename=messages instead. The default basename is messages, and the file is picked up when present.
A request with Accept-Language: de returns:
Spring MVC supplies the Locale through the configured LocaleResolver. The default reads the Accept-Language header. Use SessionLocaleResolver or CookieLocaleResolver with a LocaleChangeInterceptor when users pick a language explicitly, for example with ?lang=de.
Spring builds on the JDK mechanism, which you can use directly:
The differences are that Spring adds a hierarchy (child contexts delegate to parents), reloading with ReloadableResourceBundleMessageSource, the MessageSourceResolvable contract used by validation errors, and injection through MessageSourceAware or plain constructor injection.
checkout.error.card.declined) and never build keys from user input.'' in any message that has placeholders, because MessageFormat treats ' as a quote character.Properties objects catches gaps before release.choice format or an ICU-based library, since MessageFormat has limited plural support.No. It belongs to the core ApplicationContext and works in any Spring application, including batch jobs and command-line tools.
Use ReloadableResourceBundleMessageSource with a cacheSeconds value and a file: basename, or in Boot set spring.messages.cache-duration.
Spring falls back through the locale chain to the base file. If the key is missing everywhere it throws NoSuchMessageException unless a default is supplied.
Yes. Implement AbstractMessageSource and override resolveCode, then cache the results because the lookup runs on every call.
MessageSource is Spring's i18n entry point: keys in properties files, a locale-aware lookup with fallback, and parameter formatting through MessageFormat. Use Boot's spring.messages.* properties for the common case, disable the system locale fallback on servers, and keep UTF-8 encoding explicit.