英文:
SpringBoot ResourceBundleMessageSource with external remote files
问题
我正尝试使用外部文件设置内部化消息源(MessageSource)以用于我的Spring Boot应用程序。
基本上,我的想法是将本地化文件准备就绪(例如存储在AWS S3上或类似位置)。
我尝试使用以下代码:
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource rs = new ResourceBundleMessageSource();
rs.setBasename("http://www.myexternalservice.com/messages");
rs.setUseCodeAsDefaultMessage(true);
return rs;
}
当我尝试检索消息时,我使用以下代码:
String message = messageSource.getMessage("keyname", null, Locale.US);
但在这种情况下,我在调试控制台中看到以下内容:
MessageSource找不到基本名称为http://www.myexternalservice.com/messages的ResourceBundle [http://www.myexternalservice.com/messages],区域设置为en_US。
我还尝试过不同的区域设置。
这可行吗?
英文:
I'm trying to setup the MessageSource for the internalization of my Spring Boot App using external files.
Basically, my idea is to have the localized files out of the box (for example on AWS S3 or something like this).
I'm trying using this code:
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr;
}
@Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource rs = new ResourceBundleMessageSource();
rs.setBasename("http://www.myexternalservice.com/messages");
rs.setUseCodeAsDefaultMessage(true);
return rs;
}
When I try to retrieve a message I use this:
String message = messageSource.getMessage("keyname", null, Locale.US);
But, in this case I read into the debug console this:
ResourceBundle [http://www.myexternalservice.com/messages] not found for MessageSource: Can't find bundle for base name http://www.myexternalservice.com/messages, locale en_US
I tried also with different Locale.
Is it possible?
答案1
得分: 0
我使用了这个代码示例进行解决:
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("http://www.myexternalservice.com/messages");
messageSource.setCacheSeconds(400);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
英文:
I solved using this code example:
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("http://www.myexternalservice.com/messages");
messageSource.setCacheSeconds(400);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论