I have problems with the resolution of i18n in my spring boot application. I have this configuration:
LocaleConfiguration.java Here I set Locale.US like default language, set the message directory to "src/main/resources/messages" and use locale param to change the language.
package com.myproject.web.config;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class LocaleConfiguration extends WebMvcConfigurerAdapter {
/**
* Este bean se encargará de resolver que idioma (locale) esta siendo usado
* actualmente
*
* @return
*/
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
/**
* Este bean se encargara de interceptar el locale que venga a través del
* parámetro locale de la url
*
* @return
*/
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("locale");
return lci;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
/**
* Registramos los filtros
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
My messages files:
/src/main/resources/
----/messages/
-------->messages_es_ES.properties (Spanish message file)
-------->messages.properties (Default messages file. US)
Whey I enter in my home, my app shows:
??home.welcome_en_US??
Looks like can't resolve message files (not even default file!). I should missed something but I can't find. Any help with this?
Solution
Problem here was the Basename path that I use, I have to define the full path in the classpath without the locale suffix and the file extension (For my use, this):
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
This will search in /src/main/resources/ --> classpath messages/messages --> basename And then add the required suffix ("es_ES", "en_US"..) and the file extension ".properties"
For any similar problem, This was the solution
Problem here was the Basename path that I use, I have to define the full path in the classpath without the locale suffix and the file extension (For my use, this):
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
This will search in
/src/main/resources/ --> classpath
messages/messages --> basename
And then add the required suffix ("es_ES", "en_US"..)
and the file extension ".properties"