ElPiter
ElPiter

Reputation: 4324

Spring i18n - Confusing default locales

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?

Upvotes: 3

Views: 2350

Answers (1)

ElPiter
ElPiter

Reputation: 4324

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.

Upvotes: 3

Related Questions