user6700473
user6700473

Reputation:

Spring Mvc Message Bundle in Java configuration

I use spring 5.1.5. I use Java configuration and the message bundle does not work.

  1. When I wrote it in xml everything worked as soon as the configuration was transferred to Java, the message No message found under code 'jsp.header.entry' for locale 'ru' appeared.
  2. In xml config I can set fileEncodings("UTF-8") but in Java config I didn't know how to do this

A big request to help. Thanks.

It was

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="/WEB-INF/locales/message"/>
        <property name="useCodeAsDefaultMessage" value="false"/>
        <property name="fileEncodings" value="UTF-8"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="cacheSeconds" value="-1"/>
    </bean>

    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang"/>
    </bean>

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
        <property name="defaultLocale" value="ru"/>
    </bean>
</beans>

It became

@Configuration
@EnableWebMvc
@ComponentScan({"com.seeds.controllers", "com.seeds.validators"})
public class WebConfig implements WebMvcConfigurer {

    //Message Source config
    @Bean
    public ReloadableResourceBundleMessageSource getMessageSource(){
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();

        messageSource.setBasename("/WEB-INF/locales/message");
        messageSource.setUseCodeAsDefaultMessage(false);
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setCacheSeconds(-1);
        return messageSource;
    }

    @Bean
    public LocaleChangeInterceptor getLocaleChangeInterceptor(){
        LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();

        localeChangeInterceptor.setParamName("lang");
        return localeChangeInterceptor;
    }

    @Bean
    public SessionLocaleResolver getLocaleResolver(){
        SessionLocaleResolver localeResolver = new SessionLocaleResolver();

        localeResolver.setDefaultLocale(new Locale("ru"));
        return localeResolver;
    }

//others

Upvotes: 0

Views: 1775

Answers (3)

M. Deinum
M. Deinum

Reputation: 124506

Both a MessageSource and LocaleResolver are looked up by name, respectivly messageSource and localeResolver.

When using Java based configuration the name of the method is used as the name of the bean. As you have named them getMessageSource and getLocaleResolver the desired beans aren't found and thus ignored.

To fix, change the names to messageSource and localeResolver respectivly.

@Configuration
@EnableWebMvc
@ComponentScan({"com.seeds.controllers", "com.seeds.validators"})
public class WebConfig implements WebMvcConfigurer {

    //Message Source config
    @Bean
    public ReloadableResourceBundleMessageSource messageSource(){

        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("/WEB-INF/locales/message");
        messageSource.setUseCodeAsDefaultMessage(false);
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setCacheSeconds(-1);
        return messageSource;
    }

    @Bean
    public SessionLocaleResolver localeResolver(){

        SessionLocaleResolver localeResolver = new SessionLocaleResolver();
        localeResolver.setDefaultLocale(new Locale("ru"));
        return localeResolver;
    }

Upvotes: 1

zpavel
zpavel

Reputation: 969

Your Java configuration is good as for encoding. But you implemented WebMvcConfigurer so you need to override addInterceptors method as your LocaleChangeInterceptor will be added to InterceptorRegistry. Please try :

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    localeChangeInterceptor.setParamName("lang");
    registry.addInterceptor(localeChangeInterceptor);
  }

Upvotes: 0

androberz
androberz

Reputation: 934

You could still set the file encodings in the Java config.

messageSource.setFileEncodings(new Properties() {{ put("YourFileName.ru", "your-encoding"); }});

You use the defaultEncoding if no specific encoding is found in the fileEncodings field. Since you don't use a specific encoding for your file, the default "UTF-8" encoding is used.

Upvotes: 0

Related Questions