Springboot國際化信息(i18n)解析

國際化信息理解

國際化信息也稱爲本地化信息 。 Java 經過 java.util.Locale 類來表示本地化對象,它經過 「語言類型」 和 「國家/地區」 來建立一個肯定的本地化對象 。舉個例子吧,好比在發送一個具體的請求的時候,在header中設置一個鍵值對:"Accept-Language":"zh",經過Accept-Language對應值,服務器就能夠決定使用哪個區域的語言,找到相應的資源文件,格式化處理,而後返回給客戶端。java

MessageSource

Spring 定義了 MessageSource 接口,用於訪問國際化信息。spring

  • getMessage(String code, Object[] args, String defaultMessage, Locale locale)
  • getMessage(String code, Object[] args, Locale locale)
  • getMessage(MessageSourceResolvable resolvable, Locale locale)

 

 MessageSourceAutoConfiguration

 springboot提供了國際化信息自動配置類,配置類中註冊了ResourceBundleMessageSource實現類。springboot

 1 @Configuration
 2 @ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
 3 @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
 4 @Conditional(ResourceBundleCondition.class)
 5 @EnableConfigurationProperties
 6 public class MessageSourceAutoConfiguration {
 7 
 8     private static final Resource[] NO_RESOURCES = {};
 9 
10     @Bean
11     @ConfigurationProperties(prefix = "spring.messages")
12     public MessageSourceProperties messageSourceProperties() {
13         return new MessageSourceProperties();
14     }
15 
16     @Bean
17     public MessageSource messageSource(MessageSourceProperties properties) {
18         ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
19         if (StringUtils.hasText(properties.getBasename())) {
20             messageSource.setBasenames(StringUtils.commaDelimitedListToStringArray(
21                     StringUtils.trimAllWhitespace(properties.getBasename())));
22         }
23         if (properties.getEncoding() != null) {
24             messageSource.setDefaultEncoding(properties.getEncoding().name());
25         }
26         messageSource.setFallbackToSystemLocale(properties.isFallbackToSystemLocale());
27         Duration cacheDuration = properties.getCacheDuration();
28         if (cacheDuration != null) {
29             messageSource.setCacheMillis(cacheDuration.toMillis());
30         }
31         messageSource.setAlwaysUseMessageFormat(properties.isAlwaysUseMessageFormat());
32         messageSource.setUseCodeAsDefaultMessage(properties.isUseCodeAsDefaultMessage());
33         return messageSource;
34     }
35 
36     protected static class ResourceBundleCondition extends SpringBootCondition {
37 
38         private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>();
39 
40         @Override
41         public ConditionOutcome getMatchOutcome(ConditionContext context,
42                 AnnotatedTypeMetadata metadata) {
43             String basename = context.getEnvironment()
44                     .getProperty("spring.messages.basename", "messages");
45             ConditionOutcome outcome = cache.get(basename);
46             if (outcome == null) {
47                 outcome = getMatchOutcomeForBasename(context, basename);
48                 cache.put(basename, outcome);
49             }
50             return outcome;
51         }
52 
53         private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context,
54                 String basename) {
55             ConditionMessage.Builder message = ConditionMessage
56                     .forCondition("ResourceBundle");
57             for (String name : StringUtils.commaDelimitedListToStringArray(
58                     StringUtils.trimAllWhitespace(basename))) {
59                 for (Resource resource : getResources(context.getClassLoader(), name)) {
60                     if (resource.exists()) {
61                         return ConditionOutcome
62                                 .match(message.found("bundle").items(resource));
63                     }
64                 }
65             }
66             return ConditionOutcome.noMatch(
67                     message.didNotFind("bundle with basename " + basename).atAll());
68         }
69 
70         private Resource[] getResources(ClassLoader classLoader, String name) {
71             String target = name.replace('.', '/');
72             try {
73                 return new PathMatchingResourcePatternResolver(classLoader)
74                         .getResources("classpath*:" + target + ".properties");
75             }
76             catch (Exception ex) {
77                 return NO_RESOURCES;
78             }
79         }
80 
81     }
82 
83 }
View Code

