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






9527=bojack
come=来吧
hello=你好
testVO.flag.Max=tai大了呀
testVO.flag.Min=tai小了
9527=jack
come=come
hello=hello
testVO.flag.Max=too big
testVO.flag.Min=too small
9527=杰克
come=来吧
hello=你好
testVO.flag.Max=太多了呀
testVO.flag.Min=太小了呀
spring:
messages:
basename: i18n/messages # 多个文件用逗号分隔
/**
* 设置默认语言
* @return
*/
@Bean
public LocaleResolver localeResolver() {
AcceptHeaderLocaleResolver acceptHeaderLocaleResolver = new AcceptHeaderLocaleResolver();
acceptHeaderLocaleResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
return acceptHeaderLocaleResolver;
}
### 简体中文
GET localhost:8080/i18n/test
Accept-Language: zh-CN
### 美国英语
GET localhost:8080/i18n/test
Accept-Language: en-US
package com.springmvc.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@RestController
@RequestMapping("i18n")
public class I18nController {
@Autowired
private MessageSource messageSource;
@GetMapping("test")
public Object test() {
Locale locale = LocaleContextHolder.getLocale();
String displayName = locale.getDisplayName();
System.out.println("displayName = " + displayName);
String hello = messageSource.getMessage("hello", null, locale);
System.out.println("hello = " + hello);
Map<String,Object> map = new HashMap<>();
map.put("hello", hello);
map.put("displayName", displayName);
map.put("locale", locale);
String come= messageSource.getMessage("come", null, locale);
map.put("come", come);
String c9527= messageSource.getMessage("9527", null, locale);
map.put("9527", c9527);
return map ;
}
}
@valid 参数校验与国际化
/**
* The name of the default message bundle.
*/
public static final String DEFAULT_VALIDATION_MESSAGES = "org.hibernate.validator.ValidationMessages";

