[英]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. 我想基于HttpServletRequest加载一组不同的messages_xx.properties,以根据我们的客户来区分它们。
In templates and through all the application, we have a @Bean which gives the actual customer based on the path of the request 在模板和所有应用程序中,我们都有一个@Bean,它根据请求的路径为实际客户提供
@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 现在,我们在WebConfig
配置MessageSource
,它扩展了WebMvcConfigurerAdapter
,我们想做类似的事情
@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... 因为我们的另一种选择是每种语言仅使用一个文件,并在每个短语的开头使用company.getSlug(),但是我们将删除文件的大小...
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_messages_fr.properties
:
company1.messageKey1=messageCompany1
In company2_messages_fr.properties
: 在company2_messages_fr.properties
:
company2.messageKey1=messageCompany2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.