Get all message properties key value pairs as JSON for selected locale in Spring boot

Problem Statement

For internationalization we store all key value pairs as message properties in spring boot project such that our web component doesn’t have to store all the key value pairs for all the locales which we are supporting now or we will support in future. And based on which locale user selects we have to only send the map of key value pairs as a JSON for that particular locale to our web component.


Let’s say we support three language en, cn and jp language as of now. So we will have three files, in our resources folder. And we all know that we can access the message properties directly in thymeleaf templates as #{key_name} , but in our case we want all the key value pairs as a map at once. So let’s dive deep into how we solved it step by step.

Step 1

First, we will create a method to get all the message properties by accessing getMergedProperties method of ReloadableResourceBundleMessageSource class.

Step 2

Now, we can create a utility service which will send us the HashMap of key value pairs given the locale.

Step 3 (Final)

Now we can inject MessageResolverService in our controller and convert it to JSON with the help of ObjectMapper. So we can create a utility method getLanguageMapJson which will call the getMessages method and write it as JSON using ObjectMapper and return the JSON string given the locale.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pratiyush Prakash

Full stack dev @ Texas Instruments. Follow me for short articles on software development. I will cover topics like Spring boot, Angular, SQL and so on.