springBoot 实现中文国际化
一:实现效果如下:


二 SpringBoot 国际化配置
1、创建国际化配置文件(3个):
messages.properties
messages.user.name=用户名
messages.user.password=密码
messages.user.btn=登录
messages_en_US.properties
messages.user.name=UserName
messages.user.password=Password
messages.user.btn=Sign In
messages.keyword=keyword
messages_zh_CN.properties
messages.user.name=\u7528\u6237\u540d
messages.user.password=\u5bc6\u7801
messages.user.btn=\u767b\u5f55
messages.keyword=\u5173\u952e\u8bcd
SpringBoot默认国际化文件为:classpath:message.properties,如果放在其它文件夹中,则需要在application.properties配置属性spring.messages.basename:
#表示放在classpath的i18n文件夹,文件前缀为mess
spring.messages.basename=i18n/messages
2.自定义国际化语言解析器
package com.joyny.ecas.config; import org.springframework.web.servlet.LocaleResolver;
import org.thymeleaf.util.StringUtils; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale; /**
* 自定义国际化语言解析器
*
*/
public class MyLocaleResolver implements LocaleResolver { private static final String I18N_LANGUAGE = "i18n_language";
private static final String I18N_LANGUAGE_SESSION = "i18n_language_session"; @Override
public Locale resolveLocale(HttpServletRequest req) {
String i18n_language = req.getParameter(I18N_LANGUAGE);
Locale locale = Locale.getDefault();
if(!StringUtils.isEmpty(i18n_language)) {
String[] language = i18n_language.split("_");
locale = new Locale(language[0], language[1]); //将国际化语言保存到session
HttpSession session = req.getSession();
session.setAttribute(I18N_LANGUAGE_SESSION, locale);
}else {
//如果没有带国际化参数,则判断session有没有保存,有保存,则使用保存的,也就是之前设置的,避免之后的请求不带国际化参数造成语言显示不对
HttpSession session = req.getSession();
Locale localeInSession = (Locale) session.getAttribute(I18N_LANGUAGE_SESSION);
if(localeInSession != null) {
locale = localeInSession;
}
}
return locale;
} @Override
public void setLocale(HttpServletRequest req, HttpServletResponse res, Locale locale) { } }
3、把国际化语言解析器放到Spring容器中:
package com.joyny.ecas.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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//WebMvcConfigurerAdapter
//使用WebMvcConfigurerAdapter可以扩展SpringMvc的功能,包括拦截器,转换器等
//@EnableWebMvc //设置@EnableWebMvc为完全接管SpringMvc,但一般不要设置完全接管SpringMvc
@Configuration
public class CustomMvcConfig implements WebMvcConfigurer { /**
* 配置自己的国际化语言解析器
* @return
*/
@Bean
public LocaleResolver localeResolver() {
return new MyLocaleResolver();
} /**
* 配置自己的拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//super.addInterceptors(registry);
} }
4、页面显示及切换国际化操作:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.ib{
display: inline-block;
}
.ml20{
margin-left: 20px;
}
.mt20{
margin-top: 20px;
}
</style>
</head>
<body>
<div>
<div>[[#{messages.user.name}]]:<input th:placeholder="#{messages.user.name}"/></div>
</div>
<div>
<div>[[#{messages.user.password}]]:<input th:placeholder="#{messages.user.password}"/></div>
</div>
<div>
<div><button>[[#{messages.user.btn}]]</button></div>
</div> <div class="mt20">
<span class="ib"><a th:href="@{/mapdemo/hello(i18n_language=zh_CN)}">中文</a></span>
<span class="ib ml20"><a th:href="@{/mapdemo/hello(i18n_language=en_US)}">英文</a></span>
</div> </body>
</html>
springBoot 实现中文国际化的更多相关文章
- SpringBoot起飞系列-国际化(六)
一.前言 国际化这个功能可能我们不常用,但是在有需要的地方还是必须要上的,今天我们就来看一下怎么在我们的web开发中配置国际化,让我们的网站可以根据语言来展示不同的形式.本文接续上一篇SpringBo ...
- SpringBoot系列——i18n国际化
前言 国际化是项目中不可或缺的功能,本文将实现springboot + thymeleaf的HTML页面.js代码.java代码国际化过程记录下来. 代码编写 工程结构 每个文件里面的值(按工程结构循 ...
- springboot + freemarker 实现国际化
前言 写过一篇springboot+freemarker国际化过程中的细节问题,但没有写过具体的国际化实现过程.正好有人在问,我就把我实现的过程贴出来,即使自己知识的一个备份,如果对别人有点用,那是再 ...
- SpringBoot 快速支持国际化i18n
学习目标 快速学会如何在工程中支持国际化语言. 快速查阅 专题阅读:<SpringBoot 布道系列> 源码下载:springboot-locale-i18n — Hey Man,Don' ...
- SpringBoot 配置文件 中文乱码
本方案,支持springboot 很简单 在配置文件中不写中文,写中文的ascll码 直接百度在线转ASCII,用工具 把中文转ASCII码==>\u628a\u4e2d\u6587\u8f6c ...
- SpringBoot + Spring MVC国际化使用示例
项目中需要显示中英文两种语言,所以需要对显示的内容进行国际化,如下是一个示例程序. 程序文件结构,如下图,后面详细列出各文件的代码. 1. 编写maven的pom.xml文件,如下: <proj ...
- Flutter中的日期插件date_format 中文 国际化 及flutter_cupertino_date_picker
今天我们来聊聊Flutter中的日期和日期选择器. Flutter中的日期和时间戳 //日期时间戳转换 var _nowTime = DateTime.now();//获取当前时间 print(_no ...
- springboot自动配置国际化失效分析
最近在整理springBoot国际化时,发现国际化没有生效,通过报错提示在 MessageTag -> doEndTag处打断点 最后发现messageSource并不是ResourceBund ...
- SpringBoot @RequestBody 中文乱码
今天突然想学习一下Restful风,详细的我就不赘述了,我的理解是同一个请求路径根据请求方式不同进行不同的处理 如四种提交方式,这里推荐一个插件Postman,可以模仿各种请求类型,自行百度安装吧 G ...
随机推荐
- svm原理及opencv
转自http://www.cnblogs.com/justany/archive/2012/11/23/2784125.html
- ubuntu14.04 64位安装 g2o
参考链接:http://blog.csdn.net/jiujiu932/article/details/52248577 http://www.cnblogs.com/gaoxiang12/p/473 ...
- [C++] any number to binary (Bit manipulation)
any number to binary (Bit manipulation)
- [GO]匿名字段的同名字段操作
package main import ( "fmt" ) type Person struct { name string sex byte age int } type Stu ...
- OSG图形设备接口GraphicsContext
1.图形设备与相机 在Camera类的成员函数中,setGraphicContext()函数的工作是设置相机对应的图形设备对象,换句话说,下面要介绍的GraphicsContext类就是图形设备对象的 ...
- java中interface使用
1.在C++中,类可以多重继承,一个类可以有好几个父类,但是在java中,类是不允许多重继承的,为了多重继承,java中出现了接口(interface)的定义.接口是可以多重继承的,接口的关键词是:i ...
- 设计模式7---Java动态代理机制详解(JDK 和CGLIB,Javassist,ASM)
class文件简介及加载 Java编译器编译好Java文件之后,产生.class 文件在磁盘中.这种class文件是二进制文件,内容是只有JVM虚拟机能够识别的机器码.JVM虚拟机读取字节码文件,取出 ...
- 怎么用谷歌浏览器查看请求或响应HTTP头?
要使用谷歌浏览器查看请求或响应HTTP标头,可以采取以下步骤: 在Chrome浏览器,访问一个网址,点击右键,选择检查,打开开发人员工具(或直接按F12). 选择 Network 选项卡. 重新加载页 ...
- python二维码操作:QRCode和MyQR入门
1.QRCode QRCode最简单的使用 import qrcode qrcode.make("第一个二维码").get_image().show() 根据文本生成二维码并且直接 ...
- 20145233《网络对抗》Exp5 MSF基础应用
20145233<网络对抗>Exp5 MSF基础应用 实验问题思考 什么是exploit,payload,encode exploit是发现了的可以利用的安全漏洞或者配置弱点,这类模块存储 ...