Quick revision
The short answer
We place messages.properties and messages_de.properties under src/main/resources, and Spring Boot auto-configures a MessageSource with the basename messages, set by spring.messages.basename. Boot's default LocaleResolver reads the Accept-Language header, so code that calls messageSource.getMessage(code, args, locale), or resolves a MessageSourceResolvable such as a validation error, gets German text for de and English otherwise. We set spring.messages.fallback-to-system-locale=false so an unsupported locale falls back to messages.properties rather than the server's locale, and Bean Validation messages use the same MessageSource through LocalValidatorFactoryBean.
What to remember
In this answer, we added messages.properties and messages_de.properties and let Spring Boot auto-configure a MessageSource with the messages basename. The default LocaleResolver takes the locale from Accept-Language, messages are resolved with getMessage(code, args, locale) or directly from MessageSourceResolvable errors, and setting fallback-to-system-locale to false makes unsupported locales fall back to the default file rather than the server's locale. Bean Validation messages join the same MessageSource through LocalValidatorFactoryBean.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We place messages.properties and messages_de.properties under src/main/resources, and Spring Boot auto-configures a MessageSource with the basename messages, set by spring.messages.basename. Boot's default LocaleResolver reads the Accept-Language header, so code that calls messageSource.getMessage(code, args, locale), or resolves a MessageSourceResolvable such as a validation error, gets German text for de and English otherwise. We set spring.messages.fallback-to-system-locale=false so an unsupported locale falls back to messages.properties rather than the server's locale, and Bean Validation messages use the same MessageSource through LocalValidatorFactoryBean.
Why this question is asked
An API that returns validation errors only in English is fine until the first customer in another market sees them in a form. Spring has supported internationalisation, often shortened to i18n, from the start, and Spring Boot makes the setup almost automatic. Interviewers ask for it in a coding round because the happy path is easy and the details are not: which locale is chosen, what happens for a language we do not support, and how validation messages join in. In this answer, we will create the message files, see how Boot configures the MessageSource, follow how the locale comes from Accept-Language, resolve messages in code, fix the system-locale fallback trap, and connect Bean Validation.
1Creating the Message Files
A message file is a properties file of codes and texts. The file without a language suffix is the default, and files with a suffix such as _de hold the translations. Placeholders like {0} are filled with arguments at runtime, using java.text.MessageFormat rules. The too-many entry uses a named placeholder, {value}, because it is meant for Bean Validation, which we come to at the end.
Boot reads message files as UTF-8 by default, controlled by spring.messages.encoding, so real umlauts can be written directly in the German file; the example above uses ASCII spellings only to keep it simple.
2How Boot Configures the MessageSource
When Boot finds messages.properties on the classpath, its MessageSourceAutoConfiguration creates a ResourceBundleMessageSource with the basename messages. The basename is the file name without the locale suffix and extension, and spring.messages.basename can list several, separated by commas, such as messages,errors. If we name our files differently and forget the property, no MessageSource is configured from them and codes are not found.
3How the Locale Is Chosen
A LocaleResolver decides the locale of each web request. Boot's default is AcceptHeaderLocaleResolver, which reads the Accept-Language header the browser or client sends, such as de-DE,de;q=0.9,en;q=0.8. Spring makes that locale available through LocaleContextHolder and as a Locale controller parameter.
Then the MessageSource looks for the best file. For de_DE it tries messages_de_DE.properties, then messages_de.properties, then the default file, taking the first file that contains the code.
- Request with Accept-Language: de-DE
LocaleResolverpicks de_DE - Look for messages_de_DEnot present
- Look for messages_deYesUse the German textNoTry the next fallback
- Fall back to system locale?YesServer locale file, often wrongNoUse messages.properties
- Code missing everywhere
NoSuchMessageException
5 steps in order. Play them, or step through them one at a time.
4Resolving Messages in Code
We inject the MessageSource and call getMessage with the code, the arguments and the locale. Anything that implements MessageSourceResolvable, which includes the FieldError objects of a failed validation, can be resolved directly, because it carries its own codes, arguments and default message.
Notice that the locale comes in as a controller method parameter; Spring fills it from the LocaleResolver, so the advice needs no knowledge of headers.
5The System Locale Trap
By default, spring.messages.fallback-to-system-locale is true. That means a request in a language we do not support, say French, first falls back to the server's own locale. If the server happens to run with a German default locale, the French user gets German text, and on another server with an English default the same request gets English. The behaviour then depends on how the machine or container was configured.
Setting it to false makes the default file the fallback everywhere, which is predictable and testable.
6Connecting Bean Validation Messages
Bean Validation constraints carry message templates. If the template is a code in braces, such as {order.name.required}, the validator can look it up in our MessageSource instead of its own ValidationMessages.properties, so all texts live in one place and follow the same locale. LocalValidatorFactoryBean, Spring's bridge to Hibernate Validator, accepts the MessageSource; recent Spring Boot versions wire the application MessageSource into the auto-configured validator, and otherwise we configure it ourselves.
Notice why the validation entry uses {value} rather than {0}. The validator looks up the text in our MessageSource but then fills it with its own interpolation, which knows the constraint's attributes by name, such as value for @Max. Positional {0} placeholders belong to messages we resolve ourselves with getMessage.
7Follow-up questions
How would you reload messages without restarting?
Use ReloadableResourceBundleMessageSource instead of the default ResourceBundleMessageSource, with a cache duration such as 60 seconds, and point its basename at files outside the jar, for example file:/config/i18n/messages, because files inside a jar cannot change without a new build. In Boot, spring.messages.cache-duration controls caching for the auto-configured source, but to read external files we declare our own reloadable bean. For texts managed by non-developers, a MessageSource backed by a database table with a cache is often the better long-term option.
How can a client choose the language other than with Accept-Language?
Configure a different LocaleResolver, such as CookieLocaleResolver or SessionLocaleResolver, together with a LocaleChangeInterceptor that reads a parameter like ?lang=de. For a stateless REST API, Accept-Language is usually the right choice because it is the HTTP standard for this.
What happens if a code is missing in every file?
getMessage throws NoSuchMessageException, unless a default message is passed or spring.messages.use-code-as-default-message is true, in which case the code itself is returned. In tests I prefer the exception, and a test that checks every code exists in every language file catches missing translations before release.
8Conclusion
In this answer, we added messages.properties and messages_de.properties and let Spring Boot auto-configure a MessageSource with the messages basename. The default LocaleResolver takes the locale from Accept-Language, messages are resolved with getMessage(code, args, locale) or directly from MessageSourceResolvable errors, and setting fallback-to-system-locale to false makes unsupported locales fall back to the default file rather than the server's locale. Bean Validation messages join the same MessageSource through LocalValidatorFactoryBean.
Open with
I would put messages.properties with the English texts and messages_de.properties with the German ones under src/main/resources. Spring Boot sees messages.properties and auto-configures a MessageSource with the basename messages, which is the spring.messages.basename property, so if I used other names I would set that property.
Then, if they want more
Boot's default LocaleResolver is the Accept-Language one, so a request with de-DE gets the locale de_DE, and the MessageSource tries messages_de_DE, then messages_de, then the default file.
In code I inject MessageSource and call getMessage with the code, arguments and locale, which I can take as a controller method parameter; validation errors like FieldError are MessageSourceResolvable, so I can pass them directly, which is handy in an exception handler that builds a ProblemDetail. One property I would always set: spring.messages.fallback-to-system-locale=false.
Otherwise a French request falls back to the server's own locale, so the answer depends on how the container was configured; with false it falls back to messages.properties, which is predictable.
For Bean Validation I write constraint messages as codes in braces, like {order.name.required}, and make sure the validator uses the same MessageSource through LocalValidatorFactoryBean's setValidationMessageSource, which recent Boot versions do for us. Then all texts and translations live in one place.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Configure a Spring Boot application to return validation messages in English and German...": https://scrivio-interview-library.netlify.app/spring-boot/spring-utilities/404dc6444b/