国际化信息理解

国际化信息也称为本地化信息 。 Java 通过 java.util.Locale 类来表示本地化对象,它通过 “语言类型” 和 “国家/地区” 来创建一个确定的本地化对象 。举个例子吧,比如在发送一个具体的请求的时候,在header中设置一个键值对:"Accept-Language":"zh",通过Accept-Language对应值,服务器就可以决定使用哪一个区域的语言,找到相应的资源文件,格式化处理,然后返回给客户端。

MessageSource

Spring 定义了 MessageSource 接口,用于访问国际化信息。

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

MessageSourceAutoConfiguration

springboot提供了国际化信息自动配置类,配置类中注册了ResourceBundleMessageSource实现类。

 @Configuration
@ConditionalOnMissingBean(value = MessageSource.class, search = SearchStrategy.CURRENT)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@Conditional(ResourceBundleCondition.class)
@EnableConfigurationProperties
public class MessageSourceAutoConfiguration { private static final Resource[] NO_RESOURCES = {}; @Bean
@ConfigurationProperties(prefix = "spring.messages")
public MessageSourceProperties messageSourceProperties() {
return new MessageSourceProperties();
} @Bean
public MessageSource messageSource(MessageSourceProperties properties) {
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;
} protected static class ResourceBundleCondition extends SpringBootCondition { private static ConcurrentReferenceHashMap<String, ConditionOutcome> cache = new ConcurrentReferenceHashMap<>(); @Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) {
String basename = context.getEnvironment()
.getProperty("spring.messages.basename", "messages");
ConditionOutcome outcome = cache.get(basename);
if (outcome == null) {
outcome = getMatchOutcomeForBasename(context, basename);
cache.put(basename, outcome);
}
return outcome;
} private ConditionOutcome getMatchOutcomeForBasename(ConditionContext context,
String basename) {
ConditionMessage.Builder message = ConditionMessage
.forCondition("ResourceBundle");
for (String name : StringUtils.commaDelimitedListToStringArray(
StringUtils.trimAllWhitespace(basename))) {
for (Resource resource : getResources(context.getClassLoader(), name)) {
if (resource.exists()) {
return ConditionOutcome
.match(message.found("bundle").items(resource));
}
}
}
return ConditionOutcome.noMatch(
message.didNotFind("bundle with basename " + basename).atAll());
} private Resource[] getResources(ClassLoader classLoader, String name) {
String target = name.replace('.', '/');
try {
return new PathMatchingResourcePatternResolver(classLoader)
.getResources("classpath*:" + target + ".properties");
}
catch (Exception ex) {
return NO_RESOURCES;
}
} } }

首先MessageSource配置生效依靠一个ResourceBundleCondition条件,从环境变量中读取spring.messages.basename对应的值,默认值是messages,这个值就是MessageSource对应的资源文件名称,资源文件扩展名是.properties,然后通过PathMatchingResourcePatternResolver从“classpath*:”目录下读取对应的资源文件,如果能正常读取到资源文件,则加载配置类。

springmvc自动装配配置类,注册了一个RequestContextFilter过滤器。

每一次请求,LocaleContextHolder都会保存当前请求的本地化信息。

通过MessageSourceAccessor根据code获取具体信息时,如果默认配置的本地化对象为空,则通过LocaleContextHolder获取。

上图的messageSource是应用程序上下文对象(本文创建的是GenericWebApplicationContext实例),该messageSource对象会调用ResourceBundleMessageSource实例获取具体信息。

ValidationAutoConfiguration

参数校验hibernate-validator是通过这个自动装配加载进来的。

 @Configuration
@ConditionalOnClass(ExecutableValidator.class)
@ConditionalOnResource(resources = "classpath:META-INF/services/javax.validation.spi.ValidationProvider")
@Import(PrimaryDefaultValidatorPostProcessor.class)
public class ValidationAutoConfiguration { @Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ConditionalOnMissingBean(Validator.class)
public static LocalValidatorFactoryBean defaultValidator() {
LocalValidatorFactoryBean factoryBean = new LocalValidatorFactoryBean();
MessageInterpolatorFactory interpolatorFactory = new MessageInterpolatorFactory();
factoryBean.setMessageInterpolator(interpolatorFactory.getObject());
return factoryBean;
} @Bean
@ConditionalOnMissingBean
public static MethodValidationPostProcessor methodValidationPostProcessor(
Environment environment, @Lazy Validator validator) {
MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
boolean proxyTargetClass = environment
.getProperty("spring.aop.proxy-target-class", Boolean.class, true);
processor.setProxyTargetClass(proxyTargetClass);
processor.setValidator(validator);
return processor;
} }

MethodValidationPostProcessor这个后置处理处理方法里单个参数校验的注解(JSR和Hibernate validator的校验只能对Object的属性(也就是Bean的域)进行校验,不能对单个的参数进行校验。)。

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值不同,获取到的文本也不同。

