springboot-国际化

https://blog.csdn.net/liujun03/article/details/82775634

一、国际化基本原理
在Spring程序中,国际化主要是通过ResourceBundleMessageSource这个类来实现的,那么下面我们分析一下Spring Boot是如何实现国际化支持的。
Spring Boot通过MessageSourceAutoConfiguration是为我们自动配置好了管理国际化资源文件的组件的: org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration

@Bean
@ConfigurationProperties(
    prefix = "spring.messages"
)
public MessageSourceProperties messageSourceProperties() {
    return new MessageSourceProperties();
}

@Bean
public MessageSource messageSource() {
    MessageSourceProperties properties = this.messageSourceProperties(); //private String basename = "messages";
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    if (StringUtils.hasText(properties.getBasename())) {
        messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(StringUtils.trimAllWhitespace(properties.getBasename())));
    }

    if (properties.getEncoding() != null) {
        messageSource.setDefaultEncoding(properties.getEncoding().name());
    }

    messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
    Duration cacheDuration = properties.getCacheDuration();
    if (cacheDuration != null) {
        messageSource.setCacheMillis(cacheDuration.toMillis());
    }

    messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
    messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
    return messageSource;
}

 

posted @ 2019-05-07 14:25  絵飛ヾ的魚  阅读(212)  评论(0编辑  收藏  举报