Skip to content

Fallback locale other than the system locale in AbstractResourceBasedMessageSource #23977

@sonOfRa

Description

@sonOfRa

It's already possible to fallback to the System Locale in, for example ResourceBundleMessageSource. In my current case, I don't want to fall back to the system locale, but I want to fallback to the same Default Locale that the rest of our system uses.

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

@Configuration
public class ResourceConfig implements WebMvcConfigurer {

    private static final Locale DEFAULT_LOCALE = Locale.GERMAN;

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

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

I already attempted this through the MessageSourceAccessor Bean above, but that only seems to configure the default locale when invoking getMessage(...) without specifying an explicit locale manually.

So the behaviour I would expect is, that if I have message files messages_{en,de}.properties, and then a call like messageSourceAccessor.getMessage("message.key", Locale.FRENCH) would return the german string configured for message.key in messages_de.properties

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions