Spring Boot Properties: Setting the locale

Spring Boot Properties: Setting the locale

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. 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:  

.....

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. 

spring.mvc.locale=fr_FR

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. 

package com.therealdanvega.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Locale;

@RestController
public class HomeController {

    @RequestMapping("/")
    public String home(Locale locale) {
        return locale.toString();
    }

}

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. 

spring.mvc.locale=fr_FR
spring.mvc.locale-resolver=fixed

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. 

https://www.youtube.com/watch?v=nt27SPF10vY

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.

Follow me on Twitter, LinkedIn, or sign up for my newsletter to get my latest articles and tutorials.
Dan Vega

Dan Vega

I’m a Husband, Father, Curriculum Developer and maker of things from Cleveland Ohio. I created this website as a place to document my journey as I learn new things and share them with you. I have a real passion for teaching and I hope that one of blog posts, videos or courses helps you solve a problem or learn something new.