brightec
Home Services Projects Blog About Contact

Internationalise your multi-module Spring Boot Application

Alistair working at his desk alongside Rhys

How and where to define your user-facing strings.

Messages.properties

You've probably already read about the messages.properties file. It allows you to define any strings you wish to present to an end-user.

messages.properties:

To retrieve one of these strings in code you first need to get a MessageSource. This is provided via spring dependency injection.

Constructor injection:


@Service // Or some other `Component` annotation
class SomeService( private val messageSource: MessageSource )

Property injection:


@Autowired
private lateinit var messageSource: MessageSource

Then you can call getMessage:


messageSource.getMessage("title.example", null, LocaleContextHolder.getLocale())

args can be provided for string replacement. For example:


messageSource.getMessage("message.hello", arrayOf("Joe Bloggs"), LocaleContextHolder.getLocale())

Internationalization

Notice above, we pass LocaleContextHolder.getLocale() as a Locale to the getMessage call. This tells the MessageSource which language to provide.

LocaleContextHolder provides the current Locale that spring is aware of. This is a powerful mechanism. We don't need to add any custom code to our RestController's or otherwise to determine Locale. Callers can set the locale as a request header and spring will make that available.

To provide other language strings we add a new messages file, messages_fr.properties:

To learn more, check out this guide.

Modularization

When modularizing your application you may run into trouble. By default the messages.properties files from each module will conflict with each other. Only one set of messages will be available.

To avoid this, all your messages files need to have different names. A good convention to follow is to use your module name. For example: messages-application.properties, messages-application_fr.properties, messages-library.properties, messages-library_fr.properties.

Now we need to expose these to the MessageSource. By default, only files named message.properties will be found.

In your application.properties file add:


spring.messages.basename=messages-application,messages-library

Notice we do not include the language extension or the file extension here.

That's it. You can now provide strings in each of your different modules.




Looking for something else?

Search over 200 blog posts from our team

Want to hear more?

Subscribe to our monthly digest of blogs to stay in the loop and come with us on our journey to make things better!