Internationalization in Spring Boot

Ikhiloya Imokhai
SkillHive
Published in
3 min readNov 13, 2018
source

Internationalization enables applications to be adapted to different languages. Spring boot offers an easy implementation of this feature and in this short post I’ll show you how to set it up.

For this post, I’ll use a sample spring boot code from my previous article. To follow, you can clone the starter-code .

  1. Configuration class

This is a class annotated with @Configuration and it holds the LocaleResolver Bean which enables us to define a default Locale for the application. The configuration class should also implement the WebMvcConfigurer Interface which provides us with the addInterceprtors() method that enables the application to change the language depending on the language parameter appended to any request. If the request does not have a language parameter or if the language parameter does not exist, it falls back to the default Locale specified.

LocaleConfiguration class

2. Message Property files

Next, add the message property files that will hold key/value pairs of each word to translate. For this post, I’ll be using English(en) and French(fr). All the message property files would be created under src/main/resources folder of the project. I’ll create three properties files:

a. messages.properties: This holds the default messages in the default language, say

good.morning=Good Morning

b. messages_en.properties: This holds the English messages

good.morning=Good Morning

c. messages_fr.properties: This holds the French messages

good.morning=Bonjour

3. Implementation

Next, let’s add code to test our Internationalization feature. In the existing controller, add the following code:

controller for i18n

4. Notes

getMessage method

The first arg to the getMessage() holds the message key as specified in the properties file, e.g “good.morning”.

The second arg (params) is an array of Objects. You could pass a null value here. However, this becomes useful should you want to pass objects or values from the code to the messages.properties file. For example, if you want to pass the person’s name from the code to the properties file. First, we define the key in the properties file for the different languages as such:

good.morning.name={0} Good morning {1}

So that when an array of Objects(say Strings) is passed to the getMessage() method, the first element of the array replaces {0} and the second element replaces {1}. For example:

String[] params= new String[]{“Ikhiloya”, “today”}

The third arg to getMessage() is the Locale which defines the right messages.properties file to read from, if the Locale is not passed or the properties file is not found, it reverts to the default Locale set. The code below shows the implementation.

5. Testing on Postman

Using Postman, we can make a call to the controller and pass “language” as the request parameter.

a. English

localhost:8034/user/get-greeting?language=en

b. French

localhost:8034/user/get-greeting?language=fr

c. Using Parameters

localhost:8034/user/get-greeting-name?language=en

Thanks for reading, you can find the code here.

--

--

Ikhiloya Imokhai
SkillHive

Software Developer | imokhaiikhiloya@gmail.com | https://github.com/Ikhiloya | Reach out to me for Android/Java/Technical writing projects.