drenda
drenda

Reputation: 6254

Enum conversion doesn't fallback to rest-messages

I'm using Spring Boot 1.5.8, Spring Data REST, Spring HATEOAS. In my application exposing REST endpoints I enabled:

spring.data.rest.enable-enum-translation=true

In this way when I ask for an enum it is translated acconding to my locale.

Some more configuration stuff:

    @Bean
    public LocaleResolver localeResolver() {
        return new SmartLocaleResolver();
    }

    public class SmartLocaleResolver extends CookieLocaleResolver {
        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            String acceptLanguage = request.getHeader("Accept-Language");
            if (acceptLanguage == null || acceptLanguage.trim().isEmpty()) {
                return super.determineDefaultLocale(request);
            }
            return request.getLocale();
        }
    }

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:/i18n/messages");
        // messageSource.setDefaultEncoding("UTF-8");
        // set to true only for debugging
        messageSource.setUseCodeAsDefaultMessage(false);
        messageSource.setCacheSeconds((int) TimeUnit.HOURS.toSeconds(1));
        messageSource.setFallbackToSystemLocale(false);
        return messageSource;
    }

    @Bean
    public MessageSourceAccessor messageSourceAccessor() {
        return new MessageSourceAccessor(messageSource());
    }

As you can see I set also message source in order to translate also exceptions coming from the server.

My server locale is it-IT and I've rest-messages.properties (US translation) and rest-messages_it.properties (IT translation). My goal is to use rest-messages.properties when the language is not recognized and rest-messages_it.properties when the language is IT.

Right now it doesn't work. Spring Data REST read rest-messages_it.properties when there isn't a corrispondent file for the language selected.

I solved this problem with messages.properties using messageSource.setFallbackToSystemLocale(false);. Is there a way to do the same thing for rest-messages files?

Upvotes: 0

Views: 306

Answers (1)

Cepr0
Cepr0

Reputation: 30449

What if you subclass the RepositoryRestMvcConfiguration, override and copy its method resourceDescriptionMessageSourceAccessor, but set fallbackToSystemLocale to false for messageSource?

@Override
@Bean
public MessageSourceAccessor resourceDescriptionMessageSourceAccessor() {

    try {

        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("rest-default-messages.properties"));
        propertiesFactoryBean.afterPropertiesSet();

        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:rest-messages");
        messageSource.setCommonMessages(propertiesFactoryBean.getObject());
        messageSource.setDefaultEncoding("UTF-8");

        // Adding this line:
        messageSource.setFallbackToSystemLocale(false);

        return new MessageSourceAccessor(messageSource);

    } catch (Exception o_O) {
        throw new BeanCreationException("resourceDescriptionMessageSourceAccessor", "", o_O);
    }
}

And what if you create the rest-default-messages.properties file with values for the default locale?..


Update from the question author

To preserve spring.data.rest.* properties it's necessary to create a RepositoryRestConfiguration Bean as described in this post:

@Bean
@ConfigurationProperties(prefix = "spring.data.rest")
@Override
public RepositoryRestConfiguration config() {
    return super.config();
}

Upvotes: 1

Related Questions