Why doesn't Spring use locale `en` when given `en_US`?

2k views Asked by At

Related but not the same as this answer. I have

@Bean
public LocaleResolver localeResolver() {
    AcceptHeaderLocaleResolver ahlr = new AcceptHeaderLocaleResolver();
    ahlr.setDefaultLocale(Locale.ENGLISH);
    return ahlr;
}

And my browser is sending Accept-Language: en-US,en;q=0.9. I have messages_en.properties (but not messages.properties). I get this error

javax.servlet.jsp.JspTagException: No message found under code 'title' for locale 'en_US'.

I know I can fix it by renaming messages_en.properties to messages.properties, but why doesn't Spring Boot find the en locale when given en_US?

I expect this to be a problem when someone has fr_FR locale instead of just fr. Must I really duplicate the locales for every single country?

How can I make Spring smarter to use the language if the specific country locale is not found?

Spring Boot 1.5.13, Spring Core 4.3.17

2

There are 2 answers

0
M. Deinum On BEST ANSWER

For Spring Boot to auto configure a MessageSource it checks the existence of the <basename>.properties. If that exists a MessageSource is configured, else nothing is configured and no messages will be resolved.

To fix add an empty <basename>.properties to src/main/resource to have the MessageSource automatically configured.

0
Chloe On

Adding an empty messages.properties fixed this issue as given in the above comment. Very strange and non-intuitive error message. If the comment is given as an answer, I will accept that.