Spring Boot默认支持国际化配置,只需要添加配置文件即可。
(1)配置文件
放在/src/main/resources/文件夹下即可
- messages.properties 默认
- messages_en.properties(英文)
- messages_zh.properties(中文)
- messages_ja.properties(日文)
(2)默认配置
引用
spring.messages.always-use-message-format=false # Set whether to always apply the MessageFormat rules, parsing even messages without arguments.
spring.messages.basename=messages # Comma-separated list of basenames, each following the ResourceBundle convention.
spring.messages.cache-seconds=-1 # Loaded resource bundle files cache expiration, in seconds. When set to -1, bundles are cached forever.
spring.messages.encoding=UTF-8 # Message bundles encoding.
spring.messages.fallback-to-system-locale=true # Set whether to fall back to the system Locale if no files for a specific Locale have been found.
修改spring.messages.basename的配置:
引用
spring.messages.basename=i18n/messages_common,i18n/messages_api,i18n/messages_admin
以下文件将都会被加载
- /src/main/resources/i18n/messages_common.properties
- /src/main/resources/i18n/messages_api.properties
- /src/main/resources/i18n/messages_admin.properties
(3)页面显示(Thymeleaf)
/src/main/resources/hello.html
<h1 th:text="#{search.condition}"></h1>
(4)代码中使用
@Autowired
private MessageSource messageSource;
String msg = messageSource.getMessage("search.condition", null, locale);
a- 获取request的Locale
Controller参数注入
@RequestMapping("/sample")
public String sample(Model model, Locale locale) {
String message = messageSource.getMessage("search.condition", null, locale);
log.info(message);
return "sample";
}
代码获取
Locale locale1 = LocaleContextHolder.getLocale(); // 当前request
Locale locale2 = RequestContextUtils.getLocale(request); // 指定request
b- 通用的使用
@Component
public class MessageManager {
private static MessageSource messageSource;
public static String getMsg(String key) {
Locale locale = LocaleContextHolder.getLocale();
return messageSource.getMessage(key, null, locale);
}
public static String getMsg(String key, String... arg) {
Locale locale = LocaleContextHolder.getLocale();
Object[] args = new Object[arg.length];
for (int i = 0; i < arg.length; i++) {
args[i] = arg[i];
}
return messageSource.getMessage(key, args, locale);
}
@Autowired(required = true)
public void setMessageSource(MessageSource messageSource) {
MessageManager.messageSource = messageSource;
}
}
String msg = MessageManager.getMsg("search.condition");
(5)设置验证信息Validator
Hibernate Validator 和 JSR 303 validation API的提示信息,默认是ValidationMessages.properties。亦可以统一到messages.properties里。
@Configuration
public class MessageConfig extends WebMvcConfigurerAdapter {
@Autowired
private MessageSource messageSource;
@Bean
public LocalValidatorFactoryBean validator() {
LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
localValidatorFactoryBean.setValidationMessageSource(messageSource);
return localValidatorFactoryBean;
}
@Override
public Validator getValidator() {
return validator();
}
}
(6)设置语言
默认根据浏览器的语言设置来决定显示语言AcceptHeaderLocaleResolver。一般应用多采用会话级别的语言设置SessionLocaleResolver。
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
sessionLocaleResolver.setDefaultLocale(Locale.CHINA);
return sessionLocaleResolver;
}
(7)切换语言
a- 通过Interceptor
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
URL中通过参数lang就可以切换到不同语言。比如:http://localhost:8080/hello.html?lang=zh
b- 代码内设置语言
Locale locale = Locale.CHINA;
LocaleContextHolder.setLocale(locale);
request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);
分享到:
相关推荐
NULL 博文链接:https://rensanning.iteye.com/blog/2357171
00、尚硅谷_SpringBoot_源码、课件 01、尚硅谷_SpringBoot_入门-课程简介 02、尚硅谷_SpringBoot_入门-Spring Boot简介 03、尚硅谷_SpringBoot_入门-微服务简介 04、尚硅谷_SpringBoot_入门-环境准备 05、尚硅谷_...
---Day01SpringBoot基础重温篇.mp4 ---Day02SpringBoot核心原理篇.mp4 ---Day03SpringBoot完美收官篇.mp4 优秀作业名单 ---Day01优秀作业名单(排名不分先后).txt ---Day02优秀作业名单(排名不分先后).txt 源码和软件...
其对于Spring Boot的最常用的第一个小程序、JavaConfig和常见注解、启动改变、热部署、Springboot整合Thymeleaf/freemarker/JSP、异常处理、配置文件、日志、国际化等都使用案例进行了说明,并且在最后通过一个贯穿...
一、Spring Boot 入门 1、Spring Boot 简介 简化Spring应用开发的一个框架; 整个Spring技术栈的一个大整合; J2EE开发的一站式解决方案; 2、微服务 2014,martin fowler 微服务:架构风格(服务微化) 一个应用...
主要给大家介绍了关于Spring boot的相关资料,本文属于基础入门教程,对各位学习Spring boot的新手们具有一定的参考学习价值,,要的朋友们下面来一起看看吧。
推荐工具:-OpenWrite:Markdown微信编辑器是一种... 二,系列文章目录『Spring Boot 2快速教程』『基础-入门篇』『基础-Web业务开发篇』Spring Boot使用Swagger2构建RESRful API文档Spring Boot集成JSP 弹簧靴集成胸腺
Spring Boot入门 Spring Boot配置 Spring Boot与日志 Spring Boot与Web开发 Spring Boot与Docker Spring Boot与数据访问 Spring Boot启动配置原理
入门/基础 - 在本文中,我们将学习如何使用 Spring MVC 5 +、JSP、Maven 构建工具和 Eclipse IDE 创建一个简单的 Hello World Spring MVC 应用程序 - 在本文中,我们将学习如何使用 Spring MVC 5+、Maven 构建工具、...
Spring启动教程项目简介Spring Boot Tutorial是一个入门级的教程,主要包含Spring Boot的使用,以及如何集成各种第三方组件等内容。比较适合初学Spring Boot的同学们。博客:本系列教程PDF:准备中,尽请期待...目录...
包含:socket网络编程及实战,系统学习docker,看的见的算法 7个经典应用诠释算法精髓,一站式学习Redis 从入门到高可用分布式实践,Spring Boot 2.0深度实践之核心技术篇,Scala 学习 进击大数据Spark生态圈,剑指Java面试...
35、尚硅谷_SpringBoot_web开发-【实验】-国际化 36、尚硅谷_SpringBoot_web开发-【实验】-登陆&拦截器 37、尚硅谷_SpringBoot_web开发-【实验】-Restful实验要求 38、尚硅谷_SpringBoot_web开发-【实验】-员工...
小编 做的这个项目有部分功能用到了 Activiti工作流,目前对工作流的掌握程度是基本会用,我之前写过一篇 Activiti 入门篇,当前这篇文章是属于Activiti 实战部分,如果你想知道Activiti 工作流是如何使用的话,那就...
本文使用Spring Cloud Netflix作为服务治理基础,通篇穿插C4模型,侧重以最简练的方式,向大家展示如何使用TCC解决多元化事务。变体在微服务兴起的时候,由于对集团内部现有的中间件生态考量与性能的实质需求,更多...
这个例子展示了如何将Angular和Spring Boot部署到Heroku。 它还显示了如何使用ng deploy将Angular部署到Firebase,Netlify和AWS S3。 请阅读以了解如何创建此应用程序。 这篇博客文章是系列文章的第3部分。 本系列...
这两个基本上是主要问的Java框架: 框架这部分包含很多,入门框架有Spring、Spring MVC、Mybatis,目前SSH很少使用了,微服务生态这边有Spring Boot、Spring Cloud分布式:市面上流行的分布式框架,分布式缓存,消息...