package com.springmvc.demo.vo;
import lombok.Data;
import javax.validation.constraints.Max;//MethodArgumentNotValidException
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@Data
public class TestVO {
@Min(value = 3)//这里不用写messages了,因为要支持国际化
@Max(value = 10)
private Integer flag;
@NotNull
private Integer age;
}
testVO.flag.Max=too big
testVO.flag.Min=too small
spring:
messages:
basename: i18n/messages
mvc:
message-codes-resolver-format: postfix_error_code
package com.springmvc.demo.controller;
import com.baomidou.mybatisplus.extension.api.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandlerResolver {
@Autowired
MessageSource messageSource;
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
public R handleBodyValidException(MethodArgumentNotValidException exception) {
Map<String, String> errors = new HashMap<String, String>();
//得到所有的属性错误
List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors();
//将其组成键值对的形式存入map
for (FieldError fieldError : fieldErrors) {
String[] str= fieldError.getField().split("\\.");
if(str.length>1){
errors.put(str[1], fieldError.getDefaultMessage());
}else {
errors.put(fieldError.getField(), fieldError.getDefaultMessage());
}
String message = messageSource.getMessage(fieldError, LocaleContextHolder.getLocale());
return R.failed(message);
}
log.error("参数绑定异常,ex = {}", errors);
return R.failed("haha");
}
}
###
GET localhost:8080/i18n/test
Accept-Language: zh
###
GET localhost:8080/i18n/test
Accept-Language: en-US
###
POST localhost:8080/test
Content-Type: application/json
{
"flag": 2,
"age": 22
}
###
POST localhost:8080/test
Content-Type: application/json
Accept-Language: en-US
{
"flag": 5
}
/**
* Parse request locales.
*/
protected void parseLocales() {
localesParsed = true;
// Store the accumulated languages that have been requested in
// a local collection, sorted by the quality value (so we can
// add Locales in descending order). The values will be ArrayLists
// containing the corresponding Locales to be added
TreeMap<Double, ArrayList<Locale>> locales = new TreeMap<>();
Enumeration<String> values = getHeaders("accept-language");
while (values.hasMoreElements()) {
String value = values.nextElement();
parseLocalesHeader(value, locales);
}
// Process the quality values in highest->lowest order (due to
// negating the Double value when creating the key)
for (ArrayList<Locale> list : locales.values()) {
for (Locale locale : list) {
addLocale(locale);
}
}
}
@Override
public final String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException {
String[] codes = resolvable.getCodes();
if (codes != null) {
for (String code : codes) {
String message = getMessageInternal(code, resolvable.getArguments(), locale);
if (message != null) {
return message;
}
}
}
String defaultMessage = getDefaultMessage(resolvable, locale);
if (defaultMessage != null) {
return defaultMessage;
}
throw new NoSuchMessageException(!ObjectUtils.isEmpty(codes) ? codes[codes.length - 1] : "", locale);
}
private String resolveMessage(String message, Locale locale) {
String resolvedMessage = message;
ResourceBundle userResourceBundle = userResourceBundleLocator
.getResourceBundle( locale );
ResourceBundle constraintContributorResourceBundle = contributorResourceBundleLocator
.getResourceBundle( locale );
ResourceBundle defaultResourceBundle = defaultResourceBundleLocator
.getResourceBundle( locale );
String userBundleResolvedMessage;
boolean evaluatedDefaultBundleOnce = false;
do {
// search the user bundle recursive (step 1.1)
userBundleResolvedMessage = interpolateBundleMessage(
resolvedMessage, userResourceBundle, locale, true
);
// search the constraint contributor bundle recursive (only if the user did not define a message)
if ( !hasReplacementTakenPlace( userBundleResolvedMessage, resolvedMessage ) ) {
userBundleResolvedMessage = interpolateBundleMessage(
resolvedMessage, constraintContributorResourceBundle, locale, true
);
}
// exit condition - we have at least tried to validate against the default bundle and there was no
// further replacements
if ( evaluatedDefaultBundleOnce && !hasReplacementTakenPlace( userBundleResolvedMessage, resolvedMessage ) ) {
break;
}
// search the default bundle non recursive (step 1.2)
resolvedMessage = interpolateBundleMessage(
userBundleResolvedMessage,
defaultResourceBundle,
locale,
false
);
evaluatedDefaultBundleOnce = true;
} while ( true );
return resolvedMessage;
}
springboot国际化与@valid国际化支持的更多相关文章
- SpringBoot添加对Log4j2的支持
1.在添加对Log4j2的支持前,需要先把SpringBoot默认使用的Logback日志框架排除,修改pom.xml文件: <dependency> <groupId>org ...
- SpringBoot添加对Mybatis的支持
1.修改maven配置文件pom.xml,添加对mybatis的支持: <dependency> <groupId>org.mybatis.spring.boot</gr ...
- 详解SpringBoot 添加对JSP的支持(附常见坑点)
序言: SpringBoot默认不支持JSP,如果想在项目中使用,需要进行相关初始化工作.为了方便大家更好的开发,本案例可直接作为JSP开发的脚手架工程 SpringBoot+War+JSP . 常见 ...
- JavaWeb开发——软件国际化(文本元素国际化)
前几天围绕着JDBC编程进行了系统的学习.现在我们对Java程序数据库操作已经是轻车熟路了.也学会了使用各种框架来帮助我们简化编程. 今天是学习计划的第七天,虽然学习热情没有前几天高涨了.但是,写博客 ...
- springboot 使用i18n进行国际化
1.i18n介绍 i18n(其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数)是“国际化”的简称.在资讯领域,国际化(i18n)指让产品(出版物,软件,硬 ...
- 逐浪web无障碍与国际化以及全民族语言支持白皮书
北京时间2019年5月10日,领先的门户网站与WEB内核服务厂商--上海Zoomla!逐浪CMS团队发布其年度重榜产品:逐浪CMS全民族语言与国际版,体验站点:http://demo2.z01.com ...
- springboot、Thymeleaf、国际化的简单使用
1.项目体系结构 (1)知识体系 springboot:省去了很多繁琐的配置,如:视图解析器.前端控制器等 thymeleaf:获取controller数据逼能够进行展示 集合:用于存储数据,此练习没 ...
- springboot 使用i18n进行国际化乱码解决
方式1.设置国际化的编码和你使用的编译器(IDEA之类)一致,如编译器为UTF-8则在application配置文件中添加 #i18n spring: messages: encoding: UTF- ...
- 国际化之Android设备支持的语种
昨天发了关于iOS支持的语种,文章最后也补了安卓支持语种列表.但最后发现安卓设备支持跟它列的有出入,我重新完全手工整理了一遍. 我将对应的语种在安卓的语言列表里的显示,也全部逐一列出来了,方便大家到时 ...
随机推荐
- ELK(ElasticSearch+Logstash+Kibana)配置中的一些坑基于7.6版本
三个组件都是采用Docker镜像安装,过程简单不做赘述,直接使用Docker官方镜像运行容器即可,注意三个组件版本必须一致. 运行容器时最好将三个组件的核心配置文件与主机做映射,方便直接在主机修改不用 ...
- Linux入门视频笔记二(Shell)
一.Shell脚本编程基础 1.简单地理解是脚本就是一堆的Linux命令或其他命令,把他们写到一起,打包成一个文件就是脚本,Shell脚本一般以.sh后缀结尾 2.sh text.sh:运行text. ...
- springMVC:校验框架:多规则校验,嵌套校验,分组校验;ssm整合技术
知识点梳理 课堂讲义 学习目标 能够阐述表单验证的分类和区别 能够运用表单验证的常用注解 能够编写表单验证的示例 能够编写SSM整合的应用案例 能够总结SSM整合的步骤 1 校验框架 1.1 入门-视 ...
- Linux性能优化:内存使用情况分析
Blog:博客园 个人 目录 什么是内存 Linux内存回收机制 查看Linux内存情况 查看/proc/meminfo 使用free命令查看 Buffer和Cache Swap 内存泄漏和内存溢出 ...
- 全网最详细的Linux命令系列-cp命令
cp命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在,就会询问是否覆盖,不管你是否使用-i参数.但是如果是 ...
- ASP.NET扩展库之Http日志
最佳实践都告诉我们不要记录请求的详细日志,因为这有安全问题,但在实际开发中,请求的详细内容对于快速定位问题却是非常重要的,有时也是系统的强力证据.Xfrogcn.AspNetCore.Extensio ...
- Dynamics CRM Report安装出错三
需要删除和备份报表服务的密钥集 进入到Micorsoft SQL Server Reporting Services配置管理器 选择"加密密钥",点击"删除" ...
- Dynamics CRM安装教程八:Claims-based认证-外部访问配置(IFD配置)
内部访问配置完成后就剩下最关键的最后一步了,就是外部访问配置,这个配置好以后就可以让非域用户的计算机访问到我们的CRM系统了.言归正传开始进行配置打开CRM服务器的Dynamic CRM部署管理,选择 ...
- Dynamics CRM安装教程七:Claims-based认证-内部访问配置
DFS安装配置好后就要开始配置CRM基于内部认证访问的配置,即使用HTTPS在CRM服务器进行访问的设置.在CRM服务器中找到Dynamic CRM部署管理器,开始菜单选择Dynamic CRM部署管 ...
- OO_Unit4 UML模型化设计总结
OO_Unit4 UML模型化设计总结 任务简介:本单元在介绍了UML中几种基本的模型图元素的基础上,通过实现课程组提供的官方接口来完成自己的UML解析器. 架构设计 本单元最终的整体架构图如下(不包 ...