首先MessageSource配置生效依靠一個ResourceBundleCondition條件,從環境變量中讀取spring.messages.basename對應的值,默認值是messages,這個值就是MessageSource對應的資源文件名稱,資源文件擴展名是.properties,而後經過PathMatchingResourcePatternResolver從「classpath*:」目錄下讀取對應的資源文件,若是能正常讀取到資源文件,則加載配置類。服務器

 

 springmvc自動裝配配置類,註冊了一個RequestContextFilter過濾器。mvc

 每一次請求,LocaleContextHolder都會保存當前請求的本地化信息。ide

 經過MessageSourceAccessor根據code獲取具體信息時,若是默認配置的本地化對象爲空,則經過LocaleContextHolder獲取。測試

 上圖的messageSource是應用程序上下文對象(本文建立的是GenericWebApplicationContext實例),該messageSource對象會調用ResourceBundleMessageSource實例獲取具體信息。ui

ValidationAutoConfiguration

參數校驗hibernate-validator是經過這個自動裝配加載進來的。spa

 1 @Configuration
 2 @ConditionalOnClass(ExecutableValidator.class)
 3 @ConditionalOnResource(resources = "classpath:META-INF/services/javax.validation.spi.ValidationProvider")
 4 @Import(PrimaryDefaultValidatorPostProcessor.class)
 5 public class ValidationAutoConfiguration {
 6 
 7     @Bean
 8     @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
 9     @ConditionalOnMissingBean(Validator.class)
10     public static LocalValidatorFactoryBean defaultValidator() {
11         LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
12         MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
13         factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
14         return factoryBean;
15     }
16 
17     @Bean
18     @ConditionalOnMissingBean
19     public static MethodValidationPostProcessor methodValidationPostProcessor(
20             Environment environment, @Lazy Validator validator) {
21         MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
22         boolean proxyTargetClass = environment
23                 .getProperty("spring.aop.proxy-target-class", Boolean.class, true);
24         processor.setProxyTargetClass(proxyTargetClass);
25         processor.setValidator(validator);
26         return processor;
27     }
28 
29 }
View Code

MethodValidationPostProcessor這個後置處理處理方法裏單個參數校驗的註解(JSR和Hibernate validator的校驗只能對Object的屬性(也就是Bean的域)進行校驗,不能對單個的參數進行校驗。)。hibernate

LocalValidatorFactoryBean實現了javax.validation.ValidatorFactory和javax.validation.Validator這兩個接口,以及Spring的org.springframework.validation.Validator接口,你能夠將這些接口當中的任意一個注入到須要調用驗證邏輯的Bean裏。

 默認狀況下,LocalValidatorFactoryBean建立的validator使用PlatformResourceBundleLocator獲取資源的綁定關係,獲取的資源名稱是:ValidationMessages

 用戶自定義的校驗信息放在項目classpath目錄下。

另外hibernate-validator還會加載默認的校驗資源文件,名稱是:org.hibernate.validator.ValidationMessages。能夠看到,默認的校驗資源捆綁文件包含了不一樣區域的信息的配置。

經過LocalValidatorFactoryBean獲取的validator是如何根據不一樣的地區加載不一樣校驗資源文件呢?hibernate-validator暴露了一個消息插補器(MessageInterpolator),spring正是從新代理這個消息插補器。

 經過LocaleContextMessageInterpolator源碼,能夠看到最終仍是經過LocaleContextHolder獲取當前時區信息。

 

是否能夠自定義國際化校驗的資源信息呢?固然是確定的,咱們只須要重寫LocalValidatorFactoryBean類型bean的建立過程,經過setValidationMessageSource方法指定自定義的資源信息。

MessageSource測試

基礎測試

創建Resouce bundle messages

編寫message source測試方法,從request中獲取當前Locale值

 編寫測試類,指定當前請求的Locale值或者設置請求頭的header值:Accept-Language:zh

根據測試類中請求的Locale值不一樣,獲取到的文本也不一樣。

格式化測試

創建Resouce bundle messages

編寫message source測試方法,從request中獲取當前Locale值

 編寫測試類,指定當前請求的Locale值或者設置請求頭的header值:Accept-Language:zh

 

根據測試類中請求的Locale值不一樣,獲取到的格式化的文本也不一樣。

靜態message source測試

動態註冊message(可區分Locale),可用於自定義message source。

編寫測試的方法,經過MessageSourceAccessor訪問。

 編寫測試類,獲取自定義message source中的信息。

根據測試類中請求的Locale值不一樣,獲取到的文本也不一樣。

相關文章
相關標籤/搜索