FixedLocaleResolver always uses a default locale and, optionally, a default time zone. It doesn't allow changing either of them once set. In this tutorial we will focus only on the locale feature. We can set its fixed default locale at construction time. If none is set, the 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 when we want to switch an existing LocaleResolver to a fixed locale for some reason.
Let's see a quick example of how we can use it.
Example
Registering FixedLocaleResolver
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.i18n.FixedLocaleResolver;
import java.util.Locale;
@EnableWebMvc
@Configuration
@ComponentScan
public class WebConfig {
@Bean
LocaleResolver localeResolver() {
FixedLocaleResolver l = new FixedLocaleResolver();
l.setDefaultLocale(Locale.JAPAN);
return l;
}
}