Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallback locale other than the system locale in AbstractResourceBasedMessageSource #23977

Closed
sonOfRa opened this issue Nov 11, 2019 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@sonOfRa
Copy link

sonOfRa commented Nov 11, 2019

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

@sonOfRa
Copy link
Author

sonOfRa commented Nov 11, 2019

There's a workaround presented in this stackoverflow question: https://stackoverflow.com/questions/49561374/spring-messagesource-fallback-to-explicit-locale.

It kind of works, but I would expect the possibility of a static fallback locale to be working out of the box, instead of requiring a hacky workaround that has to use Exceptions for control flow.

@philwebb philwebb transferred this issue from spring-projects/spring-boot Nov 11, 2019
@philwebb
Copy link
Member

It looks like we'd need some changes in Spring Framework to support this in Spring Boot. I've transferred the issue for that team to consider.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Nov 11, 2019
@rstoyanchev
Copy link
Contributor

So we need setDefaultLocale(Locale) in AbstractResourceBasedMessageSource as an alternative to the setFallbackToSystemLocale. Does that seem right @jhoeller?

@rstoyanchev rstoyanchev added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 13, 2019
@rstoyanchev rstoyanchev added this to the 5.2.2 milestone Nov 13, 2019
@rstoyanchev
Copy link
Contributor

Tentatively scheduled for 5.2.2 as it seems straight forward enough.

@rstoyanchev rstoyanchev changed the title Provide a fallback locale other than the system locale Fallback locale other than the system locale in AbstractResourceBundleMessageSource Nov 13, 2019
@jhoeller jhoeller changed the title Fallback locale other than the system locale in AbstractResourceBundleMessageSource Fallback locale other than the system locale in AbstractResourceBasedMessageSource Nov 13, 2019
@jhoeller jhoeller self-assigned this Nov 13, 2019
@jhoeller
Copy link
Contributor

Indeed, I've introduced a setDefaultLocale(Locale) method along those lines, and a ´getDefaultLocale()` delegate which resolves either such a locally specified Locale or (dynamically) the system Locale, preserving the previous behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants