String externalization in Spring 3.1+ with MessageSource [no web.xml]

5 07 2014

spring-logoWhen dealing with translations or keeping text messages in one file you can use embedded Spring engine supporting String externalization with MessageSource Bean. There is a plenty of tutorials how to do it with web.xml based configuration, but not so many of them regarding the new Spring version and Spring boot completely in Java code.

Here I will show how to set up the MessageSource bean, create messages file, use messages both in Java and thymeleaf and a clue how to easily make a translation.

Source code
Get the source code for this tutorial from my GitHub here.

1. Start with the SpringMVC tutorial Code
This tutorial is based on the Spring MVC tutorial, so may want either follow it first, download its source code or if you are advanced enough, the following description should be enough.

2.Create messages text file
Start with defining your messages. Put the definitions in file. My messages are as follows:

hello.content=Click here to advance to the next page
greeting.header=Hello on a second page!
greeting.content=Click here to go back

The file should be placed under the src/main/resources folder. My location is src/main/resources/locale/

3. Configure MessageSource bean to see messages file
In your MVC Spring app configuration (the class marked with @Configuration annotation and extending WebMvcConfigurerAdapter class) configure the MessageSource Bean. Remember to annotate it with @Bean. Here is my config, pointing to locale/ directory in src/main/java/resources/, and setting encoding to UTF-8:

public class MvcConfig extends WebMvcConfigurerAdapter {

	public MessageSource messageSource() {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		return messageSource;

Now when you have bean and up and ready, you can use it in Java code and thymeleaf templates.

4. Use messages in a thymeleaf template
To access the message in thymeleaf template you simply query for the desired message key with syntax: #{} in th:text tag. For header element it looks as follows:

<h1 th:text="#{greeting.header}"></h1>

5. Use messages in Java code
I am not sure whether accessing the externalized messages in your Java methods indicates a good MVC design, however if you need to use them – here is how you can do it.

First get access to MessageSource bean by Autowiring it to the constructor or setter and store it in class private field:

private MessageSource messageSource;

public void setMessageSource(MessageSource messageSource) {
	this.messageSource = messageSource;

Then you can get the desired message as simple as that:

messageSource.getMessage("hello.content", null, Locale.US);

This will return a string with message text for en_US locale or the default string if implicit message for en_US is not found.

Locale and translations
To provide translation simply prepare the for each language you want to support. The locale indicator is in the file name. Here is sample config for english (en), polish (pl) and default locale message files:

In each file, under the same key, provide the translations for the specific language. Spring will interpret file name and use the file proper for requested locale:
– in Java code you indicate the locale implicit, by passing it to messageSource method
– in thymeleaf the locale is taken from users’ browser setting

Note: Always provide the default file with default language so that if implicit locale is not found, the default message will be displayed.

Source code
Get the source code for this tutorial from my GitHub here.

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!



2 responses

17 05 2015

Thank you for this simple and nice article!

30 05 2016

Thanks! Very useful

Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: