Spring MVC - Using FixedLocaleResolver

[Updated: Feb 25, 2017, Created: Jan 9, 2017]

java.lang.ObjectObjectorg.springframework.web.servlet.i18n.AbstractLocaleResolverAbstractLocaleResolverorg.springframework.web.servlet.LocaleResolverLocaleResolverorg.springframework.web.servlet.i18n.AbstractLocaleContextResolverAbstractLocaleContextResolverorg.springframework.web.servlet.LocaleContextResolverLocaleContextResolverorg.springframework.web.servlet.i18n.FixedLocaleResolverFixedLocaleResolverLogicBig

FixedLocaleResolver always uses a default locale and optionally a time zone. It doesn't allow to change both of them once set. In this tutorial we are only to focus on locale feature only. We can set it's fixed default locale during construction time. If none is set, system locale is used.

public FixedLocaleResolver() {
  setDefaultLocale(Locale.getDefault());
}

FixedLocaleResolver is usually needed when we want to turn off the default behavior of AcceptHeaderLocaleResolver. Or we want to switch existing LocaleResolver to a fixed locale for some reasons.

Let's see a quick example how we can use it.



Registering FixedLocaleResolver

If we are using Spring Boot then we have to specify two application properties:

src/main/resources/application.properties

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

The second property is the fixed default locale of FixedLocaleResolver.

Both properties are mandatory for auto configuration to work.

In a traditional mvc configuration with @EnableWebMVC, we have to register it as a bean:

@Configuration
@EnableWebMVC
public class MyConfig{
  @Bean
  LocaleResolver localeResolver(){
     FixedLocaleResolver l = new FixedLocaleResolver();
     l.setDefaultLocale(Locale.JAPAN);
     return l;
  }
 .....
}


The Controller

@Controller
public class TheController {

  @RequestMapping("/")
  @ResponseBody
  public String handleRequest (Locale locale) {
      return String.format("Request received. Locale: %s%n",
                           locale);
  }
}


The main class

@SpringBootApplication
public class Main {

    public static void main (String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

Output

We are using Postman for testing:





Example Project

Dependencies and Technologies Used :

  • spring-boot-starter-web 1.4.3.RELEASE: Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
    Corresponding Spring version: 4.3.5.RELEASE
  • tomcat-embed-jasper 8.5.6: Core Tomcat implementation.
  • JDK 1.8
  • Maven 3.3.9

Fixed Locale Resolver Example Select All Download
  • fixed-locale-resolver-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

See Also