SpringBoot ResourceBundleMessageSource with external remote files

huangapple go评论96阅读模式
英文:

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;
}

huangapple
  • 本文由 发表于 2020年7月23日 21:15:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63055198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定