MarioC
MarioC

Reputation: 3228

Spring boot - different messages.properties based on request

I would like to load a different set of messages_xx.properties based on the HttpServletRequest to differentiate them based on our customers.

In templates and through all the application, we have a @Bean which gives the actual customer based on the path of the request

@Component
public class CompanySelector {

    @Autowired
    private ICompanyService service;

    public String getURLBase(HttpServletRequest request) throws MalformedURLException {
        URL requestURL = new URL(request.getRequestURL().toString());
        String port = requestURL.getPort() == -1 ? "" : ":" + requestURL.getPort();
        return requestURL.getHost() + port;
    }

    public Company getActualCompany(HttpServletRequest request) throws MalformedURLException{
        String url = getURLBase(request);

        Company company = service.findByCompanyUrl(url);
        if(company != null){
            return company;
        }
        return null;
    }

}

Now, we configure the MessageSource in WebConfig which extends WebMvcConfigurerAdapter and we would like to do something like that

@Configuration
@ComponentScan("it.besmart.eshare.web")
public class WebConfig extends WebMvcConfigurerAdapter{

    public WebConfig(){
        super();
    }

    @Autowired
    CompanySelector companySelector;

    @Autowired
    HttpServletRequest request;

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        Company company = null;
        try {
            company = companySelector.getActualCompany(request);
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (company != null){
            messageSource.setBasename("classpath:"+ company.getSlug() + "_messages");
        } else {
            messageSource.setBasename("classpath:messages");
        }
        messageSource.setDefaultEncoding("UTF-8");

        return messageSource;
    }

}

but obviously it doesn't work because we don't have a request during configuration... Is there another way to load the messages file based on the request? Or any other best practice to adopt? Because our other choice would be using only one file per language and using the company.getSlug() at the beginning of each phrase, but we would decuplicate the size of file...

Upvotes: 0

Views: 5649

Answers (1)

Sébastien Temprado
Sébastien Temprado

Reputation: 1463

You need to declare every properties files like that:

@Bean
public MessageSource messageSource() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasenames("company1_messages", "company2_messages");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

And to get the message:

@Autowired
private MessageSource messageSource;

public String myRequest(Locale locale) {
    ...
    messageSource.getMessage(company.getSlug().".messageKey1", null, locale));
    ... 
}

In company1_messages_fr.properties:

company1.messageKey1=messageCompany1

In company2_messages_fr.properties:

company2.messageKey1=messageCompany2

Upvotes: 2

Related Questions