Spring Boot Properties: Setting the locale

A question from a student about Internationalization in Spring Boot.

In today’s article, we are going to look at a question from a student in my Spring Boot Introduction course. This question has to do with Internationalization and more specifically how to set the locale in the properties file of a Spring Boot application. 

Spring Boot Internationalization: Setting the locale

This question comes in from Mykhaylo. 

Hi,

I’ve put in my property file spring.mvc.locale=fr , but it is still messages from default messages.properties file that are displayed. 

If I display a value of current locale I see that this is en_GB, and not French:  <p th:text=”${#locale}”>…..</p>

What is the way to set up a locale in SpringBoot?

Setting the locale

Before we dive into the solution let’s take a look at what the student was doing here. If you want to override the locale and give it a fixed value you can do so by setting the following property in your application.properties. 

In this example, we are setting the locale to French. If you aren’t sure of the different locale settings, don’t worry. IntelliJ will provide intelli-sense here and give you a list of choices to choose from.

Spring Boot Locale

With that in place, I went ahead and created a quick example. 

I placed a debug marker on line 13, ran the application and visited http://localhost:8080. To my surprise, the locale was still what I would expect mine to show up as, en_US. 

Spring Boot Locale

Locale Resolver

This is actually expected behavior and it has to do with something called the LocaleResolver. This interface allows for implementations based on the request, session, cookies, etc. The default implementation is,AcceptHeaderLocaleResolver simply using the request’s locale provided by the respective HTTP header. 

This means that no matter what you set the locale to Spring is always going to look at the HTTP request header to determine the locale. Luckily for us we can override the LocaleResolver using another property in Spring Boot. 

With this setting in place, our application should now use the locale that you set in your properties file. 

Spring Boot Locale

If you think you had this working at one point in your application you aren’t crazy. This worked in Spring Boot 1.3.7 but then in 1.4 there was a change and if you want it to work going forward you must set the locale resolver. 

Setting the locale Screencast

I created a quick screencast to walk through setting the locale in your Spring Boot application. Please subscribe to my YouTube channel to get notifications about new screencasts. 

Conclusion

I want to thank Mykhaylo again for the question. This is one of those things that has probably tripped most of us at some point. 

Hi, I’m Dan Vega. I am a Software Engineer living just outside of one of my favorite places in the world, Cleveland, Ohio. I am a self-taught programmer who firmly believes that you can accomplish anything in this life if you put your mind to it, roll up your sleeves and are willing to put in the work.

I have been writing software for over 18+ years now and I use my knowledge, passion, and influence to help other developers achieve their goals. I teach online and currently, have over 30,000 students.

When I am not writing software, I have a number of things that really keep me busy. I love to read books, listen to podcasts and watch online courses. I have a real thirst for knowledge and it consumes me every single day of the week. I also love to run & lift heavy weights which keep me balanced throughout the week.

Please note: I reserve the right to delete comments that are offensive or off-topic.