Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Configure a Spring Boot application to return validation messages in English and German, with files messages.properties and messages_de.properties, based on the Accept-Language header. What do you need?

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.

Properties
# src/main/resources/messages.properties (English, the default)
order.name.required=Please enter a name.
order.quantity.max=You can order at most {0} items.
order.quantity.too-many=You can order at most {value} items.
# src/main/resources/messages_de.properties (German)
order.name.required=Bitte geben Sie einen Namen ein.
order.quantity.max=Sie koennen hoechstens {0} Artikel bestellen.
order.quantity.too-many=Sie koennen hoechstens {value} Artikel bestellen.

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.

Java
# application.properties
# messages is the default; list more as messages,errors
spring.messages.basename=messages
spring.messages.fallback-to-system-locale=false
# fail loudly on a missing code instead of returning the code itself
spring.messages.use-code-as-default-message=false

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.

Step throughFrom Accept-Language to a message
  1. Request with Accept-Language: de-DELocaleResolver picks de_DE
  2. Look for messages_de_DEnot present
  3. Look for messages_de
    YesUse the German textNoTry the next fallback
  4. Fall back to system locale?
    YesServer locale file, often wrongNoUse messages.properties
  5. Code missing everywhereNoSuchMessageException

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.

Java
@RestControllerAdvice
class ValidationErrors {
private final MessageSource messages;
ValidationErrors(MessageSource messages) { this.messages = messages; }
@ExceptionHandler(MethodArgumentNotValidException.class)
ProblemDetail invalid(MethodArgumentNotValidException ex, Locale locale) {
ProblemDetail pd = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST);
pd.setProperty("errors", ex.getBindingResult().getFieldErrors().stream()
.map(e -> messages.getMessage(e, locale)) // FieldError is a MessageSourceResolvable
.toList());
return pd;
}
}
messages.getMessage("order.quantity.max", new Object[] {10}, Locale.GERMAN);
// Sie koennen hoechstens 10 Artikel bestellen.
messages.getMessage("order.quantity.max", new Object[] {10}, Locale.ENGLISH);
// You can order at most 10 items.

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.

Properties
# fallback-to-system-locale=true (default), server default locale de_DE
# Accept-Language: fr -> no messages_fr -> system locale de -> "Bitte geben Sie einen Namen ein."
# fallback-to-system-locale=false
# Accept-Language: fr -> no messages_fr -> messages.properties -> "Please enter a name."

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.

Java
record OrderRequest(
@NotBlank(message = "{order.name.required}") String name,
@Max(value = 10, message = "{order.quantity.too-many}") int quantity) { }
// Only needed if your Boot version does not do it already:
@Bean
LocalValidatorFactoryBean validator(MessageSource messageSource) {
LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
bean.setValidationMessageSource(messageSource);
return bean;
}
// POST /orders {"name":"","quantity":20} Accept-Language: de
// errors: ["Bitte geben Sie einen Namen ein.", ...]

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.

What to say in the interviewAbout 80 seconds aloud

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.
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/