Springboot国际化信息(i18n)解析的更多相关文章

  1. 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】

    国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...

  2. 国际化支持(I18N)

    本章译者:@nixil 使用国际化支持(I18N)能够使你的应用根据用户所在地区的不同选择不同的语言.下面介绍如何在引用中使用国际化. 只允许使用UTF-8 Play只支持UTF-8一种字符编码.这是 ...

  3. SpringBoot 国际化配置,SpringBoot Locale 国际化

    SpringBoot 国际化配置,SpringBoot Locale 国际化 ================================ ©Copyright 蕃薯耀 2018年3月27日 ht ...

  4. SpringBoot系列之i18n集成教程

    目录 1.环境搭建 2.resource bundle资源配置 3.LocaleResolver类 4.I18n配置类 5.Thymeleaf集成 SpringBoot系统之i18n国际化语言集成教程 ...

  5. springboot国际化与@valid国际化支持

    springboot国际化 springboot对国际化的支持还是很好的,要实现国际化还简单.主要流程是通过配置springboot的LocaleResolver解析器,当请求打到springboot ...

  6. (spring-第14回【IoC基础篇】)国际化信息

    国际化又称为本地化. 当你把手机的language由中文切换到英文时,你的微信也相应改用英语,这就是i18n国际化.一般来说,应用软件提供一套不同语言的资源文件,放到特定目录中,应用根据不同语言的操作 ...

  7. (spring-第14回【IoC基础篇】)国际化信息 (转)

    国际化又称为本地化. 当你把手机的language由中文切换到英文时,你的微信也相应改用英语,这就是i18n国际化.一般来说,应用软件提供一套不同语言的资源文件,放到特定目录中,应用根据不同语言的操作 ...

  8. Struts2国际化信息机制

    国际化信息机制  (三种 Action范围. Package范围. 全局) 1. 全局国际化配置信息文件 全局国际化文件,对所有Action 生效,任何程序都可以访问到,需要在struts.xml 配 ...

  9. 国际化信息-->MVC

    假设我们正在开发一个支持多国语言的Web应用程序,要求系统能够根据客户端的系统的语言类型返回对应的界面:英文的操作系统返回英文界面,而中文的操作系统则返回中文界面——这便是典型的i18n国际化问题.对 ...

随机推荐

  1. JVM运行时数据区--深入理解Java虚拟机 读后感

    程序计数器 程序计数器是线程私有的区域,很好理解嘛~,每个线程当然得有个计数器记录当前执行到那个指令.占用的内存空间小,可以把它看成是当前线程所执行的字节码的行号指示器.如果线程在执行Java方法,这 ...

  2. requestAnimationFrame 兼容方案

    [toc] 编写涉及:css, html, js 在线演示codepen html代码 <div class="roll-box"> <div class=&qu ...

  3. javaScript基础-01 javascript语法结构

    一.基础 字符集 JavaScript程序是用Unicode字符集编写的. .区分大小写 .空格.换行符和格式控制符 .Unicode转义序列 .标准化 二.类型.值和变量 JavaScript的数据 ...

  4. 《机器学习基石》---VC维

    1 VC维的定义 VC维其实就是第一个break point的之前的样本容量.标准定义是:对一个假设空间,如果存在N个样本能够被假设空间中的h按所有可能的2的N次方种形式分开,则称该假设空间能够把N个 ...

  5. exported function xxx should have comment or be unexported

    0x00 问题 exported function xxx should have comment or be unexported. 0x01 解决 https://golang.org/s/sty ...

  6. jenkins无法连接到git原因

    1.账号密码错误 2.公钥私钥不对应(git上为公钥,jenkins为私钥,私钥比公钥长) 3.公钥私钥文件没有复制到jenkins目录下的.ssh文件中

  7. vue 异步加载远程组件(支持编译less语法)

    本代码已组件化,可以直接使用. 说明:本组件可以直接解析.vue文件,为了支持less语法解析,在组件中引入less.js,可在less官网下载. 组件代码 <template> < ...

  8. webpack4 前端框架基础配置实例-解决css分离图片路径问题

    1.安装nodejs 2. 需要全局和项目安装webpack和webpack-dev-server npm install webpack webpack-dev-server -g npm inst ...

  9. peewee

    字段查看http://docs.peewee-orm.com/en/latest/peewee/models.html#fields 方法使用https://blog.csdn.net/qq_3962 ...

  10. python + selenium webdriver 从主窗口A跳转至主窗口B后,无法定位窗口B的元素的问题

    在做登录脚本的时候,如果只是单纯从登录页面进行元素定位的话,并不存在这个问题 但实际情况是,从首页A进入到登录页面B(并非弹出框),这时候在页面B无法定位到该页面的元素 问题:从页面A进入页面B,无法 ...