SpringBoot整合国际化功能
(1)、编写国际化配置文件
在resources下新建i18n文件夹,并新建以下文件
①index.properties
username=username
②index_en_US.properties
username=username
③index_zh_CN.properties
username=用户名
(2)、使用ResourceBundleMessageSource管理国际化资源文件
*SpringBoot已经自动配置了管理国际化资源文件的组件

(3)在配置文件中指定国际化资源文件的文件夹及基础文件
#指定国际化资源文件的文件夹及基础文件 spring.messages.basename=i18n/index
(4)* 编写自定义的Locale区域解析器
package cn.coreqi.config; import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale; /**
* SpringBoot默认的Locale解析器是根据请求头的区域信息进行解析的(浏览器语言)
* 使用自定义的Locale解析器对url的区域信息进行解析达到点击切换区域效果
* 一旦我们自定义的区域解析器注册到Spring容器中,则SpringBoot提供的将不自动注册
*/
public class MyLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String l = httpServletRequest.getParameter("l");
if(!StringUtils.isEmpty((l))){
String [] s = l.split("_");
return new Locale(s[0],s[1]);
}
return Locale.getDefault();
} @Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) { }
}
(5)注册我们自定义的区域解析器
package cn.coreqi.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /**
* 扩展SpringMVC
* SpringBoot2使用的Spring5,因此将WebMvcConfigurerAdapter改为WebMvcConfigurer
* 使用WebMvcConfigurer扩展SpringMVC好处既保留了SpringBoot的自动配置,又能用到我们自己的配置
*/
//@EnableWebMvc //如果我们需要全面接管SpringBoot中的SpringMVC配置则开启此注解,
//开启后,SpringMVC的自动配置将会失效。
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//设置对“/”的请求映射到index
//如果没有数据返回到页面,没有必要用控制器方法对请求进行映射
registry.addViewController("/").setViewName("index");
} //注册我们自定义的区域解析器,一旦将我们的区域解析器注册到Spring容器中则SpingBoot
//默认提供的区域解析器将不会自动注册
@Bean
public LocaleResolver localeResolver(){
return new MyLocaleResolver();
}
}
(6)视图中引用国际化内容
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Index首页</title>
</head>
<body>
<h1 th:text="#{username}"></h1>
</body>
</html>
(7)测试


SpringBoot整合国际化功能的更多相关文章
- springboot整合Shiro功能案例
Shiro 核心功能案例讲解 基于SpringBoot 有源码 从实战中学习Shiro的用法.本章使用SpringBoot快速搭建项目.整合SiteMesh框架布局页面.整合Shiro框架实现用身份认 ...
- SpringBoot整合国际化I18n
本文主要实现的功能: 从文件夹中直接加载多个国际化文件 后台设置前端页面显示国际化信息的文件 实现 国际化项目初始化,简单看下项目的目录和文件 在resource下创建国际化文件 messages.p ...
- springboot整合ueditor实现图片上传和文件上传功能
springboot整合ueditor实现图片上传和文件上传功能 写在前面: 在阅读本篇之前,请先按照我的这篇随笔完成对ueditor的前期配置工作: springboot+layui 整合百度富文本 ...
- SpringBoot整合Redis使用Restful风格实现CRUD功能
前言 本篇文章主要介绍的是SpringBoot整合Redis,使用Restful风格实现的CRUD功能. Redis 介绍 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-valu ...
- SpringBoot 整合Mail发送功能问题与解决
SpringBootLean 是对springboot学习与研究项目,是根据实际项目的形式对进行配置与处理,欢迎star与fork. [oschina 地址] http://git.oschina.n ...
- 功能:SpringBoot整合rabbitmq,长篇幅超详细
SpringBoot整合rabbitMq 一.介绍 消息队列(Message Queue)简称mq,本文将介绍SpringBoot整合rabbitmq的功能使用 队列是一种数据结构,就像排队一样,遵循 ...
- SpringBoot整合Redis实现常用功能
SpringBoot整合Redis实现常用功能 建议大小伙们,在写业务的时候,提前画好流程图,思路会清晰很多. 文末有解决缓存穿透和击穿的通用工具类. 1 登陆功能 我想,登陆功能是每个项目必备的功能 ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- SpringBoot 整合thymeleaf
1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...
随机推荐
- 自学Linux Shell12.3-case命令
点击返回 自学Linux命令行与Shell脚本之路 12.3-case命令 有了case命令,就不需要写出所有elif语句来不停的检查同一个变量的值了.case命令会采用列表格式来检查单个变量的多个值 ...
- [luogu4513]小白逛公园
题目描述 在小新家附近有一条"公园路",路的一边从南到北依次排着n个公园,小白早就看花了眼,自己也不清楚该去哪些公园玩了. 一开始,小白就根据公园的风景给每个公园打了分-.-.小新 ...
- 批量修改sharepoint 2013站点里区域设置
cls [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") foreach ($we ...
- 【转】安全加密(五):如何使用AES防止固件泄露
本文导读 随着电子产品更新换代速度的加快,往往都会进行系统升级或APP功能维护升级,但是由此产生了两个主要问题.首先,由于更新过程中出现错误,该设备可能变得无用:另外一个主要问题是:如何避免未经授权的 ...
- python 线程,进程28原则
基于函数实现 from threading import Thread def fun(data, *args, **kwargs): """ :param data: ...
- install ubuntu env
install ubuntu1, mysql serversudo apt-get install mysql-server2, ssh sudo apt-get install openssh-se ...
- 【POJ2248】加法链 idfs
首先,在这道题的搜索框架中,在对每一位进行枚举时,复杂度为\(O(n^2)\),但是可知最优解序列的长度不会太长. 其次,采用 \(bool\) 类型返回值时,是一种存在性搜索,并不一定能够得到最优解 ...
- 第二节,TensorFlow 使用前馈神经网络实现手写数字识别
一 感知器 感知器学习笔记:https://blog.csdn.net/liyuanbhu/article/details/51622695 感知器(Perceptron)是二分类的线性分类模型,其输 ...
- 盘点 php 里面那些冷门又实用的小技巧
1.实用某个字段索引二维数组 取出一个数组的一个字段的值的数组,我们可以使用 array_column, 这个方法还有另外一个用法,如 array_column($array, null, 'key' ...
- 警告: No data sources are configured to run this SQL and provide advanced code assistance. Disable this inspection via problem menu (Alt+Enter). more... (Ctrl+F1) SQL dialect is not configured. Postgr
python3出现问题: 警告: No data sources are configured to run this SQL and provide advanced code assistance ...