3

Using a very simple Spring Rest HelloWorld example with intelliJ, I am trying to add internationalization (Locale.US as the default and just an alternative messages_es.properties to get the version in Spanish).

This is my Java Config file:

@Bean
public LocaleResolver localeResolver(){
    SessionLocaleResolver localeResolver = new SessionLocaleResolver();
    localeResolver.setDefaultLocale(Locale.US);
    return localeResolver;
}

@Bean
public ResourceBundleMessageSource messageSource(){
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

Then I have two resources with just an entry each:

messages.properties (default):

good.morning.message=Good morning

messages_es.properties (default):

good.morning.message=Buenos días

Testing it from Postman with "Accept-Language" header parameter, no matter what value I set on it, it always returns the version with the words in Spanish ("Buenos días"). The default English version is never returned even setting "en", "en_GB" or "en_US", among others. Always "Buenos días".

However, if I just change Spanish to French or other languages, i.e., I rename the messages_es.properties to messages_fr.properties, it works correctly. It returns "Good Morning" always except when I send "fr" in the header parameter, in which case it returns "Bonjour" or whatever text I set in the French version.

Any idea?

ElPiter
  • 4,046
  • 9
  • 51
  • 80
  • Did you implement LocaleChangeInterceptor and added it to registry? https://www.baeldung.com/spring-boot-internationalization – bkbb Sep 19 '19 at 23:53
  • Nope... I already solved it. Check my own answer... (coming up) – ElPiter Sep 20 '19 at 19:33

1 Answers1

3

I already solved it. It was a system language configuration. I have my Mac configured in Spanish. Spring was taking ES Locale as the default one.

When I changed to English and restarted the application, it did take English as the default language.

I wonder if there's something that can be done to skip the system configuration so just the Spring configuration tells what language is the actual one.

ElPiter
  • 4,046
  • 9
  • 51
  • 80
  • 2
    May be JVM option ```java -Duser.country=ES -Duser.language=es ...``` – bkbb Sep 20 '19 at 19:46
  • 1
    OMG MAN!!! I'm CRY! THANK YOU! My A* burned so hart today... Ok, telling you what I had today. I new in Spring Boot with Thymeleaf and had bug similar like you. I follow this tutorial https://www.baeldung.com/spring-boot-internationalization I create EN as default lang and RU, FR like additional. When I go to */* I got RU lang that confused me 'cause I set EN as default lang. When go to */?lang=FR* all is OK site display me FR lang. But when I go to */?lang=en* I got RU! *** So guys add `-Duser.country=EN -Duser.language=en` to run configs 'cause my fu** MacOS use RU lang and broke me GJ Mate – Стас Пишевский May 06 '20 at 19:51