It would be nice if the locale used by Spring could be constrained automatically by Spring Boot simply by setting a property

spring.mvc.locale.list=en,fr_CA,es
spring.mvc.locale.default=en

This way if (in this example), the Accept-Language header was set to zh_CN, fr-CA;q=0.9, en;q=0.8 the locale would end up being fr_CA because it is the highest quality match.

I have some code that I can contribute if there is interest.

Comment From: wilkinsona

It looks like you're proposing a configuration property that would set the supported locales on the auto-configured AcceptHeaderLocaleResolver?

Assuming I've understood you correctly, I think we'd consider a contribution that does that. I'm not entirely sure what the property's name should be as it would only be supported when using AcceptHeaderLocaleResolver and not when using FixedLocaleResolver, but we can figure that out as part of merging something.

Comment From: tadaskay

Related: An improvement https://jira.spring.io/browse/SPR-15426 has been resolved in Spring 4.3.8. Now it properly falls back to supported locales/default when Accept-Language doesn't match.

Without the auto configuration, the following can be used to setup locales that are supported by the app:

    private static final List<Locale> SUPPORTED_LOCALES = Arrays.asList(
        Locale.ENGLISH,
        Locale.SIMPLIFIED_CHINESE
    );

    @Autowired
    private WebMvcProperties properties;

    @Bean
    public LocaleResolver localeResolver() {
        AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
        resolver.setSupportedLocales(SUPPORTED_LOCALES);
        resolver.setDefaultLocale(properties.getLocale());
        return resolver;
    }

Comment From: ayudovin

Does it make sense to add property like spring.mvc.locale.supported in Locale ?

Comment From: wilkinsona

Not without some other changes to the properties. spring.mvc.locale is a java.util.Locale so we can't nest any properties beneath it.

Properties something like the following might make sense:

  • spring.mvc.locale.default
  • spring.mvc.locale.resolver
  • spring.mvn.locale.supported

However, as noted above, configuring supported locales only makes sense when the resolver is ACCEPTED_HEADER rather than FIXED. We'd also need to figure out how to migrate from the current properties to whatever we want the new ones to be. In short, I think this one needs some further thought.

Comment From: philwebb

We're cleaning out the issue tracker and closing issues that we've not seen much demand to fix. Feel free to comment with additional justifications if you feel that this one should not have been closed.