Internationalization (often written as i18n) means designing an application so that it can provide content in different languages and adapt to different user language preferences.
Instead of writing this directly in Java:
return "Welcome to our application";
you can keep the message outside the Java code:
messages.properties welcome=Welcome to our application
and provide language-specific versions:
messages_fr.properties welcome=Bienvenue dans notre application
welcome. The actual human-language text belongs in message resource files.Imagine an application used by customers from different countries.
Without internationalization, you may end up with language-specific text scattered throughout controllers, services and UI code. That makes translation and maintenance difficult.
| Without i18n | With i18n |
|---|---|
| Text is hard-coded in Java. | Text is stored in resource bundles. |
| Changing a language requires code changes. | Language files can be translated independently. |
Many if/else blocks for languages. | Message lookup chooses the appropriate message. |
| Business code becomes mixed with presentation text. | Business code uses message keys. |
Spring Boot automatically looks for a resource bundle named messages at the root of the classpath.
The default file is:
src/
└── main/
└── resources/
└── messages.properties
The official default basename is messages.
messages.properties.Suppose you only create:
messages_fr.properties messages_de.properties messages_hi.properties
but do not create:
messages.properties
Then Spring Boot will not auto-configure a MessageSource for this setup.
messages.properties file.A typical project can look like this:
src/main/resources/ ├── messages.properties ├── messages_fr.properties ├── messages_de.properties └── messages_hi.properties
Think of the files like this:
Inside the default file:
welcome=Welcome to our application login.success=Login successful user.notFound=User not found
The left side is the key and the right side is the message.
| Part | Example | Purpose |
|---|---|---|
| Key | login.success | Stable identifier used by application code. |
| Message | Login successful | Human-readable text shown to the user. |
The French file can use the same key:
login.success=Connexion réussie
The application still asks for login.success. Only the selected language changes the actual text.
Spring provides a MessageSource abstraction for resolving messages from resource bundles.
Conceptually:
User language
↓
MessageSource
↓
message key
↓
correct resource bundle
↓
localized text
For example:
Key: login.success
Locale: French
↓
messages_fr.properties
login.success=Connexion réussie
MessageSource is the Spring component responsible for looking up a message and resolving it for a particular locale.Spring Boot provides auto-configuration for localized messages when it finds an appropriate message resource bundle.
By default, Spring Boot looks for:
messages.properties
When the required default bundle exists, Spring Boot can automatically configure a MessageSource.
You therefore do not normally need to manually create a MessageSource bean for a basic setup.
The message bundle basename can be configured using the spring.messages namespace.
For example:
spring.messages.basename=messages,config.i18n.messages
This tells Spring Boot that multiple resource bundle base names can be used.
Equivalent YAML:
spring:
messages:
basename: "messages, config.i18n.messages"
If you configure:
spring.messages.basename=messages,config.i18n.messages
Spring Boot can look for message resources associated with those base names.
messages.properties messages_fr.properties config/i18n/messages.properties config/i18n/messages_fr.properties
spring.messages.basename property supports a list of locations. A location can be expressed as a package qualifier or as a classpath resource resolved from the classpath root.Large applications may organize messages into multiple bundles.
spring.messages.basename=messages,config.i18n.messages
This can separate general application messages from another collection of internationalized messages.
| Base name | Example resources |
|---|---|
messages | messages.properties, messages_fr.properties |
config.i18n.messages | config/i18n/messages.properties, language-specific variants |
Spring Boot also supports a spring.messages.common-messages property.
spring.messages.common-messages=classpath:my-common-messages.properties
YAML:
spring:
messages:
common-messages: "classpath:my-common-messages.properties"
This property supports a list of property-file resources.
basename → where your normal localized message bundles are.common-messages → additional common message property resources.
Spring Boot exposes the fallback-to-system-locale setting.
Example:
spring.messages.fallback-to-system-locale=false
YAML:
spring:
messages:
fallback-to-system-locale: false
This controls whether message resolution falls back to the system locale.
spring.messages.fallback-to-system-locale, think: “Should message lookup fall back to the JVM/system locale?”A simple Spring Boot internationalization configuration can be:
spring:
messages:
basename: "messages"
fallback-to-system-locale: false
Then your resources:
src/main/resources/ ├── messages.properties ├── messages_fr.properties └── messages_de.properties
welcome=Welcome login.success=Login successful login.failed=Login failed user.notFound=User not found order.created=Order created successfully
welcome=Bienvenue login.success=Connexion réussie login.failed=Échec de la connexion user.notFound=Utilisateur introuvable order.created=Commande créée avec succès
welcome=Willkommen login.success=Anmeldung erfolgreich login.failed=Anmeldung fehlgeschlagen user.notFound=Benutzer nicht gefunden order.created=Bestellung erfolgreich erstellt
A Locale represents language and regional preferences used for localized behavior.
Examples include:
| Locale idea | Meaning |
|---|---|
en | English |
fr | French |
de | German |
hi | Hindi |
When the application resolves a key, the locale helps determine which language-specific resource should be used.
login.success
+
French locale
↓
messages_fr.properties
↓
Connexion réussie
A common application architecture is:
HTTP Request
↓
Controller
↓
Service
↓
MessageSource
↓
Localized message
↓
HTTP Response
For example, a REST API may return a localized validation or error message based on the request's locale.
spring.messages properties. The exact way your application obtains a user's locale depends on the surrounding Spring MVC/Web configuration.A good design separates business decisions from human-language text.
if (user == null) {
return "User not found";
}
if (user == null) {
// use the message key:
// user.notFound
}
The translation belongs in the message bundle:
user.notFound=User not found
and:
user.notFound=Utilisateur introuvable
The main Spring Boot configuration namespace is:
spring.messages.*
| Property | What to remember |
|---|---|
spring.messages.basename | Defines message bundle base names. |
spring.messages.common-messages | Defines additional common message resources. |
spring.messages.fallback-to-system-locale | Controls fallback to the system locale. |
Spring Boot provides additional supported options through MessageSourceProperties.
my-spring-app/ ├── src/ │ └── main/ │ ├── java/ │ │ └── com/example/app/ │ │ ├── controller/ │ │ ├── service/ │ │ └── ... │ └── resources/ │ ├── application.properties │ ├── messages.properties │ ├── messages_fr.properties │ ├── messages_de.properties │ └── messages_hi.properties └── pom.xml
application.properties:
spring.messages.basename=messages spring.messages.fallback-to-system-locale=false
At a high level:
Spring Boot starts
↓
Reads application configuration
↓
Checks configured message bundle
↓
Finds messages.properties
↓
Auto-configures MessageSource
↓
Application can resolve localized messages
MessageSource.Suppose your project contains:
messages_fr.properties messages_de.properties
but no:
messages.properties
Then the expected Spring Boot auto-configuration does not occur.
The fix is simple:
messages.properties
Even if your application primarily serves another language, provide the documented default bundle so the auto-configuration condition is satisfied.
messages.properties.messages..., configure spring.messages.basename.user.notFound.fallback-to-system-locale is configured to do.messages.properties as the default bundle for the basic Spring Boot setup.spring.messages.basename when organizing messages into multiple bundles.It is designing an application so it can support users with different language or locale preferences without rewriting application logic for each language.
Q2. What is the default Spring Boot message bundle name?messages, with the default resource file being messages.properties.
Spring Boot's message auto-configuration applies when the default properties file for the configured resource bundle is available. By default, that is messages.properties.
A Spring abstraction used to resolve messages from resource bundles for a locale.
Q5. How do you configure multiple message bundle base names?spring.messages.basename=messages,config.i18n.messagesQ6. What does fallback-to-system-locale mean?
It controls whether message resolution can fall back to the system locale.
Q7. What is the difference between messages.properties and messages_fr.properties?The first is the default bundle; the second is a locale-specific French resource bundle.
Q8. What happens if only messages_fr.properties exists?Spring Boot does not create the auto-configured MessageSource when the default properties file for the configured basename is missing.
Q9. Why should messages be stored outside Java code?It separates human-language text from application logic and makes translation and maintenance easier.
Q10. Which Spring Boot configuration namespace is used for message configuration?spring.messages.
messages.properties with five application messages.spring.messages.basename explicitly.fallback-to-system-locale=false.config/i18n.spring.messages.basename.messages.properties temporarily and observe the auto-configuration behavior.| Goal | Configuration / File |
|---|---|
| Default bundle | messages.properties |
| French bundle | messages_fr.properties |
| German bundle | messages_de.properties |
| Message configuration namespace | spring.messages |
| Bundle names | spring.messages.basename=messages,config.i18n.messages |
| Common messages | spring.messages.common-messages=classpath:my-common-messages.properties |
| Disable system-locale fallback | spring.messages.fallback-to-system-locale=false |
| Spring abstraction | MessageSource |
Spring Boot Internationalization →
Message bundle → messages.properties → locale-specific files → MessageSource → spring.messages → basename → common messages → locale fallback
The core idea is very simple:
Message Key
↓
Message Bundle
↓
Locale
↓
Localized Message
For a basic Spring Boot application, start with:
src/main/resources/messages.properties
Then add language-specific bundles such as:
messages_fr.properties messages_de.properties messages_hi.properties
If you need a different or multiple bundle locations, configure:
spring.messages.basename=...
And remember the most important Spring Boot rule from this chapter:
messages.properties file. Spring Boot's internationalization auto-configuration depends on finding the default properties file for a configured resource bundle.